How to get or select specific columns in laravel eloquent

How to get or select specific columns using Laravel eloquent methods


Use select() method to select specific columns and get all data of those column with laravel eloquent get() method:

$models = YourModel::select('name','email','phone')->get();
$models = YourModel::select('name','email','phone')->where('id', 1)->get();


Use Laravel Eloquent get() method to get selected specific column data as given below:

$models = YourModel::get(['name','email','phone']);
$models = YourModel::where('id', 1)->get(['name','email','phone']);


Use Laravel Eloquent all() method to get selected specific column data as given below:

$models = YourModel::all(['name','email','phone']);
$models = YourModel::all(['name','email','phone'])->where('id', 1);


Use Laravel Eloquent find() method to get selected specific column data as with ID given below:

$models = YourModel::find($id, ['name','email','phone']);


© Copyright - 2018 - 2022 | All rights reserved at Funda of Web IT