haisom
haisom

Reputation: 125

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 enter image description here

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

enter image description here

Upvotes: 4

Views: 45904

Answers (5)

digout
digout

Reputation: 4252

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

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

Upvotes: 0

truffolone
truffolone

Reputation: 664

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

Upvotes: 2

haisom
haisom

Reputation: 125

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.

Upvotes: 4

lagbox
lagbox

Reputation: 50481

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();

Upvotes: 3

Adam Kozlowski
Adam Kozlowski

Reputation: 5896

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

Upvotes: 16

Related Questions