Laravel eloquent not retrieving results based on boolean column - laravel

I have 33 results on my DB where this boolean field (resolvido) is showing zero, if I run the SQL query it retrieves the results, however in the model the collection always comes empty.
If I change to Model::where('resolvido',1)->get(), I do get the correspondent results, however the Model::where('resolvido',0)->get() or the Model::where('resolvido',false)->get() or even using the query builder and raw sql, the collection never picks up those 33 results where resolvido = 0.
I've also tried in Tinker and it always comes up empty.
Migration:
$table->boolean('resolvido')->nullable();
On DB comes up as TINYINT
Any tips on what it might be?
Thanks in advance

Naturally, there is no boolean data type for MySQL but rather uses TINYINT (0 or 1) for this.
Laravel's query builder, allows using boolean in the where clause. You might want to try these:
Model::where('resolved', true)->get();
Model::where('resolved', false)->get();
Since you've set the resolved column in the DB as nullable, you might also want to try:
Model::where('resolved', null)->get();
If you don't want that behavior use $table->boolean('resolved')->default(false) instead

Try this
Model::where('resolved', null)->get()
or
Model::where('resolved', '')->get()

Related

HybridRelations laravel mongo is really slow

Used HybridRelations to store user notifications in mongoDB instead of mysql and imported 3million records and now user->notifications()->take(5)->get() or user->notifications()->paginate(5) takes over 30 seconds.
any idea how to speed it up?
on mysql with same amount of records it took 200 milliseconds.
notificaiton model has user_id and description and priority and read flag and user_id is set as an index.
Maybe this suggestion can help you.
When use user->notifications() means the query like that
db.notificaiton.find( {"user_id": <?> } )
That will get all record by user_id.
You can dump the user->notifications() and check. That should be Collection type and contains more than 5 records.
The solution is this.
You can write specific method to ensure what your mongo query expression.
The problem was the column that you are sorting by should be an index as well as the columns you use where clause on.

query to get only one item from target column in Laravel

Is there any query to get only one item from target column? below didn't working?
App\Job::whereNotNull('deleted_at')->pluck('customer_name')->first()
I wish to get just one item name from 'customer_name'
Now Laravel 5.6 using...
When using eloquent, using first will give you a single item, the first record matching your query. You can then access the property on that item:
// This will give you an instance of App\Job (or null)
$job = App\Job::whereNotNull('deleted_at')->first();
// You can then access the customer_name property on the object
$job->customer_name;
If you only want to retrieve that single column when you run your query, you can pass an array of columns to first.
App\Job::whereNotNull('deleted_at')->first(['customer_name']);
Assuming you're using the SoftDeletes trait in your model, your query will automatically have an additional check added to all of your queries to ensure that deleted_at is null. So when you're doing ->whereNotNull('deleted_at') you're adding an additional clause to ensure records are not null as well, so you won't have any records being returned.
If you want to look only at deleted records, you can use onlyTrashed():
App\Job::onlyTrashed()->first();
There is a built-in method for this: value('field')
App\Job::whereNotNull('deleted_at')->value('customer_name');
Docs: https://laravel.com/docs/5.6/queries, look for "Retrieving A Single Row / Column From A Table"
If you meant only one field but still as rows, you can use ->select('field') instead.
And since you want only trashed items, you can use ->onlyTrashed() instead of the not null check.
Soft deleting documentation: https://laravel.com/docs/5.6/eloquent#deleting-models
Try like this:
$job = \App\Job::selectRaw('customer_name')
->whereNotNull('deleted_at')
->first();
And use it like $job->customer_name

Delete query builder in laravel/eloquent using slim3 returning integer 0

I am using slim3 eloqent/laravel and am trying to build a query to delete an entry from the database using multiple where clauses.
According to laravel's documentation this query should delete correctly;
$deleteGalleryItem = Home_Page::where("ul_id",$ul)
->where("ul_update_no",$ul_update_no)
->delete();
var_dump($deleteGalleryItem);
die();
I also tried;
$deleteGalleryItem = Home_Page::where("ul_id","=",$ul)
->where("ul_update_no","=",$ul_update_no)
->delete();
var_dump($deleteGalleryItem);
die();
However each time I run the var_dump I get returned integer 0
Is this the correct way to structure a mysql delete statement in eloquent/laravel in slim3?
Or should I first select the data then delete?
There is nothing wrong with the way you've constructed your query.
The number returned is how many rows were removed with that query, so the reason you'll be getting 0 is simply because you didn't have any rows in the database with those constraints.
Hope this Helps!

Use retrieved value from internal query to query with Laravel Eloquent

The value I need for the now() query is the timezone from the Group model, I'm just not sure how to get it within the query.
I would like to use $reminder->group->timezone value instead of hardcoding the value in now(). Any help would be greatly appreciated.
$reminders = Reminder::with('group','appointment')
->where('reminder_datetime', '<', Carbon::now('America/New_York')) --> Use $reminder->group->timezone value instead of hardcoding the value
->get();
The query will not be executed until you call the get method.
However, you need the query result of the group of reminder to reach the timezone in order to call the now method of Carbon inside PHP.
Because you need the timezone before retrieving the query results, that won't work.
Instead, you can get the reminder results without the where method - with timezones - and than filter the Eloquent Collection as you want in your method.

How to stop Doctrine from returning a primary key for every query

I am kind of annoyed at Doctrine for returning primary keys in each and every query even though I don't want it to. Is there anyway to stop this ? coz I don't really want those damn primary keys along with my doctrine query results.
A query for instance that I have is:
$getAllDatesForUserQuery = $this->createQuery('s')
->select('s.datename')
->where('s.userid = ?',3)
->setHydrationMode(Doctrine::HYDRATE_ARRAY) ;
In this situation, it retrieves all the datenames as it should, but also happily returns the primary key column value. I DON"T WANT IT.
Is it me? or is it Doctrine ?
In a case like this where you want a simple array and only have a single field being selected, the answer is the Single Scalar Hydration mode. Use it like this:
$q = $this->createQuery('s')
->select('s.datename')
->where('s.userid = ?',3)
->setHydrationMode(Doctrine::HYDRATE_SINGLE_SCALAR);
You should find that the query will return a simple one-dimensional array containing only the value(s) you wanted.

Resources