Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Query Builder for Beginner In Laravel

1.Find Unique column and avoid duplicate column

3 method using distinct, groupby, pluck command

OR

OR

In above table triptitle field land of rising sun and windy city r unique land of rising 3 times repeated

2 Find unique record

$categories = Rvsp::distinct()->get();

3.count total no of rows

here total 4 rows are there

$Trips = DB::table(‘rvsps’)
->select(‘TripTitle’)
->distinct()->count();
return $Trips;

count{{$Trips}} in blade file

4.display unique field and their total no of entries

here trip title Land of the Rising Sun 3 times entry in table triptitle Windy City one times see in below table mentioned in screenshot image

$touroperator = Rvsp::where(‘TripTitle’,$TripTitle)->latest()->paginate(20);
->select(‘TripTitle’, DB::raw(‘COUNT(TripTitle) as total_member’))
->groupBy(‘TripTitle’)
->havingRaw(‘COUNT(TripTitle) > 0’)
->get();

5.display particular colunm that is requested in blade file

first check requested value in blade file

$tour->TripTitle is requested

requested value is land of rising sun

now find total record of requested value using this query
$touroperator = Rvsp::where(‘TripTitle’,$TripTitle)->latest()->paginate(20);

6.display particular colunm that is present in another table based on condition

display all city of india

here three table city,state,country is present

Firstly retrive all state from state table that is under india where india id is 101 using pluck command

  $states = State::where(‘states.country_id’,’101′)->get();

then pass this variable to where condition to match all cities that is under india state

 $city = DB::table(‘cities’)->whereIn(‘cities.state_id’, $states)->get();