简体   繁体   中英

fetch data from multiple tabels

i have some table,one of them is user table that i want fetch id from this table by searching username and second one is prize_info table that i want to fetch user_id from here by searching a prize,after all i want fetch just prize_info.user_id and user.id.what is the best way for this search part in laravel4?? here is my codes

     $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();

what is your idea about above codes????? and i check the result by this line but it does not work!

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

please help me!best regard

I would advise you used Laravel's Eloquent ORM Relationships to do this. You can easily fetch related record using the available relationships;

  1. One-to-One
  2. One-to-Many
  3. BelongsTo
  4. BelongsToMany
  5. HasMany
  6. HasOne
  7. Polymorphic Tables (using pivot tables)

You can use this:

$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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM