using WHERE condition1 AND condition2 in ::selectraw - php

I am using ::selectRaw inside my PHP function and I need that statement to get two conditions. In my case, I am using my category_id column
I tried to do two where but seems like these conflict with each other:
$query = self::selectRaw((is_array($fields)?implode(", ",$fields):$fields))
->where('category_id', '=', 1)
->where('category_id', '=', 2)
->where(function($q) use($search){
if($search){
return $q->where(['group_name' => $search]);
}
});
how do I translate WHERE category_id = 1 AND category_id = 2 in the ::selectRaw query?

You can use the whereIn function.
$query = self::selectRaw((is_array($fields)?implode(", ",$fields):$fields))
->whereIn('category_id', [1,2])
->where(function($q) use($search){
if($search){
return $q->where(['group_name' => $search]);
}
});

Related

Laravel and PHP - Condition in the SQL request

I have a query
$BaseUsers = DB::table('users', 'users.ustatus', '=', '1')
->join('user_infos', 'user_infos.user_id', '=', 'users.id')
->select('users.*', 'user_infos.add1', 'user_infos.add3')
->paginate(3);
It work fine. But i need insert condition "where" to search in result
i send $request from form. How insert condition to query?
In PHP it looks like this:
if($request['add_1'])
{
$insert1=" and add_1<>'' ";
}
if($request['add_2'])
{
$insert2=" and add_2<>'' ";
}
...
$sql=" SELECT users.*, user_infos.add1, user_infos.add3 FROM users JOIN user_infos where users.ustatus=1 and user_infos.user_id=users.id ".$insert1." ".$insert2."";
How can this be done on laravel in Controller? I mean - just insert the necessary condition insert1, insert2 ... into the query
you can use where() and has() to build your query
$query = DB::table('users');
if($request->has('age')) {
$query->where('age', '>', $request->age);
}
$query = $query->get();
I hope it's helpful
All is simple ) I use when in query
$BaseUsers = DB::table('users', 'users.ustatus', '=', '1')
->join('user_infos', 'user_infos.user_id', '=', 'users.id')
->when($request->input('f_add3'), function ($query) {
return $query->where('user_infos.add3', '<>', '');
})
->select('users.*', 'user_infos.add1', 'user_infos.add3')
->paginate(3);

how to convert laravel query

(select business_name from clients where id = orders.main_client_id) as b_name
This above sql query and I am converted sql to laravel type is right or wrong because this below query give me output [][][] this type any suggestion..
->addColumn('business_name', function($data) {
$b_name = DB::table("clients")
->select("business_name")
->where('clients.id','main_client_id')
->get();
return $b_name;
});
You left out the comparison symbol (=)
$b_name = DB::table('clients')
->where('id', '=', 'main_client_id')
->get();
or
$b_name = DB::table('clients')
->where('clients.id', '=', 'main_client_id')
->value('business_name');
you are comparing with column value not mere value, so you have to use whereColumn:
->addColumn('business_name', function($data) {
$b_name = DB::table("clients")
->select("business_name")
->whereColumn('clients.id','main_client_id')
->get();
return $b_name;
});

Subquery Where condition on Laravel query builder

my problem is simple, I'm a student, I'm learning laravel, i don't know how to create a where condition from a subquery.
This is the query
"SELECT id_parameter,value,code_rule,block,grouping,count FROM rule_definition WHERE (code_rule IN (SELECT code_rule FROM rule_definition WHERE id_parameter = 1 AND value = '$x') AND (id_parameter = 1 AND value = '$x')) OR (id_parameter != 1 AND value != '$x')";
Im using laravel 6.0 query builder.
Thanks
I don't think you need that subquery. Because it from the same table and same condition.
$query = DB::table('rule_definition')
->select('id_parameter, value, code_rule, block,grouping, count')
->where(function ($q) use ($x) {
$q->where('id_parameter','=',1)
->where('value','=',$x);
})->orWhere(function ($q) use ($x) {
$q->where('id_parameter','!=',1)
->where('value','!=',$x);
})
$query = DB::table('rule_definition')->where(function ($q){
$q->where('id_parameter','=',1)
->where('value','=','$x');
})->orWhere(function ($q){
$q->where('id_parameter','!=',1)
->where('value','!=','$x');
})->select('id_parameter, value, code_rule, block,grouping, count')

Join to leftJoin queries in Laravel

