Happy Holidays!
I'm stuck! I dabble here and there but am not extremely proficient so some help would be appreciated.
I have an incident record in the MySQL database which contains the field incident_uid. This is a comma-separated string which contains user IDs. This is then exploded across the application to form an array to look up user names when multiple users are associated with the incident.
I also have a scheme_vehicle_log record which contains a responders column - again a comma-separated string of user IDs.
I am trying to run the following but it isn't functioning as intended.
$incident_responders = explode(',', $incident->incident_uid);
$schemevehicles = SchemeVehicleLog::whereIn('responders', $incident_responders)
->whereDate('signout_date', '<=', $incident->incident_date)
->where(function ($q) use ($incident) {
$q->where('return_date', '>=', $incident->incident_date)
->orWhere('return_date', null);
})
->get();
It only either returns some rows or none at all. A dd($incident_responders) confirms the array as intended.
array:2 [▼ // app/Http/Controllers/IncidentController.php:63
0 => "97"
1 => "17"
]
However dd($schemevehicles) shows as empty.
If I replace $incident_responders within the $schemevehicles query with [97,17], I get the intended result.
Tried for the past hour and reckon this will be something very simple - what am I overlooking here?
Thanks both.
I managed to find the problem.
dd($incident_responders) was returning the data as type string and not integer and therefore the query was failing ("97" and rather 97).
Adding an array_map helped.
Thanks.
Related
I am using jessengers/laravel-mongodb in one of my project. I would like to use raw mongodb query rather than the other functions in jessengers library. when i write a simple query using raw() to get two field values from database, it returns the complete document. So i tried with where() function, the cursor shows the requested fields only.
I am afraid why the same query shows two different results...
Here is the query with where() function:
$articleDataArray = Article::where('art_xml_data.article.article_id', '=', $articleId)
->get(['art_status', 'art_file_path']);
This query returns a cursor having art_status and art_file_path values only. (As per Neil Lunn's advice I have edited the question to include the result here.)
Result:
But the below query when written in raw() returns the complete fields in the matched document.
$articleDataArray = Article::raw()->find(
array ('art_xml_data.article.article_id' => $articleId),
array ('art_status' => 1, 'art_file_path' => 1)
)->toArray();
Result:
I'm a system for a friend, but stumbled across a little problem.
I've got 3 models (customerCard, customerCardComment and customerCardFollowup).
It's better if you see the image below to understand what I'm trying to achieve. As you see I'm trying to get a result where I get a customerCard model where the sold field is equal to 0 and the customerCard model has no customerCardComment model where the type field is equal to 1,2 or 3, and the customerCard also does not have any customerCardFollowup.
Databases:
customer_cards
customer_card_comments
customer_card_followups
Both comments and followups table are related to the ID field in customer_cards. Both have foreign keys customer_card_id.
Is there a way to do this query? Or am i lost?
Thanks in advance.
Laravel's whereHas accepts a 3rd and 4th parameter. The 3rd parameter is the comparison operator against the count of the result set, and the 4th argument is the number to compare against. This will solve the first problem:
CustomerCard::where(function($query){
$query->where('sold', 0)
->whereHas('customerCardComment', function($query){
return $query->whereIn('type', [1,2,3]);
}, '=', 0);
});
Next, you can use ->has() to count the number of records returned from a relation, this will solve your second problem:
CustomerCard::where(function($query){
$query->where('sold', 0)
->whereHas('customerCardComment', function($query){
return $query->whereIn('type', [1,2,3]);
}, '=', 0)
->has('customerCardFollowup', '=', 0);
});
The 3rd one is actually a bit more complex and I need to think about how to approach that a bit more. I'll answer now to get you going in the right direction and edit and update the post with the solution to the 3rd problem shortly.
I am trying to access a SINGLE VALUE from a row but the dd(); is getting the whole row and showing it in the Collection array. The code:
$last_id = \App\Cat::limit(1)->orderBy('cat_id','desc')->get(['cat_id']);
dd($last_id);
So when I need the JUST "55" value I get :
"cat_id" => "55"
Same happens with others columns, when I need JUST the "Eletronics" I get:
"cat_name" => "Eletronics"
I have already tried lots of stuff like Limit, List, First and nothing happens, when I try to call something like dd($lastId->cat_id ); it gives me a "Undefined property" error. SO now I am really out of options since I am using the documentation example and even that way it does not works fine. So any help would be great, thank you.
I think you don't understand that the Model represents entire row of table.
That means that $last_id in your code:
$last_id = \App\Cat::limit(1)->orderBy('cat_id','desc')->get(['cat_id']);
represents entire row of cats table. If you want to get id, you have to do this:
$cat = \App\Cat::first(1)->orderBy('cat_id','desc')->get();
dd($cat->id);
What more, if you write
$cat = \App\Cat::first();
you will get first cat from cats table, and then you can access every column of this row as property of $cat object
If Laravel says you that property cat_id is undefined, propably your table don't contain cat_id column.
Already got it, i change the get for the "value" method.
\App\Cat::orderBy("id","desc")->value("id");
i have this table called dbo_modulesand it has columns ModuleCountLeft and ModuleCriticalLevel
in my controller, im trying to get the number of modules where ModuleCountLeft is less than the value of ModuleCriticalLevel. in my table, the values of ModuleCountLeft and ModuleCriticalLevel differs from row to row. so in my controller i uses this query:
$critical = DB::table('dbo_modules')
->where('ModuleCountLeft' , '<=' , DB::table('dbo_modules')->pluck('ModuleCriticalLevel') )
->count();
the problem here is that im not getting the correct values. It get the first ModuleCriticalLevel and makes it the point of comparison. for example the first ModuleCriticalLevel in the table is 20, it compares all the ModuleCountLeft to 20. Can anyone tell me what im doing wrong? or is my code wrong? please. Thanks in advance.
Try like following. You need not to pluck anything
$critical = DB::table('dbo_modules')
->where('ModuleCountLeft' ,'<=' ,'ModuleCriticalLevel')
->count();
Hope anybody can help me, I need to search for items that have category id = x in the database
Example table items
id,cats,name etc...
cats = '1,19' or maybe just '19' or maybe '1,9'
So for this example I need a to search for items that have cats with 9
I tried this but when I search for 9 it also shows 19
$items = Items::where(function($query)use($cat) {
$query->where('cats', 'like', '%,'.$cat->id.'%');
$query->orWhere('cats', 'like', '%'.$cat->id.',%');
$query->orWhere('cats', 'like', '%'.$cat->id.'%');
})->orderBy('updated_at', 'DSC')->get();
I also tried something
$items = Items::whereIn(explode(',', 'cats'), $cat->id)->get();
but it doesn't work
Appreciate any help to find the easiest and shorts way of doing this, regards
It's quite hard to understand what you want to achieve but I'll try. First of all as #particus mentioned the best way is to create pivot table when you don't need to worry about such things.
But the solution if you have list of ids in a columns separated by coma is not storing values like
1,2,3
but always adding , at the beginning and at the end, so it should be in this case:
,1,2,3,
This way, if you have in your table ,19,2,3, and you want to search for value 9, you should use look for ,9, string, for example:
$id = 9;
$items = Items::where('column', LIKE '%,'.$id.',%')->get();
Now for above string no record will be found, but if you have ,9,2,3, or just ,9, the desired record will be found.
Assuming you're using MySQL, you can use the FIND_IN_SET function.
$items = Items::whereRaw("FIND_IN_SET(".$cat->id.", cats)")->orderBy('updated_at', 'DESC')->get();
Please note, this will not use any indexes defined on the cats column. Storing array like data in a field is usually a big red flag. You would benefit by normalizing this out now, rather than trying to work around the current design.