This query does not work correctly, it only shows 1 row
$data = Post::select('id', 'name')
->whereIn('id', [$order])
->orderByRaw(\DB::raw("FIELD(id, $order)"))
->get();
but this works fine, it shows all rows
$data = Post::select('id', 'name')
->whereIn('id', [1,2,3])
->orderByRaw(\DB::raw("FIELD(id, $order)"))
->get();
Thank you!
Your Query is Here:-
$data = Post::select('id', 'name')
->whereIn('id', $order)
->orderByRaw(\DB::raw("FIELD(id, ".implode(",",$order).")"))
->get();
Remove [] from $order.
For WhereIn condition second parameter should be an array. So the $order should be
$order = [1,2,3,4]
If your $order is an array, i think that you should do this
whereIn('id', $order) instead of whereIn('id', [$order])
P.S. In official documentation mentioned that second argument should be an array:
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
Related
lets say I have a collection of users Users::all()
I would like to take sort/order it like such Users::all()->sort('created_at', 'DESC')
then I would like to sub order it by an array like [1,5,3,9,4,8] so perhpas a call like this Users::all()->sort('created_at', 'DESC')->sortBy("id", [1,5,3,9,4,8])
Any Advice?
Edit 1
I have found this, is this correct to use?
$ids = collect([1,5,3,9,4,8]);
$users = Users::all()->sort('created_at', 'DESC');
$users = $ids->map(function($id) use($users) {
return $users->where('cat_id', $id)->first();
});
I think you could just invoke orderBy() twice.
$ids = [1,5,3,9,4,8];
$users = Users::all()
->orderBy('created_at', 'desc')
->orderBy($ids)
->get();
Does this answer your question?
You can use whereIn like this probably:
$ids = [1,5,3,9,4,8];
$users = Users::all()
->orderBy('created_at', 'desc')
->whereIn('cat_id', $ids)
->get();
https://laravel.com/docs/9.x/queries#additional-where-clauses
The whereIn method verifies that a given column's value is contained within the given array
So I found a solution.
$ids = json_decode($interview->question_ids ?? '[]');
if(count($ids) == 0){ // if empty create new id array and save to DB
$ids = collect(questions::all()->where('interview_id', $interview->id)->pluck('id')->toArray());
$interview->question_ids = json_encode($ids);
$interview->save();
}
$questions = questions::all()->where('interview_id', $interview->id)->sortBy([
['order_timestamp', 'asc'],
['created_at', 'asc'],
]);
$questions = $ids->map(function($id) use($questions) {
return $questions->where('id', $id)->first();
});
$questions = $questions->flatten();
I want to get the template from user_webhook table in my database.In WHERE condition i am checking user_id,app_id and if either notify_admin or notify_customer value is 1 in user_webhook table.I am using query..
$templates= $this->where('notify_admin',1)
->orwhere('notify_customer',1)
->where('user_webhooks.user_id',$user_id)
->where('user_webhooks.app_id',$app_id)
->select( 'webhooks.id as webhook_id','webhooks.app_id','webhooks.topic','webhooks.type','webhooks.action',
'webhooks.sms_template','user_webhooks.id','user_webhooks.notify_admin',
'user_webhooks.notify_customer','user_webhooks.user_id','user_webhooks.sms_template_status',
'user_webhooks.sms_template as sms'
)
->join ('webhooks',function($join){
$join>on('webhooks.id','=','user_webhooks.webhook_id');
})
->get()
->toArray();
when i get query using DB::getQueryLog(), I found the query seems Like
select `telhok_webhooks`.`id` as `webhook_id`, `telhok_webhooks`.`app_id`,
`telhok_webhooks`.`topic`, `telhok_webhooks`.`type`, `telhok_webhooks`.`action`,
`telhok_webhooks`.`sms_template`, `telhok_user_webhooks`.`id`,
`telhok_user_webhooks`.`notify_admin`, `telhok_user_webhooks`.`notify_customer`,
`telhok_user_webhooks`.`user_id`, `telhok_user_webhooks`.`sms_template_status`,
`telhok_user_webhooks`.`sms_template` as `sms` from `telhok_user_webhooks`
inner join
`telhok_webhooks` on `telhok_webhooks`.`id` = `telhok_user_webhooks`.`webhook_id`
where `notify_admin` = ? or `notify_customer` = ? and `telhok_user_webhooks`.`user_id`
= ? and `telhok_user_webhooks`.`app_id` = ?
The result of query giving result of all app_id and user_id.
So Please tell me use of OR in where condition.
Thanks in advance.
You may chain where constraints together as well as add or clauses to the query. The orWhere method accepts the same arguments as the where method:
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')
->get();
Advanced usage:
Usere::where('id', 46)
->where('id', 2)
->where(function($q) {
$q->where('Cab', 2)
->orWhere('Cab', 4);
})
->get();
The whereIn method verifies that a given column's value is contained within the given array:
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
More: https://laravel.com/docs/5.5/queries
Change
->where('notify_admin',1)
->orwhere('notify_customer',1)
to
->where(function($q){
$q->where('notify_admin',1)
->orWhere('notify_customer',1);
})
Without this, the orWhere will compare to all other wheres in your query instead of just comparing those two columns
I am new to laravel, and i try to orderBy the rand, is it possible to order by in each column ? my code just take row rand but it dont shuffle the column just the row.
public function show(){
$name = DB::table('names')
->inRandomOrder()
->get();
return view('content', ['names' => $names]);
}
orderBy in Laravel.
The orderBy method allows you to sort the result of the query by a given column. The first argument to the orderBy method should be the column you wish to sort by, while the second argument controls the direction of the sort and may be either asc or desc:
$users = DB::table('users')
->orderBy('name', 'desc')
->get();
In your case this will work.
public function show(){
$names = DB::table('names')
->orderBy('name', 'desc')
->orderBy('city', 'asc')
->get();
return view('content', ['names' => $names]);
You Can Do Like This For To Order by in each column
public function show(){
$names = DB::table('names')
->orderBy('name', 'DESC')
->orderBy('city', 'DESC')
->orderBy('email', 'ASC')
->get();
return view('content', ['names' => $names]);
}
With foreach
$names = DB::table('names');
foreach ($request->get('order_by_columns') as $column => $direct) {
$names->orderBy($column, $direct);
}
$results = $names->get();
I am getting ids using jquery in my controller its as
if(!empty($request->input('ids'))) {
$ids = $request->input('ids');
}else{
$ids = null;
}
I try dd($ids)
Output is as in my console
array:3 [
0 => "1"
1 => "2"
2 => "on"
]
When i pass ids to my query as
Query
$student_ids = DB::table('students')
->orderBy('name', 'asc')
->where('group_id', '<>', 0)
->whereIn('students.id','=', $ids)
->pluck('id');
Its not working
Error is : Invalid argument supplied for foreach
Please help where I am wrong. Thanks
You should change this:
->whereIn('students.id','=', $ids)
To this:
->whereIn('students.id', $ids)
Because the second parameter should be iterable of IDs, but you're passing a string =. Also, make sure, you're passing an array or collection of IDs.
Change:
where('students.id', '=', $ids)
To:
whereIn('students.id', $ids);
Like others have mentioned update the whereIn parameter. Also simplify your request input like so.
$ids = $request->input('ids', []);
$student_ids = DB::table('students')
->orderBy('name', 'asc')
->where('group_id', '<>', 0)
->whereIn('students.id', $ids)
->pluck('id');
The title says it all.
I get that I can do this :
DB::table('items')->where('something', 'value')->get()
But what I want to check the where condition for multiple values like so:
DB::table('items')->where('something', 'array_of_value')->get()
Is there an easy way of doing this?
There's whereIn():
$items = DB::table('items')->whereIn('id', [1, 2, 3])->get();
You could use one of the below solutions:
$items = Item::whereIn('id', [1,2,..])->get();
or:
$items = DB::table('items')->whereIn('id',[1,2,..])->get();
If you need by several params:
$ids = [1,2,3,4];
$not_ids = [5,6,7,8];
DB::table('table')->whereIn('id', $ids)
->whereNotIn('id', $not_ids)
->where('status', 1)
->get();
You can use whereIn which accepts an array as second paramter.
DB:table('table')
->whereIn('column', [value, value, value])
->get()
You can chain where multiple times.
DB:table('table')->where('column', 'operator', 'value')
->where('column', 'operator', 'value')
->where('column', 'operator', 'value')
->get();
This will use AND operator. if you need OR you can use orWhere method.
For advanced where statements
DB::table('table')
->where('column', 'operator', 'value')
->orWhere(function($query)
{
$query->where('column', 'operator', 'value')
->where('column', 'operator', 'value');
})
->get();
If you are searching by ID you can also use:
$items = Item::find(array_of_ids);
It's work for me
fetch just id then search in array
$shops = Shop::Where('user_id', Auth::id())->get('id');
$categories = Category::whereIn('shop_id',$shops)->paginate(7);
$whereData = [['name', 'test'], ['id', '<>', '5']];
$users = DB::table('users')->where($whereData)->get();
WHERE AND SELECT Condition In Array Format Laravel
use DB;
$conditions = array(
array('email', '=', 'user#gmail.com')
);
$selected = array('id','name','email','mobile','created');
$result = DB::table('users')->select($selected)->where($conditions)->get();