laravel querybuilder how to use like in wherein function
Thanks everyone for helping me but i solved it by doing:
$book = array('book2','book3','book5');
$name = DB::Table('bookinfo')
->select('BookName', 'bookId')
->Where(function ($query) use($book) {
for ($i = 0; $i < count($book); $i++){
$query->orwhere('bookname', 'like', '%' . $book[$i] .'%');
}
})->get();
Using whereIn and where in Laravel eloquent query builder
You can try this
$laptops = Laptop::whereIn('specsTag',$requestArray)->where(function($query) use ($requestArray){
foreach($requestArray as $item){
$query->orWhere('screen_diameter','=',$item);
}
})->get();
Assuming you have $requestArray
like this
$requestArray = ['HP','Apple' '12 inch', 'Intel Core M'];
You will get sql like this
select * from `laptops` where `specsTag` in (?, ?, ?, ?) and (`screen_diameter` = ? or `screen_diameter` = ? or `screen_diameter` = ? or `screen_diameter` = ?)
How to use array with LIKE with whereIn to retrieve eloquent rows in Laravel
You can simply use orWhere
method for every keyword and it will equivalent for whereIn
. The code will look like:
$result = \App\Table::where(function($query){
$query->orWhere('content', 'LIKE', '%google%')
->orWhere('content', 'LIKE', '%youlense%')
-> and so on;
});
$result = \App\Table::where(function($query) use($keywords){
foreach($keywords as $keyword) {
$query->orWhere('content', 'LIKE', "%$keywords%")
}
});
Note: gotten query can work very slowly.
Use whereIn with values from a seperate query in Laravel
$IDs = Cart::select('item_id')->where('user_id', auth()->id());
this line return a query builder not a collection of ids ...
anyway you can get the ids in array and use it directly for whereIn :
$IDs = Cart::where('user_id', auth()->id())->pluck('item_id')->all();
$items = Item::whereIn('id',$IDs)->get();
Use a complex whereIn with Laravel's query builder
I'm stupid I forgot the get();
at the end of my query:
$db = Capsule::table('tblinvoices')->select('id')->where('userid', 9554)->where('status', 'Unpaid')->whereIn('id', function($query)
{
$query->select('invoiceid')->from('tblinvoiceitems')->where('type', 'Addon')->where('relid', 1479)->where('userid', 9554);
})->get();
Select from select using Laravel eloquent
You're trying to achieve something like this:
In the oficial documentation you can find the explanation of Advanced where clauses
DB::table('products')
->whereIn('id', function($query)
{
$query->select('id')
->from('favourites')
->where('favourites.user_id', Auth::id());
})
->get()
The final result will be (you can see this dumping the query using toSql() method instead of get()) :
select * from `products` where `id` in (select `id` from `favourites` where `favourites`.`user_id` = ?)
Laravel 9 - implode() and whereIn
commonly whereIn
method accept array
as a second parameter. but you are passing string. I think this should solve your problem.
$edi_data_mysql = DB::table('orders_grid')
->select('id')
->where('stato_ordine', 1)
->pluck('id')
->toArray();
dump($edi_data_mysql); // returns **[2, 3, 5]**
$jde_data = DB::connection('oracle')->table('table')
->select('col1', 'col2', 'col3', 'col4')
->where('col5', DB::raw("TRIM('Y')"))
->whereIn('oracle_id', $edi_data_mysql)
->get();
By this way you can avoid loop. No matter it is string or integer, it should work for both.
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
Related Topics
How to Measure the Strength of a Password
How to Find Annotations in a PHP5 Object
Ios7 - Receipts Not Validating at Sandbox - Error 21002 (Java.Lang.Illegalargumentexception)
PHP Switch Case More Than One Value in the Case
How to Replace the Deprecated Set_Magic_Quotes_Runtime in PHP
Answering Http_If_Modified_Since and Http_If_None_Match in PHP
How to Get PHP, Symlinks and _File_ to Work Together Nicely
How to Include Config.PHP Efficiently
Get Only Filename from Url in PHP Without Any Variable Values Which Exist in the Url
How to Reset MySQL Pointer Back to the First Row in PHP
Laravel Many to Many Self Referencing Table Only Works One Way
Get a Coupon Code via Url and Apply It in Woocommerce Checkout Page
Using Prepared Statement, How I Return the Id of the Inserted Row
Partially Hide Email Address in PHP
PHP Get How Many Days and Hours Left from a Date
What's the Best Way to Get the Fractional Part of a Float in PHP