laravel model select columns code example

Example 1: laravel eloquent get only field name

//Eloquent: Get specific columns (not all the row). Pluck returns an array.
Model::where('id', 1)->pluck('name', 'surname');
// If you only want to get the result value:
Model::where('id', 1)->value('name');

Example 2: laravel not in query

DB::table(..)->select(..)->whereNotIn('book_price', [100,200])->get();

Example 3: join in laravel eloquent

$customer = DB::table('customers')
                ->join('shops', 'customers.shop_id', '=', 'shops.shop_id')
                ->where('customer_contact', $contact_no)
                ->get();

Example 4: eloquent get only some columns

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

Example 5: eloquest how to select one specific column in database

ModelName::find($id, ['name', 'surname']);

Tags:

Misc Example