mercredi 20 novembre 2019

Laravel 5 string as primary key

I'm working on a project that uses Laravel 5.5 and one of the models has its ID defined by the user. Below the model, setting $incrementing to false and having it in the $fillable array.

class AIOpportunity extends Model
{
    protected $table = 'ai_opportunity';
    protected $primaryKey = 'id_opportunity';
    public $incrementing = false;

    protected $fillable = [
        'id_opportunity',
        'budget',
        'created_by'
    ];
}   

The problem: when trying to execute this query :

$rec = AIOpportunity::where('id_opportunity', 'abc')->first();

It executed the following query in the database:

select * from ai_opportunity where id_opportunity=abc

Which throws an error because of the type. Is there something else I can do so Eloquent understands it's a varchar field?

Thanks for any help



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire