In Kohana if I want to select fields 'id', and 'username' I do it this way:
$record = DB::select('id', 'username')
->from('users')
->where('id', '=', 1)
->as_object()
->execute();
But how to do it if the list of fields I need to select is in an array, like this:
$fields = array('id', 'username');
How to use DB::select in this situation?
You are looking for DB::select_array().
You can also use $record = call_user_func_array('DB::select', $fields) and then continue building the query; it might be ugly but it also works :)
Related
$user= Select from user where 'email' = $email AND 'specialty' =null OR 'address' = null OR 'country' =null OR 'state' = null;
This is the code i have but its not working properly. What i want is to return the row if any of the stated columns has its value as null.
$doctor= Doctor::where('email',$email)->where(function ($query) {
$query->orWhere('state','=','')
->orWhere('country','=','')
->orWhere('address','=','')
->orWhere('specialty','=','');
})->get();
First things first - in SQL to filter by NULL one should be using where x IS NOT NULL.
For the laravel part, Eloquent has a whereNull method, see more: https://laravel.com/docs/5.6/queries
So your eloquent code should look somehing like:
$doctor= Doctor::where('email',$email)->where(function ($query) {
$query->orWhereNull('state')
->orWhereNull('country')
->orWhereNull('address')
->orWhereNull('specialty');
})->get();
I'm try to make a tag-based search system similar to the one here on stackoverflow using Laravel and jQuery Token-Input plugin. The contents of the "tags" are then to be used as the query itself, drawn from a list from a different table.
Using Eloquent, I want to build a query based on a variable number of tags in the search bar (limited only by the number of possible tags there can be). It would look something like this if not done as a loop:
$query = Model::whereHas('attribute', 'name', '=', 'tag1')
->whereHas('attribute', 'name', '=', 'tag2')
->whereHas('attribute', 'name', '=', 'tag3')
// Repeat until...
->get();
The 'attribute' in question is actually something drawn from a pivot table.
Obviously, I want it to be in the form of a loop since we're dealing with a variable number of tags. How would I go about doing this?
I suppose you can so something like this:
$query = Model::whereHas('attribute', 'name', '=', 'tag1');
foreach ($other_tags as $tag) {
$query = $query->whereHas('attribute', 'name', '=', $tag);
}
print_r($query->get());
The only hint here is that you need to use first tag to init Model::whereHas. Other tags can be iterated over and each one will be added to $query.
If you have tags in array like this
$tags = [ 'tag1', 'tag2', 'tag3', 'tag4', ...];
then you can simply use whereIn
$results = Model::whereHas('attribute', function($query) use ($tags) {
$query->whereIn('name', $tags);
})->get();
Little bit of trouble with the eloquent framework for laravel.
I need to replicate a query like this :
SELECT *
FROM RepairJob
WHERE NOT EXISTS (SELECT repair_job_id
FROM DismissedRequest
WHERE RepairJob.id = DismissedRequest.repair_job_id);
Right now I have
$repairJobs = RepairJob::with('repairJobPhoto', 'city', 'vehicle')->where('active', '=', 'Y')->whereNotExists('id', [DismissedRequest::all('repair_job_id')])->get();
Anyone an idea? I need to get all the repairjobs where there is no record for in the dismissed requests table
I get this error when using the query above
Argument 1 passed to Illuminate\Database\Query\Builder::whereNotExists() must be an instance of Closure, string given
Try this:
$repairJobs = RepairJob::with('repairJobPhoto', 'city', 'vehicle')
->where('active', '=', 'Y')
->whereNotExists(function($query)
{
$query->select(DB::raw(1))
->from('DismissedRequest')
->whereRaw('RepairJob.id = DismissedRequest.id');
})->get();
Try doesntHave() method. Assuming 'dismissedRequests' as relation name in RepairJob model.
$jobs = RepairJob::with('repairJobPhoto', 'city', 'vehicle')
->where('active', 'Y')->doesntHave('dismissedRequests')->get();
I was wondering how can I build a condition based query in Laravel using eloquent?
I've found how to do it with a raw query but that that's not what I want also the answer to this question isn't that dynamic at least not as dynamic as I want it to be.
What I try to achieve is to create a dynamic WHERE query based on certain conditions, for example if the field is filled or not.
If I use the following code,
$matchThese = [
'role' => 'user',
'place' => \Input::get('location')
];
$availableUsers = User::where($matchThese)->take($count)->orderByRaw("RAND()")->get();
The query will fail if I don't send a location as POST value. I don't want it to fail I want it to skip to the next WHERE clause in the query. So basically if there's no place given don't search for it.
Build up the query and include the ->where() clause depending on whether or not you have the location in your input:
$query = User::where('role', 'user');
$query = \Input::has('location') ? $query->where('location', \Input::get('location')) : $query;
$availableUsers = $query->take($count)->orderByRaw('RAND()')->get();
Just build the array with an if condition:
$matchThese = [
'role' => 'user',
];
if(\Input::has('location')){
$matchThese['place'] = \Input::get('location');
}
$availableUsers = User::where($matchThese)->take($count)->orderByRaw("RAND()")->get();
$query = DB::table('table_name');
if($something == "something"){
$query->where('something', 'something');
}
$some_variable= $query->where('published', 1)->get();
You can use something like this.
I am trying to retrieve all rows in my table, but with user filters(where conditions).
Table looks like this:
news: id, category, type, body, is_active
I want the user to filter them by: type and is_active
So i am using
if(Input::get("is_active"))
News::where("is_active", 1)->get();
if(Input::get("type"))
News::where("type", Input::get("type"))->get();
if(Input::get("category"))
News::where("category", Input::get("category"))->get();
How can I run all conditions on the same query? I don't want to make if/else for each condition and re-write the query all over again!
This way:
$query = News::newQuery();
if(Input::get("is_active"))
$query->where("is_active", 1)->get();
if(Input::get("type"))
$query->where("type", Input::get("type"))->get();
if(Input::get("category"))
$query->where("category", Input::get("category"))->get();
return $query->get();
You can pass a closure to where()
Try this:
$filters = ['is_avtive', 'type', 'category'];
News::where(function($q) use ($filters){
foreach($filters as $field)
$q->where($field, Input::get($field));
})->get();
Try this
News::where("is_active", 1)
->where('type', Input::get("type"))
->where('category', Input::get("category"))
->get();
or if you want only active news with other or conditions.
News::where("is_active", 1)
->orWhere(function($query)
{
$query->where('type', Input::get("type"))
->where('category', Input::get("category"))
})->get();
Check out advanced where