jarektkaczyk / eloquence

Extensions for the Eloquent ORM
http://softonsofa.com
MIT License
1.09k stars 142 forks source link

Can't update or delete database records #268

Closed LukieB1 closed 3 years ago

LukieB1 commented 3 years ago

I'm using this package in my Lumen 8.0 project, it's all working fine up until the point i'm trying to update or delete a record in my database using the model's instance. When I call $model->delete() it does return true but the record in the database never get's deleted. Same goes for $model->update($array), the record does not get updated but the properties in the model itself do. Now I have this workaround to delete (or update) a record Model::where('id', $model->id)->delete() which does work, but it would be nice to just be able to use the model's instance..

To give an example of how I build my models with this package:

<?php

namespace App\Models;

use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Mappable;

class Status extends Model
{
    use Eloquence, Mappable;

    protected $maps = [
        'opened' => 'status_opened',
        'processed' => 'status_processed',
    ];

    protected $casts = [
        'opened' => 'boolean',
        'processed' => 'boolean',
    ];

    protected $fillable = [
        'opened',
        'processed',
    ];

    public function __construct()
    {
        parent::__construct();

        $this->appends = array_keys($this->maps);
        $this->hidden = array_values($this->maps);
    }
}
LukieB1 commented 3 years ago

I found out that in some of my models I was using a capitalized $primaryKey which caused the updates and deletes to fail.

protected $primaryKey = 'StatusID'; -> protected $primaryKey = 'statusid';