lundi 26 octobre 2020

General Error with laravel General error: 1366 Incorrect integer value: for column image_id

i'm not sure what is wrong
"message": "SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'C:\Users\roman\AppData\Local\Temp\php85A7.tmp' for column 'image_id' at row 1 (SQL: insert into foods (name, description, price, offer_price, quantity, image_id, updated_at, created_at) values (Hussain Wael, kasdhv;loasid, 100, 1, 12, C:\Users\roman\AppData\Local\Temp\php85A7.tmp, 2020-10-26 22:25:30, 2020-10-26 22:25:30))",

store function

    public function store(FoodRequest $request)
    {
        $data = Food::create($request->all());


        if ($file = $request->file('image_id')) {
            $path = $file->store('image');

            $data['image_id'] = Image::create([
                'name' => $file->getClientOriginalName(),
                'size' => $file->getSize(),
                'path' => $path
            ])->id;
        }
        return $this->response_api(true, __('front.success'), StatusCodes::OK);
    }

my migration

 Schema::create('foods', function (Blueprint $table) {
        $table->bigIncrements('id');

        $table->string('name');
        $table->unsignedBigInteger('image_id');
        $table->integer('quantity')->default(1);
        $table->float('price' , 10 , 2)->default(1);
        $table->string('addons')->default(0);
        $table->longText('description');
        $table->float('offer_price',10,2)->default(0);
        $table->unsignedInteger('category_id')->nullable();
        $table->unsignedBigInteger('menu_id')->nullable();
        $table->timestamps();
    });

My request :

public function rules()
{
    return [
        'name'=>['string','required'],
        'image_id'   => 'required_unless:_method,PUT|nullable|image',
        'quantity'=>['required','numeric'],
        'category_id' =>[ 'nullable','exists:categories,id',],
        'menu_id' =>[ 'nullable','exists:menus,id',],
        'price'=>'required','numeric','min:1',
        'offer_price'=>['nullable','numeric'],
        'description' =>[ 'nullable',],
        'addons' =>[ 'nullable'],

    ];
}

Model :

class Food extends Model

{

 protected $fillable = [
        'parent_id',
        'menu_id',
        'name',
        'image_id',
        'quantity',
        'price',
        'addons',
        'description',
        'offer_price'];

Relations with images table:

 public function image()
    {
        return $this->belongsTo(Image::class)->withDefault();
    }


Schema::create('images', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('size');
        $table->string('path');
        $table->timestamps();
    });


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire