When using Laravel's orm--eloquent, one of the most frequently encountered operations is to take some of the properties in the model, and the corresponding is to fetch a specific column of the table in the database.
If you use the DB façade to write query constructors, you only need to call the Select () method chained:
$users = db::table (' users ')->select (' name ', ' email as User_email ')->get ();
There are two ways of using eloquent:
1. Use SELECT ()
$users = user::select ([' name '])->get ();
2. Pass the column an array group as a parameter directly into all ()/get ()/find (), etc.
1 $users = User::all ([' name ']); 2 $admin _users = user::where (' role ', ' admin ')->get ([' id ', ' name ']); 3 $user = User::find ($user _id, [' name ']);
Use the same in an associative query:
$posts = User::find ($user _id)->posts ()->select ([' title '])->get ();
$posts = User::find ($user _id)->posts ()->get ([' title ', ' description ']);
Note that you cannot use dynamic properties (->posts) to invoke an affinity relationship, but you need to use an association relationship method (->posts ()).
Use Laravel's eloquent model to get the specified columns of the database