Hi Guys,
I'm recently decided to make a switch to start coding with Laravel. I will try to specify my question as good as i can.
--
-- Tabelstructuur voor tabel `cms_posts`
--
CREATE TABLE IF NOT EXISTS `cms_posts` (
`post_id` int(11) NOT NULL AUTO_INCREMENT,
`post_title` varchar(255) NOT NULL,
PRIMARY KEY (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
-- --------------------------------------------------------
--
-- Tabelstructuur voor tabel `cms_text`
--
CREATE TABLE IF NOT EXISTS `cms_text` (
`text_id` int(11) NOT NULL AUTO_INCREMENT,
`text_content` text NOT NULL,
`text_post_id` int(11) NOT NULL,
PRIMARY KEY (`text_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
My router:
Route::get('posts', 'PostController@tooninfo');
My controller:
class PostController extends BaseController {
public function tooninfo()
{
$post = Post::all();
//$text = $post->berichten; // error
return View::make('post')->with('post', $post);
}
}
Post model:
class Post extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'cms_posts';
/**
* Defines a one-to-one relationship.
*
* @see http://laravel.com/docs/eloquent#one-to-one
*/
public function teksten()
{
return $this->hasOne('Text'); //return $this->hasOne('Text', 'text_post_id', 'post_id');
}
}
My text model:
class Text extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'cms_text';
public function berichten() {
return $this->belongsTo('Post');
}
}
My blade file. It shows the title.
<p>@foreach ($post as $p)
<p>This is user {{ $p->post_title }}</p>
@endforeach</p>
My question:
Every post 'cms_posts' has one text entry in the table 'cms_text'. Post_id and text_post_id contain the numbers that Laravel should match. As for now i get the error 'Something whent wrong'. Can anyone tell me to make this work? I read all over the internet but somehow i still don't understand.
I would like to accomplish the following in my blade model:
<p>@foreach ($post as $p)
<p>This is user {{ $p->post_title }}</p>
<br />
{{ $p->text_content }} (contains the text records from the text model
@endforeach</p>
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 }}
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!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community