简体   繁体   中英

Laravel Eloquent belongsto relationship returns null

I have two Eloquent models:

1) Post

class Post extends Model
{
    protected $table = 'posts';
    protected $fillable = ['id', 'user_id', 'product_id', 'site_id', 'link_id', 'body', 'created_at', 'updated_at'];

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

    public function product(){
        return $this->belongsTo(Product::class);
    }

2) Product

protected $table = 'products';
protected $fillable = ['id', 'user_id', 'manufacturer_id', 'shift_product_id', 'name', 'english_name',
                        'slug', 'text', 'spec', 'live', 'created_at', 'updated_at'];

public function posts(){
    return $this->hasMany(Post::class);
}

I need to get the product from a post I do that:

$posts = Post::get();

foreach($posts as $key){
    dd($key->product);
}

Like this it returns NULL If I do like this: dd($key->product()); I get the product but I can't to use that在此处输入图片说明

but I need to get something like that to use whant I need:

在此处输入图片说明

Try to point out foregin key and other key in relation, examples:

public function post()
{
    return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
}

public function user()
{
    return $this->belongsTo('App\User', 'foreign_key', 'other_key');
}

More: https://laravel.com/docs/5.5/eloquent-relationships

The relationship probably doesn't exist in the database.

Based on your fillable array on Post , the way you have the relationships setup looks correct as you are following naming conventions for keys and your belongsTo relationship methods have the correct name for convention.

$post->product() is not returning your Product model. It is returning a Relation type object ( BelongsTo ). This is used for querying the relationship. $post->product would be the dynamic property for the relationship that would return the already loaded relationship or load the relationship and give you the result.

Laravel 5.5 Docs - Eloquent - Relationships - Relationship Methods Vs. Dynamic Properties

If the relationships are setup correctly $post->product being null would mean the relationship doesn't actually exist in the database, no matching id in products for product_id or product_id being null. (assuming no foreign key constraint)

Side note: eager loading the relationship would be a good idea:

$posts = Post::with('product')->get();

i found my problem i dont have in the DB product with ID = 1 :/ stuped problem

thanks for all the help i leran alot from u.

I just came across this post because I got a similar error while working on a project.

What I discovered is that when you query a model with the all() method, it ignores the related softdeleted rows.

When you try to access them tho, you get the null

Remember to hit save() after associate and dissociate. Got me a couple of times:

$model->relation()->associate($record)->save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM