laravel get only one column code example

Example 1: laravel get certain columns

ModelName::all('column1', 'column2', 'column3');

Example 2: eloquent all only one culomn

$collection = collect([
    ['product_id' => 'prod-100', 'name' => 'Desk'],
    ['product_id' => 'prod-200', 'name' => 'Chair'],
]);

$plucked = $collection->pluck('name');

$plucked->all();

// ['Desk', 'Chair']

Example 3: eloquent get only some columns

Table::select('name','surname')->where('id', 1)->get();

Example 4: laravel get single column value

$items = DB::table('items')
             ->select('id', 'ref_code', 'name', 'price')
             ->where('ref_code','=', $request->ref_code)
             ->first();

Tags:

Php Example