Support the ongoing development of Laravel.io →
Eloquent Views Blade
Last updated 2 years ago.
0

Hi,

First you have to specify your foreign key at the table 'cms_text' that references 'post_id' on 'cms_post'.

Then you should specify local keys and foreign keys in your Model's relations (because it does not match with the naming conventions of laravel)

// Post Model
public function teksten()
    {
        return $this->hasOne('Text', 'text_post_id', 'post_id');
    }

// Text Model
public function berichten() 
   {
        return $this->belongsTo('Post', 'text_post_id', 'post_id');
    }

Finally, to invoke the relation, you should use the same name as specified in your model.

// your view
{{ $p->teksten->text_content }}
Last updated 9 years ago.
0

Hi Othmanus,

Great, your solution solved my problem!

In order for laravel to recognize it automaticly i should remove the text_ from text_post_id so both tables have the same key in their table if i understand i right?

A big thankyou!

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

vioza vioza Joined 18 Feb 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.