1

I have a table and I want to update it, but theres always a problem everytime i update, it says its null or request column not found 0, someone can help me? Any idea?

Update controller

 public function update(Request $request, string $id)
    {
        

          $invoice = Invoice::find($id);
          $input = $request->all();
          $invoice->update($input);

        $title = $request->title;
        $quantity = $request->quantity;
        $unit_price = $request->unit_price;
        $sort_order = $request->sort_order;

            $invoiceDetails = [];

            for ($i = 0; $i < count($title); $i++) {
                $invoiceDetails[] = [
                    'sort_order' => $sort_order[$i],
                    'title' => $title[$i],
                    'quantity' => $quantity[$i],
                    'unit_price' => $unit_price[$i],
                ];
            }

             

            $invoice = $invoice->invoice_detail()->update($invoiceDetails);

        return redirect('invoice')->with('flash_message', 'Updated');

    }

Invoice model

class Invoice extends Model
{
    use HasFactory;
    protected $table = 'invoices';
    protected $primaryKey = 'id';
    protected $fillable = ['user', 'company', 'invoice_month'];
    
    const UPDATED_AT = 'modified';
    const CREATED_AT = 'created';
    
    public function invoice_detail(){
        return $this->hasMany(Invoice_detail::class, 'invoice_id');

    }

}

Invoice detail model

class Invoice_detail extends Model
{
    use HasFactory;
    protected $table = 'invoice_details';
    protected $primaryKey = 'id';
    protected $fillable = ['title', 'quantity', 'unit_price', 'invoice_id'];

    const UPDATED_AT = 'modified';
    const CREATED_AT = 'created';
    public function invoice(){
        return $this->belongsTo(Invoice::class);
    }

}

route


Route::get('invoice', [InvoiceController::class, 'index']);

Route::get('search', [InvoiceController::class, 'search']);

Route::resource('/invoic', InvoiceController::class);

enter image description here

I just want the right code to update in laravel, its in an array, cause my view is a bootstrap table

9
  • Can you point out which line throws the exception exactly and quote the error message? Commented Dec 3, 2023 at 15:32
  • $invoice = $invoice->invoice_detail()->update($invoiceDetails); this one the error is SQLSTATE[HY000]: General error: 1 no such column: 0 Commented Dec 3, 2023 at 15:36
  • Can you show me the structure of your invoice_detail table? Commented Dec 3, 2023 at 15:37
  • you can click the link for the image, "database structure" please, i edited on the top Commented Dec 3, 2023 at 15:41
  • Are you sure you are connected to the right database? Commented Dec 3, 2023 at 15:46

2 Answers 2

0

Updating the child records should not be done like this, update your code to this code because you haven't any primary key ID of the child records. so first delete the child records and recreate them with updated records.

public function update(Request $request, string $id)
{
    $invoice = Invoice::findOrFail($id);
    $invoice->update($request->all());

    $title = $request->title;
    $quantity = $request->quantity;
    $unit_price = $request->unit_price;
    $sort_order = $request->sort_order;

    $invoiceDetails = [];

    for ($i = 0; $i < count($title); $i++) {
        $invoiceDetails[] = [
            'sort_order' => $sort_order[$i],
            'title' => $title[$i],
            'quantity' => $quantity[$i],
            'unit_price' => $unit_price[$i],
        ];
    }
    $invoice->invoice_detail()->delete();
    $invoice->invoice_detail()->createMany($invoiceDetails);

    return redirect('invoice')->with('flash_message', 'Updated');
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Khayam, ive been looking for this, youre brilliant and intelligent. Lots of thanks for helping me. I accepted this answer
0

You are passing a multidimensional array so your keys are nested therefore considered missing.

Change your code to:

    for ($i = 0; $i < count($title); $i++) {
        $invoiceDetails = [
            'sort_order' => $sort_order[$i],
            'title' => $title[$i],
            'quantity' => $quantity[$i],
            'unit_price' => $unit_price[$i],
        ];

        $invoice = $invoice->invoice_detail()->update($invoiceDetails);
    }

         

I am assuming based on your code that the variables you use to assign values to your keys are always present.

Maybe you should also consider some validation upon your request fields.

5 Comments

i tried that, its not working. It doesnt save any data
It goes to its original data, nothing is saved
Does your relation work properly? Is the invoiceDetails array having the right values that you are expecting through out the loop? You want to update the same relation inside the loop so basically your invoice_detail relation will get the last values of the loop. Do you actually need a loop for that? The error you were getting was for the reason i mentioned in my answer regarding the laravel update function usage.
yes it has the proper relationship because i was able to save it, on the save/insert part, the code is $invoice->invoice_detail()->createMany($invoiceDetails);
createMany and update are two different things, Their input also is different structured. Debug your invoicedetails array and see if it actually holds the values that you want to update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.