Laravel Querybuilder How to Use Like in Wherein Function

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



Leave a reply



Submit