Quantumass soufiane Quantumass soufiane - 1 year ago 99
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){

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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download