lundi 26 avril 2021

Larave BelongsTo relationship returns null

I have two Eloquent models:

1) Project :

class Project extends Model
{
    protected $table = 'projects';
    protected $fillable = ['id', 'user_id', 'account_id'];

    public function user(){
        return $this->belongsTo(User::class);
    }

    public function account()
    {
        return $this->belongsTo(Account::class, "account_id");
    }

2) Account :

class Account extends Model
{
    // ...

    public function projects(){
        return $this->hasMany(Project::class);
    }

The goal is to have returned the account of the project :

Project::with("account")->get()->pluck("account");

Result :

null


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire