Laravel fetch data in details with condition

Laravel fetch data in details with condition

Friends, you must have learnt about crud operation in Laravel. Crud operation means inserting data in the database, displaying it in the printout, editing it and deleting it. In this tutorial, we will learn the fetch operation and with its help we will display the data from the database in our frontend.

By the way, fetch operation is a very big chapter because there are many factors inside it like where condition, like condition, join, there are such conditions. Today we will learn in detail about other conditions and use them to display our data on our website.

where clause:

$users = DB::table('users')
->where('votes', '=', 100)
->where('age', '>', 35)
->get();

$users = DB::table('users')
->where('name', 'like', 'T%')
->get();

$users = DB::table('users')->where([
  ['status', '=', '1'],
  ['subscribed', '<>', '1'],
])->get();
$users = DB::table('users')
  ->select('name', 'email as user_email')
  ->get();
orWhere and whereNot:

$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')
->get();

$users = DB::table('users') 
->whereNot('votes', '>', 100) 
->orWhere('name', 'John') 
->get();
whereBetween & whereNotBetween:

$users = DB::table('users')
->whereBetween('votes', [1, 100])
->get();

$users = DB::table('users')
->whereNotBetween('votes', [1, 100])
->get();
whereIn / whereNotIn / orWhereIn / orWhereNotIn:

$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();

$users = DB::table('users')
->whereNotIn('id', [1, 2, 3])
->get();

$users = DB::table('users')
->orWhereIn('id', [1, 2, 3])
->get();

$users = DB::table('users')
->orWhereNotIn('id', [1, 2, 3])
->get();
whereDate / whereMonth / whereDay / whereYear / whereTime

$users = DB::table('users')
->whereDate('created_at', '2016-12-31')
->get();

$users = DB::table('users')
->whereMonth('created_at', '12')
->get();

$users = DB::table('users')
->whereDay('created_at', '31')
->get();

$users = DB::table('users')
->whereYear('created_at', '2016')
->get();

$users = DB::table('users')
->whereTime('created_at', '=', '11:20:45')
->get();
whereColumn / orWhereColumn

$users = DB::table('users')
->whereColumn('first_name', 'last_name')
->get();

$users = DB::table('users')
->whereColumn([
  ['first_name', '=', 'last_name'],
  ['updated_at', '>', 'created_at'],
])->get();
join:

$users = DB::table('users')
   ->join('contacts', 'users.id', '=', 'contacts.user_id')
   ->join('orders', 'users.id', '=', 'orders.user_id')
   ->select('users.*', 'contacts.phone', 'orders.price')
   ->get();

 

 

 

 

 

 

Leave a Reply