I have found a couple of questions on Stackoverflow regarding this matter but alot of the fixes relate to older Laravel versions and don't seem to work on Laravel 5.6.
This is my model:
class Meal extends Model
{
protected $primaryKey = ['meal_id', 'branch_id'];
public $incrementing = false;
public function inhouseOrders(){
return $this->belongsToMany('App\InhouseOrder')->withPivot('qty');
}
public function branch(){
return $this->belongsTo('App\Branch');
}
}
This is the migration which created the table:
Schema::create('meals', function (Blueprint $table) {
$table->string('meal_id')->unique();
$table->decimal('unit_price', 8, 2);
$table->string('branch_id');
$table->foreign('branch_id')->references('branch_id')->on('branches')->onDelete('cascade');
$table->string('name');
$table->string('status');
$table->timestamps();
$table->primary(['meal_id', 'branch_id']);
});
I have a function in my controller MealsController
which is used to update meal status as such:
public function changeStatus($branch_id, $meal_id){
$meal = $this->meal->where('meal_id', $meal_id)->where('branch_id', $branch_id)->first();
//$this->meal->find([$meal_id, $branch_id]) doesn't work here so I have to chain two where() functions
$meal->status = 'unavailable';
$meal->save();
return redirect()->route('view_meals');
}
$meal->save()
throws an Illegal Offset type
error in the following funciton in Model.php
protected function getKeyForSaveQuery()
{
return $this->original[$this->getKeyName()]
?? $this->getKey();
}
EDIT Forgot to mention, I tried the fix mentioned in this question: Laravel Eloquent CANNOT Save Models with Composite Primary Keys
But it just gave me a App\Meal does not exist
error