I have a query that Im trying to retrieve. Its suppose to get a listings information with its trips and location details.
This is how Im calling the query inside the Destinations controller:
public function destinations($id) {
$destination = Destination::findOrFail($id);
$listingGuides = Listing::findGuidesTrips($destination)
->with('locations')
->withCount('trips')
->get();
return view('destinations.index', compact('listingGuides');
}
And the findGuidesTrips method is inside the Listings Model:
public static function findGuidesTrips($destination) {
$query = self::query()
->leftJoin('trips', 'listing_id', '=', 'listings.id')
->addSelect(
\DB::raw('listings.name,listings.slug,listings.type,listings.id,MIN(trips.cost) as starting_price')
)
->groupBy('listings.id');
$query = self::query()
->leftJoin('locations', 'listing_id', '=', 'listings.id')
->addSelect(
\DB::raw('locations.longitude as longitude')
)->addSelect(
\DB::raw('locations.latitude as latitude')
);
$query = $query->whereHas('locations',function($query) use ($destination) {
$query->where('region', 'like', $destination->location)->orWhere('country', $destination->location);
});
return $query;
}
This is what I get back:
As you can see, I have 2 $query = self::query() quires, but only one is being called (the bottom one). Its ignoring the top self::query.
I was just wondering how would I go about combining these 2 leftJoin queries into one perhaps? Or is there a better way of doing this query?
( I tried doing this: )
$query = self::query()
->leftJoin('trips', 'listing_id', '=', 'listings.id')
->addSelect(
\DB::raw('listings.name,listings.slug,listings.type,listings.id,MIN(trips.cost) as starting_price')
)
->leftJoin('locations', 'listing_id', '=', 'listings.id')
->addSelect(
\DB::raw('locations.longitude as longitude')
)->addSelect(
\DB::raw('locations.latitude as latitude')
)->groupBy('listings.id');
But it gives me Integrity constraint violation: 1052 Column 'listing_id' in on clause is ambiguous error
As stated by #Tim Lewis and #Niklesh, all I had to do was:
trips.listing_id for first query and locations.listing_id for second.
Here is the final query:
public static function findGuidesTrips($destination) {
$query = self::query()
->leftJoin('trips', 'trips.listing_id', '=', 'listings.id')
->addSelect(
\DB::raw('listings.name,listings.slug,listings.type,listings.id,MIN(trips.cost) as starting_price')
)
->leftJoin('locations', 'locations.listing_id', '=', 'listings.id')
->addSelect(
\DB::raw('locations.longitude as longitude')
)->addSelect(
\DB::raw('locations.latitude as latitude')
)->groupBy('listings.id');
$query = $query->whereHas('locations',function($query) use ($destination) {
$query->where('region', 'like', $destination->location)->orWhere('country', $destination->location);
});
return $query;
}

Laravel Eloquent, return rows where both conditions are True

I have a table of Weeks that is joined to a property table, Weeks table looking like this:-
PropID, WeekDate, Available
1 , 2015-07-04, Yes
1 , 2015-07-11, Yes
1 , 2015-07-18, No
2 , 2015-07-04, Yes
2 , 2015-07-11, No
2 , 2015-07-18, No
I want to select properties where both the weeks of the 4th and 11th are available. In the example above, I want to return two rows with PropID 1 as both are available and no rows from PropID 2 as only one of the weeks are available.
I've tried various ways, but either get nothing or always return the 1st, 2nd and 4th rows.
I think this is close, but it's still missing something as it is looking for dates that are <= AND >=
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where(function($sub)
{
$sub->where('WeekDate', '>=', '2015-07-04');
$sub->where('WeekDate', '<=', '2015-07-11');
});
$q->where('Available', '=', 'Yes');
})
->get();
Not sure this helps, but the Property table is simply
PropID, PropName
1 , Property 1
2 , Property 2
Just found that this SQL works.
SELECT PropID FROM tblweeks WHERE WeekDate IN ('2015-07-04', '2015-07-11') AND Available = 'yes' GROUP BY PropID HAVING COUNT(*) = 2
This will give your result as Property 1 only:
$weeks = Property::whereHas('Week', function ($q) {
$q->where(function ($sub) {
$sub->whereIn('WeekDate', array('2015-07-04', '2015-07-11'));
$sub->where('Available', '=', 'y');
});
$q->groupBy('property_id');
$q->having('count(*)', '=', '2');
})->get();
There are some changes i have did in your query :
Please check this solution
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '=', '2015-07-04');
$q->orWhere('WeekDate', '=', '2015-07-11');
$q->where('Available', '=', 'Yes');
})
->get();
This should work and will return desire output
There are some changes, the query might be using group by query :
Please check this solution
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '=', '2015-07-04');
$q->orWhere('WeekDate', '=', '2015-07-11');
$q->where('Available', '=', 'Yes');
$q->group('WeekDate');
})
->get();
This should work and will return desire output
You actually need two whereHas for this:
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '>=', '2015-07-04');
$q->where('Available', '=', 'Yes');
})
->whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '<=', '2015-07-11');
$q->where('Available', '=', 'Yes');
})
->get();
I believe you do not need the second nested query.
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '>=', '2015-07-04');
$q->where('WeekDate', '<=', '2015-07-11');
$q->where('Available', '=', 'Yes');
})
->get();
Updated
Have you looked into whereBetween.
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->whereBetween('WeekDate', '2015-07-04', '2015-07-11');
$q->where('Available', '=', 'Yes');
})
->get();

Categories