lundi 8 février 2016

Laravel: Many to Many Left Join

I need some help here!!! :) i have 4 tables

  • user
  • files
  • categories
  • sales

The relationships are many to many as follows:

  • user_categories
  • files_categories

and then i have another table called Sales where i store the user_id and the file_id. I want to select one random file that the user hasn't bought yet (meaning its not in the sales table) and where it match with the user categories found in the user_categories. With the following code i get a random file now:

$user = Auth::user();
$userCat = $user->categories()->get()->random(1);
$random_file= $userCat->files()->get()->random(1);

Additionally is the code ok for large tables or not. Any suggestions will be much appreciated! Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire