How can I build a condition based query in Laravel?
In Fluent you can do:
$query = DB::table('node');
if ($published == true)
$query->where('published', '=', 1);
if (isset($year))
$query->where('year', '>', $year);
$result = $query->get();
How can I build a condition based query in Laravel using eloquent
Build up the query and include the ->where()
clause depending on whether or not you have the location in your input:
$query = User::where('role', 'user');
$query = \Input::has('location') ? $query->where('location', \Input::get('location')) : $query;
$availableUsers = $query->take($count)->orderByRaw('RAND()')->get();
Create conditional query in laravel
In where callback function you should pass query instance and for additional params you can pass it in use()
$product = DB::table('brands')->join('products', 'brands.id', '=', 'products.brandid')
->where('categoryid', '=', 1)
->where(function ($query)use($search) {
if(!empty($search)){
$query->where('productname','like','%'.$search.'%')
->orWhere('brands.name','like','%'.$search.'%');
}
});
Laravel Query Builder: where condition based on user input
Try this:-
$Select_db = DB::table('mytable');
if (Input::get('name') != "")
$Select_db->where('name', Input::get('name'));
if (Input::get('place') != "")
$Select_db->where('place', Input::get('place'));
if (Input::get('color') != "")
$Select_db->where('color', Input::get('color'));
$result = $Select_db->get();
And if there are multiple columns to match, then try using this:-$Select_db = DB::table('mytable');
foreach($_POST as $key => $val){
if(Input::get($key) != ""){
$Select_db->where($key, Input::get($key));
}
}
$Select_db->get();
How can I build a condition based query in Laravel 6.x?
You can use when
as a conditional clause:
public function test (Request $request)
{
$apartments = DB::table('apartments')
->when($request->filled('bathrooms'), function ($query) use ($request) {
return $query->where('bathrooms', '=', $request->input('bathrooms'));
})
->when($request->filled('rooms_number'), function ($query) use ($request) {
return $query->where('rooms_number', '=', $request->input('rooms_number'));
})
->get();
return view('pages.test2' , compact('apartments'));
}
How to make laravel query with search condition and filter condition from relational table
You have several OR
in your where clauses so adding the last part makes it something like:
WHERE … OR … OR (… AND …)
What you need is to group your orWheres
:$cases->where(function ($query) {
$query
->where('number', 'like', '%'.$this->search.'%')
->orWhere('title', 'like', '%'.$this->search.'%')
->orWhereHas('type', function ($query) use ($key) {
$query->where('name', 'like', $key.'%');
});
});
More here: https://laravel.com/docs/9.x/queries#or-where-clauses How to write if elseif else condition laravel query builder?
There's no else
counterpart to when()
when using multiple conditions (if/elseif/else
), but if you think of it, it's just the inverse of the sum of the other conditions. To exactly mirror a PHP if/elseif/else
, you need to check the first condition, and then explicitly check the second condition and not the first condition, and to mimic the else
condition, its just whichever boolean condition is needed when all else conditions fails.
$query = DB::table('table')
->select('*')
->when($count < 3, function ($q) {
// if count < 3
})
->when($count > 7, function ($q) {
// else if count > 7
})
->when($count =< 7 && $count >= 3, function($q) {
// Else, count between 3 and 7 (inclusive)
});
You can conditionally apply a where clause "manually" by using native PHP if/elseif/else
controls and apply a simple where()
, which might be more explicit if your conditions become very expressive or complex.There is however an else
condition when you have a simple if/else
, as you can provide it another closure. This cannot be used with multiple conditions, as you've originally asked about, but I included it for reference.
$query = DB::table('table')
->select('*')
->when($count < 3, function ($q) {
// if count < 3
}, function($q) {
// Else, count greater or equal to 3
});
How to build query for multiple condition for same column in laravel query builder?
You are not combining your "where" clauses properly. With the "orWhere" at the end, it essentially ignores all the conditions (ie. which user it relates to) of the preceding where clauses, so adds to the results any row where plan_validity is null. So after you query by user ID and status, you then need to combine the remainder of the query into a logical group like so :
$user = Users::where('user_id', $card_details->user_id)
->where('status', 1)
->where(function (Builder $query) {
return $query
->whereDate('plan_validity', '>=', Carbon::now())
->orWhere('plan_validity', null);
})
->get();
Is there any equivalent of SQL IN (...) query in Laravel for multiple if condition?
You could use PHPs in array function https://www.php.net/manual/en/function.in-array.php
$arr = [1, 2, 3, 4];
if (in_array($type, $arr) {
}
If you have multiple groups of values to compare to, use switch()switch($type) {
case 1:
case 2:
case 3:
case 4:
echo 'group 1';
break;
case 6:
case 7:
case 8:
echo 'group 2';
break;
default:
echo 'no group found';
break;
}
Related Topics
Why Should I Never Run 'Composer Update' in Production
Using Moodle Create Users and Enroll Them in Courses via SQL
Having Trouble Writing to a File with PHP on Ubuntu
Create an Assoc Array with Equal Keys and Values from a Regular Array
Mail Not Sending with PHPmailer Over Ssl Using Smtp
Laravel 4.2 Says My Application Is in Production. How to Turn This Off
How to Wrap Text Using Imagick in PHP So That It Is Drawn as Multiline Text
Efficient Reloading Data/Pushing Data from Server to Client
Determining If a File Exists in Laravel 5
Programming Multi-Language PHP Applications
How to Auto-Resize a Div with CSS While Keeping Aspect Ratio
Shortcodes Inside a Shortcode - Wordpress
Possible to View PHP Code of a Website
Selenium2 Firefox: Use the Default Profile