0
votes

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>
1
the error says, you need to insert album_id while creating new instancesta
on your blade do you have the id of album ??sta
i don't have itDefa M
What should I do?Defa M
As per your migration table, your gallery item is belongsTo a album, so you need to pass the album_id while insert new record, or make it nullable (nullable is not a good idea as per your migration)sta

1 Answers

1
votes

Because in create_galeris_table.php migration file album_id field is not nullable or has a default value and in your GaleriController.php you are not inserting a value for album_id

So you could make album_id nullable in your migration file

$table->integer('album_id')->unsigned()->nullable();

Or insert a value for album_id while creating a new gallery record, so in store method you will add

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();

        $album = Album::find('some-id'); // this id of the album you want to add to this gallery
        $galeri->album_id = $album->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!']);
}