$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();
Related
I am new to Laravel. I am looking for a way to insert a value to specific field such as created_at automatically whenever the row is inserted to the DB table.
I found out that only Eloquent way which uses some Model->save() inserts timestamp to desired field.
But I want to be able to insert a value even when I use QueryBuilder. Is there any way to do so?
I have looked at this post: Query builder not inserting timestamps
Any advice would be appreciated.
Thank you in advance
You've said before you're using Model::insertGetId(), but the thing is this method is not using Eloquent. It's a Query Builder method. So, use create() instead:
$object = Model::create($data);
$id = $object->id;
If you still want to use insertGetId(), you'll need to add created_at timestamp manually:
$data['created_at'] = now();
Model::insertGetId($data);
Or you could set CURRENT_TIMESTAMP as a default for timestamp column, but it's so much better to just use Eloquent to handle this.
You can make a new helper methods in helpers.php
Something like this:
public function queryBuilderInsert($table, $data, $deleted_at = false)
{
$data['created_at'] = \Carbon::now();
$data['updated_at'] = \Carbon::now();
$deleted_at ? $data['deleted_at'] = null :;
\DB::table($table)->insert($data);
}
public function queryBuilderUpdate($builder, $data)
{
$data['updated_at'] = \Carbon::now();
$builder->update($data);
}
And then you can build your queries like this:
For insert use queryBuilderInsert method
$example_data = [
'name' => 'Name',
'email'=> 'Email'
];
queryBuilderInsert('users',$example_data);
If you're using soft deletes on your table you can use the third parameter of queryBuilderInsert
queryBuilderInsert('users',$example_data, true);
For update, you can make your query and then call queryBuilderUpdate method
$example_update_data = [
'name' => 'Name',
'email'=> 'Email'
];
$builder = \DB::table('users')->where('id',1);
queryBuilderUpdate($builder,$example_update_data);
It will set updated_at date and make update
I want to check if user login only if email exist and activated==1
set in database but activated always show null value.
Image 1:
Image 2:
Try this one
$user = DB::table('users')->where([
['email', '=', $email],
['activated', '=', '1'],
])->get();
or
make your where clause with array to check multiple conditions.
You can use Model class to get data using where clause.
See Queries: Laravel
Another Example (eloquent):
$matchThese = ['email' => $email, 'activated' => '1'];
//........
//........
$results = User::where($matchThese)->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.
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 :)