php - Laravel 4 : How to retrieve data in a one-to-many relationship? -
php - Laravel 4 : How to retrieve data in a one-to-many relationship? -
(see codes below)
i have 2 models user , messages. want take 10 messages-with associated sender name-based on each of inputted conversation id , reverse data. have succeeded in getting messages couldn't scale downwards 10 messages each conversation/with associated sender name.
i have tried homecoming messages info users info either don't know how access sender's name or doesn't homecoming associated user :
$allmessages = messages::with('user')->wherein('conv_id',$conv_id);
i have tried homecoming 10 messages returns 10 messages based on messages got instead of based on each conversation.
$allmessages = messages::wherein('conv_id',$conv_id)->take(10);
how 10 messages based on id of each conversation messages' associated sender name? bonus points improvement of code. give thanks in advance!
user model
public function messages(){ homecoming $this->hasmany('messages'); }
messages model
public function user(){ homecoming $this->belongsto('user'); }
controller
public function index() { $timestamps = input::get('from'); //timestamp of latest message $conv_id = input::get('conv_id'); //array of conversation ids $allmessages = messages::wherein('conv_id',$conv_id); if(is_null($timestamps)){ $messages = $allmessages->orderby('created_at','desc'); }else{ asort($timestamps); $messages = $allmessages->where('created_at','>',end($timestamps)); } homecoming $messages->get()->reverse(); }
migrations
messages table
schema::create('messages', function(blueprint $table) { $table->increments('id'); $table->integer('user_id'); $table->integer('conv_id'); $table->text('body'); $table->timestamps(); });
user table
schema::create('users',function($table) { $table->increments('id'); $table->string('email')->unique(); $table->string('password',100); $table->string('name',150); $table->string('usertype',50); $table->boolean('block'); $table->string('remember_token',100); $table->timestamp('lastlogin_at'); $table->timestamps(); $table->softdeletes(); });
edit : var_dump preview result of werewolf's reply in chrome
0: {id:37, user_id:1, conv_id:2, body:damn mate, created_at:2014-06-20 00:55:32,…} 1: {id:38, user_id:1, conv_id:2, body:hello, created_at:2014-06-20 02:18:21,…} 2: {id:39, user_id:1, conv_id:2, body:dude, created_at:2014-06-20 02:20:10,…} 3: {id:40, user_id:1, conv_id:2, body:test, created_at:2014-06-20 06:52:37,…} 4: {id:67, user_id:1, conv_id:2, body:haha, created_at:2014-06-21 01:25:56,…} 5: {id:68, user_id:1, conv_id:2, body:hey, created_at:2014-06-21 01:26:14, updated_at:2014-06-21 01:26:14,…} 6: {id:69, user_id:1, conv_id:1, body:testa, created_at:2014-06-21 01:27:02,…} 7: {id:70, user_id:1, conv_id:1, body:testser, created_at:2014-06-21 01:27:32,…} 8: {id:115, user_id:1, conv_id:2, body:haha, created_at:2014-06-21 02:45:06,…} 9: {id:116, user_id:1, conv_id:2, body:test, created_at:2014-06-21 02:57:03,…}
you may seek this:
$allmessages = messages::with('user') ->where('conv_id', $conv_id) ->take(10) ->get();
this homecoming collection of messages
model need loop like:
@foreach($allmessages $message) {{ $message->user->name }} {{ $message->body }} @endforeach
also, may use:
// first message , user->name $allmessages->first()->user->name; // sec message , user->name $allmessages->get(1)->user->name;
php laravel laravel-4 eloquent one-to-many
Comments
Post a Comment