I'm trying to run the following queries and running into this error:
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
When I remove the 'whereNotIn' part the query works. I know for a fact that the first query works, as I tested that individually. How can I fix this error? Here's the code:
$alreadyCheckedOutDevicesQuery = DB::connection('NEWSTAFFPORTAL')->table('DeviceCheckout_checkout')->select('deviceID')->where('inBy', '=', '')->get();
$alreadyCheckedOutDevices = GlobalModel::convertDBObjectsToArray($alreadyCheckedOutDevicesQuery);
$deviceTableInformation = DB::connection('NEWSTAFFPORTAL')->table('DeviceCheckout_deviceListTestingTable')->select('deviceID', 'name', 'type', 'brand', 'model')->whereNotIn('deviceID', $alreadyCheckedOutDevices)->orderBy('name', 'ASC')->get();
Try doing it in a subquery:
$info = DB::connection('NEWSTAFFPORTAL')
->table('DeviceCheckout_deviceListTestingTable')
->select('deviceID', 'name', 'type', 'brand', 'model')
->orderBy('name', 'asc')
->whereNotIn('deviceID', function ($query)
{
$query->from('DeviceCheckout_checkout')
->select('deviceID')
->where('inBy', '');
})
->get();
This will work:
$alreadyCheckedOutDevices = DB::connection('NEWSTAFFPORTAL')
->table('DeviceCheckout_checkout')
->where('inBy', '=', '')
->lists('deviceID');
$deviceTableInformation = DB::connection('NEWSTAFFPORTAL')
->table('DeviceCheckout_deviceListTestingTable')
->select('deviceID', 'name', 'type', 'brand', 'model')
->whereNotIn('deviceID', $alreadyCheckedOutDevices)
->orderBy('name', 'ASC')
->get();
Also it should be better in terms of performance, than using subquery.
Simplified explain:
+----+--------------------+-----------------+---------+------+-------------+
| id | select_type | type | key | rows | Extra |
+----+--------------------+-----------------+---------+------+-------------+
| 1 | PRIMARY | ALL | NULL | 100 | Using where |
| 2 | DEPENDENT SUBQUERY | unique_subquery | PRIMARY | 1 | Using index |
+----+--------------------+-----------------+---------+------+-------------+
+----+-------------+------+------+------+-------------+
| id | select_type | type | key | rows | Extra |
+----+-------------+------+------+------+-------------+
| 1 | SIMPLE | ALL | NULL | 100 | Using where |
+----+-------------+------+------+------+-------------+
Related
There is a table with the following structure: (primary keys: user_id, record_id)
+---------+-----------+-------+
| user_id | record_id | value |
+---------+-----------+-------+
| 1 | 1 | 100 |
| 1 | 2 | 200 |
| 2 | 1 | 300 |
| 2 | 2 | 400 |
+---------+-----------+-------+
When i change value parameter over eloquent-query in my Controller like this:
$playerRecord = Test::where('user_id', '=', $player_id)->where('record_id', '=', '1')->first();
$playerRecord->value = $user_value1;
$playerRecord->save();
I have an error: https://flareapp.io/share/47qg8ZEm#F49
If I define only one primary key in the model, all records with this key are updated, despite the fact that I forced to update a specific record.
$playerRecord = Test::where('user_id', '=', $player_id)->where('record_id', '=', '1')->update(['value'=>$user_value1]);
if($playerRecord){
return "data updated";
}else{
return "data not found";
}
My code not working together, but if i only use one of them->where('numbers.client_group_id', NULL) OR ->whereNotIn('id', function($query) use ($request){ itu work normal.
$model = DB::table('numbers')->select('id','phone');
$model
->where('numbers.client_group_id', NULL)
->whereNotIn('id', function($query) use ($request){
$query->select('number_push_logs.number_id')->from('number_push_logs')->where('number_push_logs.client_group_id',$request->client_group);
})
->orderBy('quality', 'asc')->limit($request->total_push);
this my table looks like:
numbers table
| id| phone | client_group_id|
|---|------------|----------------|
| 1 |0852176255**| 1 |
| 2 |0852176255**| 1 |
| 3 |0852176255**| NULL |
number_push_logs table
| id| number_id |client_group_id |
|---|------------|----------------|
| 1 |1 | 1 |
| 2 |1 | 2 |
| 3 |2 | 2 |
so i want to select numbers data where client_group_id = NULL and id numbers not in number_push_log table with specific client_group_id
Try something like this. Use join. It performs better than nested demand.
\DB::table('numbers as n')
->select('id','phone')
->leftJoin('number_push_logs as npl', 'n.client_group_id', '=', 'npl.client_group_id')
->whereNull('npl.id')
->orderBy('quality', 'asc')
->limit($request->total_push);
I have got the following query, which returns 44 rows:
SELECT id, IF(r.make, CONCAT(makes.description, ': ', r.make), 0) AS make, IF(r.model, models.description, 0) AS model, r.text, r.h1_tag, r.title, r.keywords, r.description, r.website_search_path_id, r.website_vehicle_type_id
FROM website_results_text r
LEFT JOIN vehicle_makes makes ON makes.code = r.make
LEFT JOIN vehicle_models models ON models.code = r.model AND models.make = r.make
WHERE r.website_id = 1966
The results are similar to as follows, not included all rows and excluded columns that don't really matter.
---------------------------------------------------------------------
| id | make | model | text | h1_tag | title |
_____________________________________________________________________
| 192| 0 | 0 | test | test | test |
| 193| Fiat:24 | 0 | test | test | test |
---------------------------------------------------------------------
Below is the query that I have got so far, as you can see i'm missing the IF statements for if there is no make returned from the vehicle_makes table.
$resultsText = ResultsText::where([ 'website_id' => $website->id ])
->join('vehicle_makes', 'website_results_text.make', '=', 'vehicle_makes.code')
->join('vehicle_models', 'website_results_text.model', '=', 'vehicle_models.code')
->select(
'vehicle_makes.description AS make',
'vehicle_models.description AS model',
'website_results_text.text',
'website_results_text.h1_tag',
'website_results_text.title',
'website_results_text.keywords',
'website_results_text.website_search_path_id',
'website_results_text.website_vehicle_type_id'
)->get();
I have a table entries similar as follows:
+---------+---------+----------+
| Test_id | User_id | Attempts |
+---------+---------+----------+
| 12 | 5 | 1 |
| 13 | 5 | 1 |
| 12 | 5 | 2 |
+---------+---------+----------+
Now I want to select the elements group by test_id and should get the latest entry.
I tried this query:
$tests_took = Testresult::where('course_id', $courseId)
->where('user_id', Auth::id())
->groupby('test_id')
->orderBy('attempts', 'desc')
->get();
When I display the result, I'm getting the first two rows only (only one row for one test_id - which I what I want.) But instead of the last row for the test_id=12, it shows the first row. I always want the biggest attempt to be displayed.
My current output is like:
| 12 | 5 | 1 |
| 13 | 5 | 1 |
But I want this:
| 12 | 5 | 2 |
| 13 | 5 | 1 |
How can I achieve this? to get the latest row as the first array element when I use groupby or is there any other way to do this?
ORDER BY and GROUP BY don't work very well together...
If you simply want the highest attempt per test_id i suggest using the MAX() function:
$tests_took = Testresult::select('test_id', 'user_id', DB::raw('MAX(attempts) AS max_attempts'))
->where('course_id', $courseId)
->where('user_id', Auth::id())
->groupby('test_id')
->get();
You may use the following lines:
Testresult::select('*')
->join(DB::raw('(Select max(id) as last_id from Testresult group by test_id) LatestId'), function($join) {
$join->on('Testresult.id', '=', 'LatestId.last_id');
})
->orderBy('attempts', 'desc');
}
So I'm trying to get a Laravel Eloquent query to work. Here's the scenario:
1) I have one table called c5_wall_route_taxonomies That looks like the following:
---------------------------------------------
| ID | TYPE | CREATED_ON |
---------------------------------------------
| 2 | 'country' | 2014-08-12 12:12:12 |
---------------------------------------------
Where ID is an auto-incrementing integer, TYPE is an enum, and CREATED_ON is a pretty standard timestamp.
2) I have another table called c5_wall_route_taxonomy_options that looks like the following:
-----------------------------------------------------------------
| ID | WALL_ROUTE_TAXONOMY_ID | META_KEY | META_VALUE |
-----------------------------------------------------------------
| 1 | 2 | 'label' | 'Malaysia' |
| 2 | 2 | 'code' | 'MY' |
-----------------------------------------------------------------
And I'm trying to get the fields from the c5_wall_route_taxonomy table along with an extra 'country_label' field.
$result = DB::table('wall_route_taxonomies')
->join('wall_route_taxonomy_options as country', function($join) {
$join->on('wall_route_taxonomies.id', '=', 'country.wall_route_taxonomy_id')
->where('country.meta_key', '=', 'label');
})->get();
Note that my table prefix is c5_.
Laravel tells me that it's unable to find c5_country.wall_route_taxonomy_id.
Now in this scenario, I could always just opt not to use the alias, and just use wall_route_taxonomy_options.wall_route_taxonomy_id instead, but what if I wanted to get both the label and the code from the options table as individual fields in my output?
Surely there's a way to implement this in Laravel?