I have a Conversation model like this :
class Conversation extends Model
{
protected $primaryKey = 'conversation_id';
public function questions (){
return $this->hasMany('App\Question','conversation_id','conversation_id');
}
public function latestQuestion ()
{
return $this->hasOne('App\Question','conversation_id','conversation_id')
->orderBy('created_at', 'desc');
}
}
And a Question model like this :
class Question extends Model
{
protected $primaryKey = 'question_id';
public function conversation ()
{
return $this->belongsTo('App\Conversation', 'conversation_id', 'conversation_id');
}
}
As you can see each Conversation can have some Question.
Furthermore, Conversation model has a latestQuestion relationship that fetches latest Question for that Conversation.
Now I want to list all Conversations with their latest question.
For that I wrote :
$conversations =
Conversation::
with('latestQuestion')
->orderBy('created_at','desc')
->get();
above code, orders conversations by their created_at field.
But I want to make a list of Conversations that conversations with new question be on top of list. in fact I want to order Conversations based On latest Question created_at field But I do not know How can I do that?