Home > Back-end >  How to insert select option form to database in Laravel
How to insert select option form to database in Laravel

Time:09-07

SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value (SQL: insert into gm (type, gram, carat, price, file, updated_at, created_at) values (bn-0003dw, 3, 0, 2115000, 1662456904_BN -0003DW.jpg, 2022-09-06 16:35:04, 2022-09-06 16:35:04))

I want to insert select option form into database but error appears as above.

This is my blade code for select option from relation database.

<select  name="category_id">
   <option value="option_select" disabled selected>Choose</option>
   @foreach ($category as $ct)
     <option value="{{$ct->id}}" {{old('category_id') == $ct->id ? 'selected' : ''}}>{{$ct->category_name}}</option>
   @endforeach
</select>

This is my controller code for insert into database.

public function create()
    {
        $category   = Category::all();
        return view('backend.pages.create', compact('category'));
    }

    public function createact(Request $request)
    {
        $this->validate($request,[
            'type'          => 'required|alpha_dash',
            'category_id'   => 'required|numeric',
            'gram'          => 'required|numeric',
            'carat'         => 'required|regex:/() /',
            'price'         => 'required|numeric',
            'file'          => 'required|file|image|mimes:jpeg,jpg|max:1048'
        ]);

        // Menyiapkan data gambar yg diupload ke variable $file
        $file = $request->file('file');
        $file_name = time()."_".$file->getClientOriginalName();

        // Isi dengan nama folder tempat kemana file diupload
        $upload_directory = 'p_gm';
        $file->move($upload_directory, $file_name);
        
        Goldmart::create([
            'type'          => $request->type,
            'category_id'   => $request->category_id,
            'gram'          => $request->gram,
            'carat'         => $request->carat,
            'price'         => $request->price,
            'file'          => $file_name
        ]);

        return redirect('/gm');
    }

This is my Gm model.

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Gm extends Model
{
    use SoftDeletes;

    protected $table = "gm";
    protected $fillable = ['type', 'category', 'gram', 'carat', 'price', 'file'];
    protected $dates = ['deleted_at'];

    public function category()
    {
        return $this->belongsTo('App\Category');
    }
}

This is my Category model.

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table = "category";
    protected $fillable = ['category_name'];

    public function gm()
    {
        return $this->hasOne('App\Gm');
    }
}

I tried to change value category_id to Null in gm table the error doesn't appears but category_id is not filled id from category table however Null. The result category column is empty.

CodePudding user response:

add category_id in your Gm model fillable array.then it will work

  • Related