php - Laravel dropzone problem for upload with product_id

577

Suppose i have one product and it has many images . i used Dropzone js and if i upload images then its fine but if want to store with product_id then it pass null value . without passing any value its working fine. so how can i store both like image name and product_id at once ? Here is my controller:

 public function store(Request $request)
    {
        if ($request->hasFile('file')){

            $file= $request->file;
            $fileName = $file->getClientOriginalName();
            $fileName =  time() .'-'.$fileName;
            $productImage = public_path("uploads/products/{$fileName}"); // get previous image from folder
            if (File::exists($productImage)) { // unlink or remove previous image from folder
                unlink($productImage);
            }
            $file->move('public/uploads/products/',$fileName);
            $image  = new Image();
            $image->image = $fileName;
            $image->product_id = $request->product_id;
            $image->save();

//            return redirect()->route('product.index');

        }
}

Database Schema :

 Schema::create('images', function (Blueprint $table) {
            $table->increments('id');
            $table->string('image');
            $table->integer('product_id')->unsigned()->nullable();
            $table->foreign('product_id')->references('id')->on('products')->onUpdate('cascade')->onDelete('cascade');
            $table->timestamps();
        });

Error:

message: "SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (eshop.images, CONSTRAINTimages_product_id_foreign FOREIGN KEY (product_id) REFERENCESproducts (id) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert intoimages (image,product_id,updated_at,created_at) values (1538483231-blog-1-3.png, 123, 2018-10-02 12:27:11, 2018-10-02 12:27:11))"

809

Answer

Solution:

Mysql gives " Cannot add or update a child row: a foreign key constraint fails " because you don't have any product in products table for 123 id. First you need to create that product. After that you can create relational records for it

518

Answer

Solution:

you cannot update table if some columns are foreign key in another table.

If you want to update if firstly drop the table which foreign key had(images). After you can update thisproducts table.

372

Answer

Solution:

in @blade use

<input type="hidden" name="product_id" class="form-control m-input" value="{{$id}}">

instate of

<input type="hidden" name="product_id" class="form-control m-input" value="123">
589

Answer

Solution:

I can think of 3 reasons that might have a link to your problem:

1: it cold be that u need to make the column fillable in the model.

2: because u have it as a foreign_key u have to set the id automatically not with an insert (gets from origin) or unbind the value.

3: it means that the value u are inserting does not exists on the origin table there for u are getting this error.

Good luck

People are also looking for solutions to the problem: php - Laravel return redirect not working in iOS container

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.