Quantumass soufiane Quantumass soufiane - 3 months ago 11
PHP Question

sort w/ left join laravel

I want to join the row (subcomment) that has the max marks so I tried this

$comments=$comments->leftJoin('sub_comments', function($join){
$join->on('comments.id', '=', 'sub_comments.comment_id');
$join->where('sub_comments.marks', '=', function($q){
$q->from('sub_comments')->selectRaw('max(marks)');
});


and I get this


Object of class Closure could not be converted to string


is there any way I can do this? I appreciate any helpful answer, thanks in advance.

Answer

I solve it by adding a hasMany relationship to the Model of the comments then I sorted the sub_comments ( second table «right table» ) inside the model like that

return $this->hasMany('App\SubComment')->orderBy('sub_comments.marks');

and it is working good

Comments