Laravel : Delete all records that are not in array - laravel

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

Related

Laravel - Automatically store history in column

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

Laravel: A field that lookup another field from different table

I use Backpack for Laravel.
I know that to add a field, we can use:
CRUD::addField(['name'=>..])
But, I don't know how to add a field that lookup another field input from different table to show a lookup value.
Please look at the illustration below.
Thanks.
Edit:
I found a temporary solution to this, but not effective.
Reference: https://backpackforlaravel.com/docs/4.1/crud-how-to#add-a-select2-field-that-depends-on-another-field
With select2_from_ajax you still have to select the option (even its just one option left), meanwhile what I really want is its automatically select that one option.
It's depends on what you need to do. You can use relationship field and show a select with the list of user. If you really wanna do it like this. You can probably make an ajax request to show it. In that case you ll need to use a custom view for the edit or the create view.

Adding repetitive actions to the create method

How can I make repetitive actions available each time I call the create method on a model. It so happens like each time I write into the database, where each table has a column of createdby - so I just want to write in one place that this column should take on the username in the session!
Thanks in advance
There are several events in Eloquent you can hook into.
It sounds like you're after the creatingevent.
User::creating(function($user) {
$user->created_by = Auth::user()->name;
});

Update, Insert, Delete records Joomla Table

I'm able to fetch information for whatever table I need the problem here is the update, insert, and delete records, is not working..
I have read the Joomla doc's but even doing the simplest update queries are not working... so here is my code:
UPDATE:
// I'm getting the data from an array
if (!empty($_POST['data'])) {
$getData = json_decode($_POST['data'], true);
}
// after this line I have a foreach for the array
// in the foreach I have a few IF's
// ether if the result from IF is True or False
// in both I have similar queries
// So let say IF return true;
// Lets prepare the Data using OBJECT's
$object = new stdClass();
$object->product_id = $v['product_id'];
$object->product_name = $v['product_name'];
$object->product_price = $v['product_price'];
$object->product_number = $v['product_number'];
$result = JFactory::getDbo()->updateObject('#__tienda_product', $object, 'product_number');
// that should Update my tables but it doesn't ... now my table has about 21 columns
// but I only need to update 4 columns base on the column product_number
You might have notice the $v['product_id'] where $v is from the foreach, the foreach and the first query are working fine, I did a few echo's before moving to the Update part just to make sure I'm getting the correct data in a format that it should be... any way... the Update part is not working... so I thought it may be because of my table I when to use one table from the regular instalation of joomla... but it still the same no result on the update...
Does any one know how to "update" a table using Joomla CMS as framework?...
Remember I want to use the updateObject() method...
Why don't I use Joomla Framework instead of the CMS's library?
Well, I can give you a few examples, let say:
a business man want a simple basic report, he doesn't care about managing the site he has people to do that, he also gets a sales report, but he doesn't trust the crew just yet and he want to able to see and compare the result, and he needs and standalone without all the fancy tools that joomla has and for that he need an standalone app that can give that kind of report... ok I might have gone a bit far, but the idea is to have basic, simple, easy to read reports and updates, and that is why I have started this project.
Again, I already went to joomla and read the docs but following their examples it just not working... now, am I have to declear all of the columns even if they don't need to be update? or am I missing an execution for the query which in joomla doesn't mention any executions when using $object() only when using regular queries SQL... the idea is to use $object() ...
Thank you for taking the time.
You have defined the updateobject as a variable, and are not calling it. try changing this:
$result = JFactory::getDbo()->updateObject('#__tienda_product', $object, 'product_number');
to this:
JFactory::getDbo()->updateObject('#__tienda_product', $object, 'product_number');
Also, on a side note, you should not use $_POST and instead should use JInput

CodeIgniter Many-to-Many Relationship Management

Can anyone point out a good many-to-many database tutorial for CodeIgniter.
Just trying to work out the process of creating, and then updating a many-to-many relationship. My example uses a multi-select of values, wondering how you take care of monitoring changes on update etc.
I'd like to share what I do in my application. This is basically same with my answer in this question.
After user submit, and before entering to database, I will fetch the existing data in the database into an array. Example: $collection = array('111', '112', '113', '114'); (This is just for example. In real, it should fetch from database then put the value to array)
I will check the new user input in two step. First step is to see if it already in the database or not. If it not, then insert. Otherwise ignore:
foreach ( $inputs as $input )
{
if ( ! in_array($input, $collection) )
{
//do insert here
}
}
Then in second loop, I do it in reverse, to delete the data that not selected by user.
foreach ( $collection as $data )
{
if ( ! in_array($data, $inputs) )
{
//do delete here
}
}
In your case, you might or might not need the second loop. I needed this since I make the input as checkboxes, that the user can choose to activate / deactivate, thus I translate it as insert and delete.
Since you will implement it using multi-select, then basically it's same with my checkboxes.
If you have structure or code example, feel free to share it, and I will help you fine tune it (of course with my style, that might or might not optimized yet).
This website has some tutorials for many to many. It uses doctrine though.
http://www.phpandstuff.com/articles/category/tutorials
The codeigniter from scratch series will cover almost anything you want to know about the framework
http://net.tutsplus.com/videos/screencasts/codeigniter-from-scratch-day-1/
hope that helps. there are 7 to date btw

Resources