//Post model
<?php
class Post extends \Eloquent {
protected $fillable = ['title' ,'body'];
public function user(){
return $this->belongsTo('User');
}
}
//User model
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'signup';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
public function posts(){
return $this->hasMany('Post');
}
}
//PostController
<?php
class PostController extends BaseController{
public function listing(){
$posts = Post::all();
return View::make('post.listing', compact('posts'));
}
?>
//UserController
public function show($id){
$user = User::find($id);
return View::make('user.show', compact('user'));
}
//listing
@extends('layout.default')
@section('content')
@foreach($posts as $post)
<h1>{{{ $post->title }}} By {{{$post->user->email}}}</h1>
@endforeach
@stop
//show
@extends('layout.default')
@section('content')
@foreach($user->posts as $post)
<li>{{$post->email}} {{{$post->title}}}</li>
@endforeach
@stop
@section('sidebar')
<p><a href="#">Link</a></p>
<p><a href="#">Link</a></p>
<p><a href="#">Link</a></p>
@stop
//Routes
Route::get('post/listing', array('uses'=>'PostController@listing', 'as'=>'post.listing'));
Route::resource('user', 'UserController');