Laravel - Automatically store history in column - laravel

Can anyone help me to automatically save the history of all changed columns in a model to a history column in the same table as JSON format with column(s) name, original value, changed to, changed by?
I am looking for Traits like centralized function to use in all models.
I am using Laravel 8 with PostgreSQL.

This answer is maybe outdated since it is Laravel 4. But if it works, it is quickest way to insert log in DB wherever save() method is called for that model.
https://stackoverflow.com/a/20694395/13893004

Related

How to import table with values in laravel

I have a table called post.sql. I have created a model called Post using php artisan command,then edited the columns inside the migration table for posts but i am not able to fetch all the values in the table.
Is there any way to achieve it?
You have many different ways to insert the data into the database.
Are you looking for import? You can use the package:
https://packagist.org/packages/maatwebsite/excel
Are you looking to insert record one by one? - You can use tinker
Are you looking to insert it via a form? - You create a controller with all the resources and can query it regularly or using Eloquent on form post.

Update Schema via an Eloquent Model, Laravel 5.2

i'd like to know if it's possible at all to update the table schema in a Migration through a specific Eloquent Model, or if i actually need to pass in the name of the Table and Connection every single time.
I ask this because in my case this requires an additional configuration file that my package must publish to the end users, apart from the already required table Eloquent model (which is used for other purposes)
You can update schema later and add or drop columns and/or index.
To do this you create a new migration and add the changes there. It will change the table over your previous version.
More info in Laravel documentation.
For renaming the table
Schema::rename($from, $to);
https://laravel.com/docs/5.2/migrations#renaming-and-dropping-tables

Laravel 5.3 Scaffold CRUD from existing database

i have an existing database and all my models are defined already. I would like to scaffold Controllers and Views because it's really time consuming.
i found this which sound good: infyom
They say in the documentation that you can use it from an existing database but any command keeps asking me for fields, (as it is trying to create a model from scratch), what i want to to use my existing models.
I found the "scaffold from table" option in the documentation:
php artisan infyom:scaffold Equipement --fromTable --tableName=Equipement
But it says:[ErrorException]
Undefined index: Equipement
i guess it did not find my existing model.
I was wondering, what do they call datatable anyway ? I can see in their config file they have a folder for that, maybe i should define my models there somehow ?
Thanks for anyone who can help on that. (or providing an alternative solution for scaffolding from existing database/models)
You must add id and:
updated_at` TIMESTAMP NULL DEFAULT NULL,
deleted_at` TIMESTAMP NULL DEFAULT NULL
in your table.

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.

Laravel : Delete all records that are not in array

I have a datepicker calendar in my views, and basically I need to synchronize the selected dates (that I send in ajax) with a BoxDeliveryDate model (which only has one column named "date").
So in my Controller I was able to write a pretty nice method to only create a new record if one the selected dates is not yet stored in the database, like this :
foreach (Request::get('dates') as $date) {
$date_formated = date('Y-m-d', strtotime($date));
BoxDeliveryDate::firstOrCreate(['date'=>$date_formated]);
}
Now, if the user de-select one the dates in the datepicker, later, and synchronize, I need to delete it from the database.
Is there a nice way to do that in Laravel ? In other words, to delete every record of the table that are NOT in my Request::get('dates') ?
Also, I searched for a simple way to synchronize everything with only one method, but couldn't find anything.
Thanks for helping !
You can use whereNotIn() for that:
BoxDeliveryDate::whereNotIn('date', Request::get('dates'))->delete();
Note that this will not trigger any model events nor will it work with soft delete. Also, depending on the format of dates you might have to format the array before passing it to whereNotIn().
Also I believe it should be Request::input('dates') but it's possible that both actually works...
I would highly recommend using the soft delete trait (built into Laravel) if your doing mass deletes!
http://laravel.com/docs/4.2/eloquent#soft-deleting
$this->model->whereNotIn('id', $ids)->delete();

Resources