Sitemap

Laravel Query Builder Part -01 (Basic)

Aug 28, 2023

Basic query builder for starting…

Press enter or click to view image in full size
Laravel Query Builder Part -01 (Basic)

Getting all specific column values from all rows
Example: Gettings all emails from the user’s tables

Code:

//gettings all email from the users tables

$users = DB::table('users')->get();
foreach($users as $user){
$all_email = $user->email;
echo $all_email;
}

Getting Specific rows from the tables
Example: Gettings 3rd row of the user's tables

Get Ruman Ahmed’s stories in your inbox

Join Medium for free to get updates from this writer.

Code:

//gettings 3rd row of products tables

$products = DB::Table('products')->first(3);
return $products;

Gettings key-value pair from a single table
Example: Return a key value from brands tables (brandName is value and id is key)

Code:

//gettings key-value or associative array from brands table

$brands = DB::Table('brands')->pluck('brandName','id');
return $brands;

Next part link: Laravel Query Builder — (Part-02)