Laravel 5.4: Re-assign a soft-deleted record - laravel

Using soft-deletes in Laravel 5.4. When I try to create an record that already has been created but soft-deleted I get the message "The xxx has already been taken". What is the best way to be able handle the event when
a user attempts to save a record that has already has been soft deleted?
I have been trying to capture this event in the store method of the controller but the store method does not get called if the record already exists - AFAIK.
In my use case I have radios that can be associated to only one user at a time but can be unassigned e.g. soft-deleted and then reassigned to another user in the future.

You can restore() to restore a soft deleted model:
App\User::withTrashed()->where('id', $user_id)->restore();
Restoring Soft Deleted Models

Related

How to Soft Delete a data in Laravel 7 but keeping the data with a Disable status in application?

I have a table with user management in my Laravel Application, and I start use soft delete to dont lost data from database, but I want when I delete my data from my app, just put a disable status instead of delete from app.
enter image description here
Someone can help me? thanks!
When you use soft deletes, it just added a time stamp to the deleted_at column. So when you retrieve the data, Laravel then just provide data that doesnt have timestamp on the column. If you want to retrieve all of the data (including soft deleted data) then just use withTrashed() on your modal.
$flights = App\Flight::withTrashed()
->where('account_id', 1)
->get();
So on your front end, just do a simple checking by disable the button if the deleted_at column is not null.

How to display in blade which rows are soft deleted?

I have created a polling system and in the backend (CMS area) I want the ability for admins to be able to remove polls. When an admin removes a poll, it should soft delete the poll. This is working as intended, however I also want to have the ability for the admin to be able to restore a poll. To do this I am displaying all of the polls (including the soft deleted polls) in the admin area.
PollController index() to get all polls
$polls = Poll::withTrashed()->get();
In the blade I want to have two different buttons for each poll. One of restoring and one for deleting but I only want to display 1 button for each poll depending on whether it can be restored or deleted.
To do this, I have put this inside the foreach in the blade:
#if($poll->trashed())
// Restore button
#else
// Delete button
#endif
However the issue is, trashed() keeps returning true for all the polls when only 1 out of the 3 polls I have are actually soft deleted. I am unsure as to why trashed() returns all of these are true?
How would I get this method working correctly? Thanks.
PART 1
It depends on your query. When using soft deletes, Laravel will query all models that are not soft-deleted by default. When you also want to get the soft-deleted models, you need to call the withTrashed() method on your query. Read more here:
https://laravel.com/docs/5.5/eloquent#querying-soft-deleted-models
To understand what withTrashed() does, you need to understand how soft-deleting works. Soft-deleting models works by adding a new column to your database tables called deleted_at. It's value defaults to null. When you soft-delete a model, Laravel will put the current timestamp into that column. Therefore, this field doesn't contain a null value anymore.
When querying models when using soft-deletes, Laravel appends a deleted_at is null condition to the query. Calling the withTrashed() method, removes that condition from the query.
Have a look on the source of the default query modifier and the withTrashed method.
PART 2
That are events. You can call that to tell Laravel, that it should execute that specific closure when this event happens. In your example, it is listening for the "deleting" event. See more on that here:
https://laravel.com/docs/5.5/eloquent#events
PART 3
You can entirely delete soft-deletable models with the forceDelete() method. See "Permanently Deleting Models" here:
https://laravel.com/docs/5.5/eloquent#querying-soft-deleted-models
FOR EXAMPLE
$items = App\Model::withTrashed()->get();
If you want to restore a single item, just find it by ID:
$item = App\Model::find($id);

Algolia Update Index On Relational Database change with Laravel Scout

I have implemented Aloglia for my Movies table with actors as relational table and it works fine.
Problem:
When I update any movie its also updating algolia index (its good). But how can I update index if I made any change in relational table (for example update an actor of movie).
How to push a specific record manually with laravel scout.
Thanks
The issue itself lies in laravel's events. Whats happening is scout is listening for an 'updated' event which only occurs in laravel when the model object is saved and is dirty (aka value differ from that in the db).
There are two ways you can do this.
The bad lazy way would simply be to add ->touch() to the model prior to save - this will force the updated_at field to update and ultimately trigger the updated event. This is bad as you're wasting a DB query.
The second and preferable way is to register another observer on 'saved' which triggers regardless of whether or not the object is dirty. Likely you either want to check if the model is dirty and only index when its not (to prevent double indexing from the updated event) or just de-register the 'updated' listener that comes in Scout.

How to log all events in my Laravel application

I would like to log all events in my app like:
User 1 - created a new item - created_at...
User 2 - deleted item 213- created_at...
etc.
I don't really see how am I supposed to do it.
Is there any plugin that should help?
You should use Eloquent Events for this. These events will fire before or after any data will be saved into or restored from a database.
Eloquent models fire several events, allowing you to hook into various
points in the model's lifecycle using the following methods: creating,
created, updating, updated, saving, saved, deleting, deleted,
restoring, restored. Events allow you to easily execute code each time
a specific model class is saved or updated in the database.

Laravel audit log for model events using generic way

I am using Laravel 5.2.
I want to create audit log entries for each and every model in my project. Whenever any model gets created, updated or deleted; the audit log for that record should be logged along with old and new values.
For example if I update user's first name from John to George, the audit table entry should be logged something like First name updated from John to George.
I know I can fire created, updated and deleted events and using individual listners I can log this. But I want to create it more generic way.
I don't want to define boot method with created, updated and deleted events in each and every model as I have more than 75 models existing in my project. I don't like duplication of that method in each and every model. I want to make it more generic way, like use something like trait, using that in model should automatically identify which event is fired.
Again, the listener will also listen the event but not for any specific model but generic. I don't want any tight coupling. It should decide which model is updated and make entry of log according to that.
Can any one guide me how to achieve this?

Resources