在使用Laravel的ORM—Eloquent时,经常会遇到的一个操作是获取模型中的某些属性,对应于获取数据库中表中的特定列。如果使用DBfacade来编写查询生成器,则只需要在链中调用select()方法:$users=DB::table('users')->select('name','emailasuser_email')->获取();有两种使用Eloquent的方式://1.使用select()$users=User::select(['name'])->get();//2.直接把列名数组作为参数传递给all()/get()/find()等方法$users=User::all(['name']);$admin_users=User::where('role','admin')->get(['id','name']);$user=User::find($user_id,['name']);在关系查询中使用相同的方法:$posts=User::find($user_id)->posts()->select(['title'])->get();$posts=User::find($user_id)->posts()->get(['title','description']);//注意不能使用动态属性(->posts)调用关系,需要使用关系方法(->posts())。
