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

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!

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.

Laravel eloquent not retrieving results based on boolean column

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()

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

Laravel Eloquent Conditional inside Query like PLSQL

I'm trying to do conditional statement inside Laravel query, is it possible?
In PLSQL I would do the following to do IF ELSE, can I do it in Laravel Eloquent ?
select *
from x
where x.id = 1
and ( (varName = 'all') OR (x.name = 'red') )
What's happening up there is user is picking from a drop downbox (varName), and is either picking all colors as 'all', or picking individual colors.
I know it would be easy to write two IF Else statements and duplicate the SQL with each condition, but I have a really big SQL, and don't want to duplicate the whole SQL twice just for something simple. Is it possible to do this in Laravel? Or are there other ways to achieve it?
You may use whereRaw for this, for example
...
->where('x.id','=',1)
->whereRaw('(varName = "all" or x.name="red")')
...
Please note that the raw query isn't escaped by default, so make sure to do this if your handling user input.

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