phplaraveleloquentlaravel-8eloquent-relationship

"Illegal Offset Type" exception on "save()" method in Laravel


I've this simple application built with Laravel and Eloquent, that should connect to a localhost phpmyAdmin Database. I've got this method:

public function clickOrderPlateClient($id_client, $id_plate){
        $order = Order::where('id_client', $id_client)
            ->where('id_plate', $id_plate)
            ->first();

        $order->qt = ($order->qt) + 1;
        $order->save();
    }

which should increment by 1 the attribute "qt" in my table "orders" (described by the class Order) in the record corresponding to the given client which has ordered the given dish.

But I'm always getting an "Illegal offset type" error corresponding to the $order->save(); row.

error

this is my Order class:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    protected $table ='orders';
    protected $primaryKey = ['id_client','id_plate'];
    public $timestamps = false;
    public $incrementing = false;

    public function getClient(){
        return $this->belongsTo('App\Models\Client','id_client');
    }

    public function getPlate(){
        return $this->belongsTo('App\Models\Plates','id_plate');
    }
}


Solution

  • You can not use two primary keys in the model.

    Try to work with "composite key"

    Solution 1:

    With library:

    composer command to install the library

    composer require thiagoprz/eloquent-composite-key
    

    Model code:

    <?php
    namespace App\Models;
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    use Thiagoprz\EloquentCompositeKey\HasCompositePrimaryKey;
    class Order extends Model
    {
        use HasCompositePrimaryKey;
        protected $table ='orders';
        protected $primaryKey = ['id_client','id_plate'];
        public $timestamps = false;
        public $incrementing = false;
        public function getClient(){
            return $this->belongsTo('App\Models\Client','id_client');
        }
        public function getPlate(){
            return $this->belongsTo('App\Models\Plates','id_plate');
        }
    }
    

    For reference check here: Laravel: working with Composite Keys

    Solution 2:

    Without installing any library:

    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    
    class Order extends Model
    {
        protected $table ='orders';
        protected $primaryKey = 'composite_key';
        public $timestamps = false;
        public $incrementing = false;
    
        public function getClient(){
            return $this->belongsTo('App\Models\Client', 'id_client');
        }
    
        public function getPlate(){
            return $this->belongsTo('App\Models\Plates', 'id_plate');
        }
    
        // Define the composite key attribute
        public function getCompositeKeyAttribute()
        {
            return $this->id_client . '-' . $this->id_plate;
        }
    }