php - How to use an array from a query result and to then be used again for another query -


the scenario have friend lend me borrowed books if knows can same books @ library. following query:

$collectbook= libraryone::where('user_id', auth::id())->get(); 

he needs loop through array collect relevant ids predicting has 4 books unsure.

if (isset($collectbook)) { foreach ($collectbook $books =>$values)                if($books==0){              $first_book = ($values->book_id);             }elseif ($books==1){             $second_book = ($values->book_id);              }elseif($books==2) {              $third_book = ($values->book_id);             }elseif($books==3) {              $fourth_book = ($values->book_id);             }     } 

i runs 4 separate queries check if has matching books

try {             $collectbookone= librarytwo::where('user_id', auth::id()) ->where('item_id', $first_book)->first(); }catch(\exception $e){  } try { $collectbooktwo= librarytwo::where('user_id', auth::id()) ->where('item_id', $second_book)->first(); }catch(\exception $e){  } try { $collectbookthree= librarytwo::where('user_id', auth::id()) ->where('item_id', $third_book)->first(); }catch(\exception $e){  } try{ $collectbookfour= librarytwo::where('user_id', auth::id()) ->where('item_id', $fourth_book)->first(); }catch(\exception $e){  } 

the question whether knows way of listing matching books library 1 , library 2 in more more elegant way above. not knowing how many books 1 has in first place problem..thanks in advance

you should use sql joins. based on functions using laravel. have model libraryone , librarytwo. assuming actual table name libraryone libraryone , librarytwo librarytwo, code should this:

db::table('libraryone')->where('libraryone.user_id', auth::id())->join('librarytwo', function($join)     {         $join->on('libraryone.book_id', '=', 'librarytwo.item_id')->where('librarytwo.user_id', auth::id());      }) ->get(); 

this list books both on libraryone , librarytwo. im curious why book identifier on libraryone book_id while on librarytwo item_id. piece of advice, learn read sql joins. need knowledge moving forward in career.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -