laravel关联两个表内容取出的办法

use Illuminate\Support\Facades\DB;

$articles = DB::table('articles')->join('category','articles.cateid','=','category.id')->select('articles.*','category.name')->get();

重点,一定记得select语句写上

搜索功能,同时联合两个表的写法:

$articles = DB::table('articles')->join('category','articles.cateid','=','category.id')
->where('title','like','%'.$_POST['searchname'].'%')
->whereBetween('articles.updated_at', [$_POST['logmin'], $_POST['logmax']])
->select('articles.*','category.name')
->get()->toArray();

两个表中的同一个字段记得用表名区分

posted @ 2017-07-05 20:20  mayer326  阅读(2412)  评论(0编辑  收藏  举报