Laravel / Model / Single column in multi records in to single record using pluck() in query
Single column in multi records in to single record using pluck() in query
-
STEP
Example 1
Outputnamespace App\Http\Controllers; use Illuminate\Support\Facades\Http; use App\Models\Product; class ITSController extends Controller { /** * Write code on Method * * @return response() */ public function index() { $products = Product::pluck('name')->toArray(); dd($products); } } Array ( [0] => Samsung Galaxy [1] => Apple iPhone 12 [2] => Google Pixel 2 XL [3] => LG V10 H800 ) Example 2
Outputnamespace App\Http\Controllers; use Illuminate\Support\Facades\Http; use App\Models\Product; class ITSController extends Controller { /** * Write code on Method * * @return response() */ public function index() { $products = Product::select('name')->get()->toArray(); dd($products); } } Array ( [0] => Array ( [name] => Samsung Galaxy ) [1] => Array ( [name] => Apple iPhone 12 ) [2] => Array ( [name] => Google Pixel 2 XL ) [3] => Array ( [name] => LG V10 H800 ) )