mostafasadek's avatar

"SQLSTATE[HY000]: General error: 1364 Field 'images' doesn't have a default value (SQL: insert into `products` (`category_id`, `name_ar`, `name_en`, `shortDetail ▶"

Hi, I have this error from yesterday i tried many times to solve it but i can't so i need someone to guide me :

Product model

class Product extends Model
{
  protected $fillable =[

      'category_id','images','name_ar','name_en','shortDetails_ar','shortDetails_en',
      'quantity','size','color','ref','description_ar','description_en','additionalInfo_ar','additionalInfo_en',
      'price','created_at','updated_at',


];

Migration

public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('category_id');
            $table->string('images');
            $table->string('name_ar');
            $table->string('name_en');
            $table->text('shortDetails_ar');
            $table->text('shortDetails_en');
            $table->integer('quantity');
            $table->string('size');
            $table->string('color');
            $table->string('ref');
            $table->text('description_ar');
            $table->text('description_en');
            $table->text('additionalInfo_ar');
            $table->text('additionalInfo_en');
            $table->double('price', 15, 8);
            $table->timestamps();

        });

Controller code

 $product = Product::create([
        'category_id'=> $request->category,
        'name_ar'=> $request->name_ar,
        'name_en'=> $request->name_en,
        'shortDetails_ar'=> $request->shortDetails_ar,
        'shortDetails_en'=> $request->shortDetails_en,
        'quantity'=> $request->quantity,
        'size'=> $request->size,
        'color'=> $request->color,
        'ref'=> $request->ref,
        'description_ar'=> $request->description_ar,
        'description_en'=> $request->description_en,
        'additionalInfo_ar'=> $request->additionalInfo_ar,
        'additionalInfo_en'=> $request->additionalInfo_en,
        'price'=> $request->price,


        ]);
        $product->save();

        if($request->hasfile('images'))
        {

            foreach ($request->file('images') as $key => $value)
            {

                if ($products = $value)
                {
                    $destinationPath = 'public/files/'; // upload path
                    $fileName = date('YmdHis') . "." . $products->getClientOriginalExtension();
                    $products->move($destinationPath, $fileName);
                    $save[]['images'] = "$fileName";
                }
            }

        }

        Product::insert($save); // store file into mysql database


0 likes
16 replies
CliffordAtCaveoDotNL's avatar

In your migration the images field should be nullable:

$table->string('images')->nullable();
mostafasadek's avatar

No but i did it below in the request because when i do it in create method it gave me error in array

CliffordAtCaveoDotNL's avatar

Actually this whole section looks weird:

if($request->hasfile('images'))
        {

            foreach ($request->file('images') as $key => $value)
            {

                if ($products = $value)
                {
                    $destinationPath = 'public/files/'; // upload path
                    $fileName = date('YmdHis') . "." . $products->getClientOriginalExtension();
                    $products->move($destinationPath, $fileName);
                    $save[]['images'] = "$fileName";
                }
            }

        }

        Product::insert($save); // store file into mysql database

You are comparing an image with a product object and only saving a product with images later on.

Before this if statement you are creating and then saving the product.

Sergiu17's avatar
$product = new Product();
$product->category_id = $request->category_id;
// and so on for every single attribute
$product->images = ''; // an empty string
// or make it nullable, or add default value in your migration file



if($request->file('images')) {
  $images = [];

  foreach ($request->file('images') as $key => $value) {
    $fileName = date('YmdHis') . "." . $products->getClientOriginalExtension();

    $images[] = $fileName;
  }

  $product->images = implode('|', $images);
}

$product->save();
Sergiu17's avatar

No but i did it below in the request because when i do it in create method it gave me error in array

and this is why you get the error

mostafasadek's avatar

Here is the product controller after editing it

 $product = Product::create([
        'category_id'=> $request->category,
        'name_ar'=> $request->name_ar,
        'name_en'=> $request->name_en,
        'shortDetails_ar'=> $request->shortDetails_ar,
        'shortDetails_en'=> $request->shortDetails_en,
        'quantity'=> $request->quantity,
        'size'=> $request->size,
        'color'=> $request->color,
        'ref'=> $request->ref,
        'description_ar'=> $request->description_ar,
        'description_en'=> $request->description_en,
        'additionalInfo_ar'=> $request->additionalInfo_ar,
        'additionalInfo_en'=> $request->additionalInfo_en,
        'price'=> $request->price,
        'images'=> $request->images,


        ]);

        $product = new Product();
        $product->category_id = $request->category_id;
// and so on for every single attribute
        $product->images = ''; // insert a empty string
// or make it nullable, or add default value



        if($request->file('images')) {
            $images = [];

            foreach ($request->file('images') as $key => $value) {
                $fileName = date('YmdHis') . "." . $products->getClientOriginalExtension();

                $images[] = $fileName;
            }

            $product->images = implode('|', $images);
        }

        $product->save();



    }



mostafasadek's avatar

And here is the migration

Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('category_id');
            $table->string('images')->nullable();
            $table->string('name_ar');
            $table->string('name_en');
            $table->text('shortDetails_ar');
            $table->text('shortDetails_en');
            $table->integer('quantity');
            $table->string('size');
            $table->string('color');
            $table->string('ref');
            $table->text('description_ar');
            $table->text('description_en');
            $table->text('additionalInfo_ar');
            $table->text('additionalInfo_en');
            $table->double('price', 15, 8);
            $table->timestamps();


Sergiu17's avatar
// remove this
 $product = Product::create([
        'category_id'=> $request->category,
        'name_ar'=> $request->name_ar,
        'name_en'=> $request->name_en,
        'shortDetails_ar'=> $request->shortDetails_ar,
        'shortDetails_en'=> $request->shortDetails_en,
        'quantity'=> $request->quantity,
        'size'=> $request->size,
        'color'=> $request->color,
        'ref'=> $request->ref,
        'description_ar'=> $request->description_ar,
        'description_en'=> $request->description_en,
        'additionalInfo_ar'=> $request->additionalInfo_ar,
        'additionalInfo_en'=> $request->additionalInfo_en,
        'price'=> $request->price,
        'images'=> $request->images,


        ]);
mostafasadek's avatar

"Call to undefined method App\Models\Product::getClientOriginalExtension()"

mostafasadek's avatar

Please can i send you access for me at anydesk software to see the whole code better ? or team viewer , zoom any software

Sergiu17's avatar

create a private/public repository in github if you'd like

Please or to participate in this conversation.