簡體   English   中英

從多個表格中獲取數據

[英]fetch data from multiple tabels

我有一些表,其中一個是我要通過搜索用戶名從此表中獲取ID的用戶表,第二個是我要通過搜索獎品從此處獲取user_id的prize_info表,畢竟我只想獲取pril_info.user_id和user.id.laravel4中此搜索部分的最佳方法是什么? 這是我的密碼

     $search = '%'.Input::get('keywords').'%';
    $pages      = DB::table('user')
    ->select('user.id','user.username')
    ->where('username', 'LIKE', $search)->get();

    $blogitems  = DB::table('prize_info')
    ->select('prize_info.id', 'prize_info.prize_name', 'prize_info.user_id')
    ->where('prize_name', 'LIKE', $search)->get();

您對上述代碼有何想法???? 我通過這一行檢查結果,但它不起作用!

$results = $pages->union($blogitems)->take(30)->get();

請幫助我!最好的問候

我建議您使用Laravel的雄辯的ORM關系來做到這一點。 您可以使用可用的關系輕松獲取相關記錄;

  1. 一對一
  2. 一對多
  3. 屬於
  4. 屬於許多
  5. 有很多
  6. HasOne
  7. 多態表(使用數據透視表)

您可以使用此:

$search = '%'.Input::get('keywords').'%'
$results = DB::table('user')
                    ->select('user.id','user.username','prize_info.id', 'prize_info.prize_name', 'prize_info.user_id')
                    ->leftJoin('prize_info','user.id','prize_info.user_id')
                    ->take(30)
                    ->get();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM