mysql - How add to collection only models with no record joined - Eloquent -


in model questions have simple relation standpoint

public function standpoints_byrel() { //    return $this->hasmany('app\models\standpoint'); return $this->hasmany('app\models\standpoint',  'question_id');  } 

now, have yet model userattitude (tableuser_attitudes`) allow users upvote , downvote standpoints.

i able list standpoints, voted given user:

    $user_attitudes = userattitude::join('entitystandpoints', function ($q) use($questionid,$user) {         $q->where('user_attitudes.item_type', '=', 'entitystandpoint');         $q->on('user_attitudes.item_id', '=', 'entitystandpoints.id');         $q->where('entitystandpoints.question_id', '=', $questionid);         $q->where('user_attitudes.creator_id','=', $user);     })     ->select('user_attitudes.*')     ->get(); 

to do

now try list standpoints, not voted given user. have no idea how using eloquent. appreciated.

edit

condition meet: if user votes or down, new model userattitude created. therefore standpoint models not down- or upvoted have nothing join. still, in userattitude there 2 fields upvoting : 'attitude' , 'importance'. 1 of them null

try left join left parameter of join null. (but please check syntax out, i'm not eloquent expert):

   $user_attitudes = userattitude::leftjoin('entitystandpoints', function ($q) use($questionid,$user) {         $q->where('user_attitudes.item_type', '=', 'entitystandpoint');         $q->on('user_attitudes.item_id', '=', 'entitystandpoints.id');         $q->where('entitystandpoints.question_id', '=', $questionid);         $q->where('user_attitudes.creator_id','=', $user);     })     ->wherenull('entitystandpoints.id')     ->select('user_attitudes.*')     ->get(); 

let me know.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -