I want to create a photo album using laravel version 8.9.0 I have created an album, when I click on an album I will go to the gallery view, when I upload a photo an error occurs like this
General error: 1364 Field 'album_id' doesn't have a default value (SQL: insert into galeris
(deskripsi
, user_id
, image
, updated_at
, created_at
) values (asa, 1, 1604133200.png, 2020-10-31 08:33:20, 2020-10-31 08:33:20))
create_galeris_table.php
public function up()
{
Schema::create('galeris', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('user_id');
$table->integer('album_id')->unsigned();
$table->string('image');
$table->text('deskripsi');
$table->foreign('album_id')->references('id')->on('albums')->onDelete('CASCADE')->onUpdate('CASCADE');
$table->timestamps();
});
}
GaleriController.php
public function store(Request $request)
{
$request->validate([
'deskripsi' => 'required',
'image' => 'required|image|mimes:jpg,jpeg,png|max:2000',
]);
$galeri = New Galeri;
$galeri->deskripsi = $request->deskripsi;
$galeri->user_id = auth()->id();
if ($request->hasFile('image')) {
$file = $request->file('image');
$fileName = time().'.'.$file->getClientOriginalExtension();
$destinationPath = public_path('images/galeri');
$file->move($destinationPath, $fileName);
$galeri->image = $fileName;
}
$galeri->save();
return back()->with(['success' => 'Data Berhasil Disimpan!']);
}
galeri.blade.php
<div class="modal fade" id="uploadImage" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<form action="{{route('galeri.store')}}" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<input type="file" name="image" class="form-control" style="padding-top: 3px;">
</div>
<div class="form-group">
<textarea name="deskripsi" class="form-control" placeholder="Deskripsi"></textarea>
</div>
<button type="submit" class="btn btn-success btn-block">Save</button>
</form>
</div>
</div>
</div>
</div>
album_id
while creating new instance – staid
ofalbum
?? – sta