sync() and attach() not working together? - laravel

In my controller first i attach array of ids like this for my additional fields:
$property->features()->attach($additional_ids);
Then im using sync() because i have checkboxes :
$property->features()->sync($features);
But problem is when i have this then my attach() is not working. Any suggestion how can i fix this? When i remove this sync() my attach is working. So idea is that i have checkboxes for features, and i have input fields for some additional features.I store this ids in pivot table .

sync() will override all changes made by attach(). So, just merge arrays and use sync():
$property->features()->sync(array_merge($features, $additional_ids));

Use this:
$property->features()->sync($features, false);
Which will sync without detaching.
Or you could also call:
$property->features()->syncWithoutDetaching($features)
Which will do the same.

Related

Laravel relationship on a column that is an array

I am rather stuck (or not stuck, as I could go and solve this simply with foreach) with a problem I am facing.
I have database where I have a model that should have a relation to another one. No problem here. However, that relation is defined in a column that is an array. I sadly cant change that. Is there any way to still get the corresponding relation of hasMany etc?
Or is my only choice to get all entries of my model and then go over foreach?
Thanks in advance.

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.

laravel 5.7 : paginate with unique data

I want create a query that sort by desc after it get unique event_name (i have many same name in my table but i want show one of them) and after it i want to paginate it
i tried this
DB::table('events')->latest()->get()->unique('event_name');
but i cant paginate it because its a collection
i tried this
Event::order By("created_at","DESC")->paginate(6)->all();
but i cant unique data
anyone can help me?
You can use
Event::distinct('event_name')->orderby("created_at","DESC")->paginate(6);
Try This
Event::orderBy("created_at","DESC")->distinct('event_name')->paginate(6);
If I understood you correctly, you need something like this?
Event::groupBy('event_name')->orderBy('created_at', 'desc')->paginate(6);

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

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

Resources