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

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;

Leave a Reply

Your email address will not be published. Required fields are marked *