How to check values before update in Eloquent? - laravel

When using updating via Eloquent it returns affected rows as a result. Is it possible somehow to get status of update? For example if I passing some bad id in WHERE it still return 0 affected rows, though here I need to know that this id does not exist. Basically when the user clicks a save button, but nothing edit - I get 0 affected rows from Eloquent and also when bad id is entered. I need to somehow separate this things.

You could check first if the WHERE returns anything:
if(Model::where($where)->count() > 0){
// row(s) found > do update
}
else {
// where doesn't match any rows
}

Related

I want to get the next id that will be created but not yet in laravel

I want to get the next id that will be created but not yet in laravel.
Is this code correct?
$tableStatus = DB::select("show table status from database_name where Name = 'table_name'");
if (empty($tableStatus)) {
throw new \Exception("Table not found");
}
// Get first table result, get its next auto incrementing value
echo $tableStatus[0]->Auto_increment;
Yes, the code is correct.
You can achive this in several other ways. For example, You can query the particular database table to extract last created id by ordering the rows based on created_at column or id column itself and finally increment the value by 1 to get next auto-increment value.

Laravel 5.5 - Check if only one column will be updated

Using Laravel 5.5 and models, I would like to know, before updated if only one column, statut, will be updated.
The interest is to just updated the statut if there are no changes or, if there are some columns to be updated, create a new row. Because each modification need to be approved before published.
Actually, I have a big if like :
if($request->statut != $product->statut && $request->title == $product ...)
Is there a quickest way ?
Thanks.
You can utilize isDirty('field') method to check if the field was changed (and not yet saved). getDirty() will return you array of all such fields and their values.
If I understood your case correctly, then you can do:
if ($product->isDirty('statut')) {
if (count($product->getDirty()) == 1) {
...
}
}
First we check if field statut was changed for your $product. Then we just check if number of changed fields is 1 (meaning that there are no more changed fields than just statut).

How to return empty list of records in laravel?

I have some model Task.
If current user has permission then he can see all tasks
return Task::all();
If current user does not have this permission then he can see only empty list
return Task::where('id', 0)->get();
Design is so that user must always get some result, but my functions work with data returned from Task::all() so I can not return empty array or empty Eloquent collection.
What should I do?
You don't want empty array, you don't want empty collection. Maybe you should change the design. Anyway, since you really want to pass an empty list of tasks, manually insert 1 o 2 tasks in the database as first tasks, so their IDs will always be 1 & 2. But leave everything else empty. Then you may always return those 2
return Task::whereIn('id', [1,2])->get();
My suggestion, is to actually fix the design to expect empty object, so you could do something like
return '';
One option would be to return a newly created Task object in an array if the JSON schema has to match exactly. It would look like so:
$emptyTask = new Task();
return [$emptyTask]

laravel update shouldn't do anything if no value changed

If I open a form to edit and without changing any value just hit Submit button to update, it updates the row in the table i.e. it changes the updated_at time and even executes some code in bootXXXX(static::saving(function($model) {}) function.
I do not want that to happen.
How can I find If user changed nothing and its a blank submit button hit?
Of course before update I can fetch the current values and compare them with input $request->all() and decide to update or not, but Is there any better way to do so the laravel way?
Thanks,
K
you have 2 methods one is getDirty() by using this you will get current changed input value, another one is getOriginal() if you use that you will get what previous data you have
By using those 2 methods i am giving here a example,may be it will help you
static::saving(function($model)
{
foreach($model->getDirty() as $attribute => $value){
$original= $model->getOriginal($attribute);
echo "Changed $attribute from '$original' to '$value'<br/>\r\n";
}
return true; //if false the model wont save!
});

Apply filter on collection Object

I am getting this object on list page
$_productCollection=$this->getLoadedProductCollection();
//return 3 records
now I applied filter as
$_productCollection=$_productCollection->addFieldToFilter('genre', array('finset' => '126'));
//now it should return 1 record
but it gives me a count of 3. Now, if I run the query in database by getting the query using echo $_productCollection->getSelect(); it returns 1 record.
Can anybody help me to resolve this?
Most likely this doesn't work because $this->getLoadedProductCollection() returns a collection which already has been loaded by the catalog/layer singleton.
But you could override Mage_Catalog_Model_Layer::prepareProductCollection() to get in control and add the custom filters you want.

Resources