composer dumpautoload
如果使用DB门面写查询构造器,那只需要链式调用select()方法即可:
$users = DB::table('users')->select('name', 'email as user_email')->get();
使用Eloquent的话,有两种方式:
1. 使用select()
$users = User::select(['name'])->get();
2. 直接将列名数组作为参数传入all()/get()/find()等方法中
1 $users = User::all(['name']); 2 $admin_users = User::where('role', 'admin')->get(['id', 'name']); 3 $user = User::find($user_id, ['name']);
在关联查询中使用同理:
$posts = User::find($user_id)->posts()->select(['title'])->get();
$posts = User::find($user_id)->posts()->get(['title', 'description']);
视图参数传递用compact
0down vote |
|
Goutte
$data = ModelA::find($id, ['column1', 'column2']);
$data = ModelA::first(['column1', 'column2']);
$data = ModelA::where(['column1', '=', 'value'])->get(['column1', 'column2']);