Laravel livewire not seeing render update after attach - laravel

I have a livewire component rendering two lists on a page. I have a button on the left list to add that item to the right list (many to many) relation using attach. This is working, but the right list isn't getting re-rendered. When I then click a filter I have on the left list, the right list re-renders showing the newly attached item. It seems like either render() isn't being called when I do the attach method, or it is taking another call to render to pick up the new relation? I also tried calling $this->render from the attach method, but that didn't help.
public function render()
{
$items = item::with('status')
->where(function($query){
$query->when(!empty($this->selectedStati), function ($query) {
$query->whereHas('status', function ($query) {
$query->whereIn('id', $this->selectedStati);
})->orWhereDoesntHave('status');
});
})
->get();
$testItems = $this->test->items;
//dd($testItems);
return view('livewire.items-source', [
'selectedStati' => $this->selectedStati,
'statuses' => $this->status,
'items' => $items,
'testItems' => $testItems
]);
}
public function filterStatus($id){
if (($key = array_search($id, $this->selectedStati)) !== false) {
unset($this->selectedStati[$key]);
} else {
$this->selectedStati[]=$id;
}
session(['status'=>$this->selectedStati]);
}
public function addToTest($id){
//attach the given item to the test
$this->test->items()->attach($id);
dd($this->test->items);
}
public function removeFromTest($id){
//detach the given item from the test
$this->test->items()->detach($id);
}

after attach the item to test collection refresh model to rehydrate it with fresh data like
public function addToTest($id){
//attach the given item to the test
$this->test->items()->attach($id);
$this->test->refresh();
}

Related

Laravel custom attributes loads relationships even when attribute is not asked

I have a custom attribute that calculates the squad name (to make our frontend team lives easier).
This requires a relation to be loaded and even if the attribute is not being called/asked (this happens with spatie query builder, an allowedAppends array on the model being passed to the query builder and a GET param with the required append(s)) it still loads the relationship.
// Model
public function getSquadNameAttribute()
{
$this->loadMissing('slots');
// Note: This model's slots is guaranteed to all have the same squad name (hence the first() on slots).
$firstSlot = $this->slots->first()->loadMissing('shift.squad');
return ($firstSlot) ? $firstSlot->shift->squad->name : null;
}
// Resource
public function toArray($request)
{
return [
'id' => $this->id,
'squad_name' => $this->when(array_key_exists('squad_name', $this->resource->toArray()), $this->squad_name),
'slots' => SlotResource::collection($this->whenLoaded('slots')),
];
}
Note: squad_name does not get returned if it's not being asked in the above example, the relationship is however still being loaded regardless
A possible solution I found was to edit the resource and includes if's but this heavily reduces the readability of the code and I'm personally not a fan.
public function toArray($request)
{
$collection = [
'id' => $this->id,
'slots' => SlotResource::collection($this->whenLoaded('slots')),
];
if (array_key_exists('squad_name', $this->resource->toArray())) {
$collection['squad_name'] = $this->squad_name;
}
return $collection;
}
Is there another way to avoid the relationship being loaded if the attribute is not asked without having spam my resource with multiple if's?
The easiest and most reliable way I have found was to make a function in a helper class that checks this for me.
This way you can also customize it to your needs.
-- RequestHelper class
public static function inAppends(string $value)
{
$appends = strpos(request()->append, ',') !== false ? preg_split('/, ?/', request()->append) : [request()->append];
return in_array($value, $appends);
}
-- Resource
'squad_name' => $this->when(RequestHelper::inAppends('squad_name'), function () {
return $this->squad_name;
}),

Can't get Livewire Events Emit and Listen to work

I am trying to setup an event listener, so that when a child livewire component gets the title updated, it would refresh the parent component to show the update instead of having to hard refresh the page to see the update.
This is a quick gif showing what is taking place
https://i.gyazo.com/faefb27c2fe0fb32da097fbbf5cc1acb.mp4
I have 2 livewire components.
Parent = ViewSidebar.php / view-sidebar.blade.php
// view-sidebar.blade.php
#foreach ($kanbans as $kanban )
#livewire('kanbans.show-sidebar-kanban', ['kanban'=>$kanban], key($kanban->id))
#endforeach
// ViewSidebar.php
public $kanbans;
protected $listeners = ['refreshKanbans'];
public function refreshKanbans()
{
$this->kanbans = Kanban::where('status', $this->active)
->orderBy('order', 'ASC')
->get();
}
public function mount()
{
$this->refreshKanbans();
}
public function render()
{
return view('livewire.kanbans.view-sidebar');
}
In the child component I set this
public function updateKanban($id)
{
$this->validate([
'title' => 'required',
]);
$id = Kanban::find($id);
if ($id) {
$id->update([
'title' => $this->title,
]);
}
$this->resetInputFields();
$this->emit('refreshKanbans');
}
All of the files are in a subfolder called kanbans could this be breaking it?
Trying to follow along these docs https://laravel-livewire.com/docs/2.x/events
I also tried this approach with calling the emit $this->emit('updateKanbanSidebar'); and setting the listener like this protected $listeners = ['updateKanbanSidebar' => 'refreshKanbans'];
Clearly I am understanding the documentation wrong, but no clue where the issue is.
Any help is much appreciated!
Thank you in advance :)
There is something wrong in your code, so let me help you with. After emit from child (be sure this is doing well) just need have this in parent component
Parent
protected $listeners = ['refreshKanbans' => '$refresh'];
public function render()
{
$this->kanbans = Kanban::where('status', $this->active)
->orderBy('order', 'ASC')
->get();
return view('livewire.kanbans.view-sidebar');
}
Let me know about
I was able to get this to work using this in the child component, and skipping the emits. I was able to DD and the emit was working properly, but not sure why it wasn't updating.
public function updateKanban($id)
{
$this->validate([
'title' => 'required',
]);
$this->kanban->update(['title' => $this->title]);
$this->resetInputFields();
$this->kanban=Kanban::find($this->kanban->id);
}

Returning a value back to the same livewire component?

I'm trying to send a property, maxId back with my user data. This property is the id of the last row received from the database (cursor-based pagination). I do this like so:
public $maxId = 0;
public function loadMore()
{
//?
}
public function render()
{
$data = User::where('id', '>', $maxId)->limit(10)->get();
$data->maxId = $data->last()->id;
return view('livewire.users', ['users' => $data]);
}
But how can I return (or retain the data on the server) of maxId so I know where to start collecting my data from next time.
Please note I do not wish to implement standard laravel pagination.

Laravel: Cache with pagination clear issue

I have laravel (7.x) application. I recently added the cache functionality for the performance boost. After implementing the cache functionality, I was having trouble with the pagination while loading the data in grid format, so I googled for the solution and found this Pagination with cache in Laravel.
Although, it did solve my problem. But, the case is that I have about 100 pages and due to the solution I found, each page has it's own cache. Now, if I create or update any record then it doesn't reflect in the grid because the data is loaded from the cache.
PostController.php:
...
$arraySearch = request()->all();
# calculating selected tab
$cache = (!empty(request()->inactive)) ? 'inactive' : 'active';
$cacheKey = strtoupper("{$this->controller}-index-{$cache}-{$arraySearch['page']}");
# caching the fetch data
$arrayModels = cache()->remember($cacheKey, 1440, function() use ($arraySearch) {
# models
$Post = new Post();
# returning
return [
'active' => $Post->_index(1, 'active', $arraySearch),
'inactive' => $Post->_index(0, 'inactive', $arraySearch),
];
});
...
Post.php:
public function _index($status = 1, $page = null, $arraySearch = null)
{
...
$Self = self::where('status', $status)
->orderBy('status', 'ASC')
->orderBy('title', 'ASC')
->paginate(10);
...
return $Self;
}
How do I clear all this cache to show the newly created or updated record to with the updated values.?
1. Store All pages under the same tag:
As seen on the documentation: https://laravel.com/docs/master/cache#storing-tagged-cache-items
You can use tags to group cached items.
$cacheTag = strtoupper("{$this->controller}-index-{$cache}");
$arrayModels = cache()->tags([$cacheTag])->remember($cacheKey, 1440, function() use ($arraySearch) {
...
2. Set an event listener on Post to clear the tag
You can run an Event listener on your Post update() or create() events.
https://laravel.com/docs/7.x/eloquent#events-using-closures
You can then clear the tag cache using
Cache::tags([$cacheTag])->flush();
I know this isn't the proper solution. But, until I find the proper way to do it, this is the option I am kind of stuck with.
PostController.php:
public function index()
{
...
$arraySearch = request()->all();
# calculating selected tab
$cache = (!empty(request()->inactive)) ? 'inactive' : 'active';
$cacheKey = strtoupper("{$this->controller}-index-{$cache}-{$arraySearch['page']}");
# caching the fetch data
$arrayModels = cache()->remember($cacheKey, 1440, function() use ($arraySearch) {
# models
$Post = new Post();
# returning
return [
'active' => $Post->_index(1, 'active', $arraySearch),
'inactive' => $Post->_index(0, 'inactive', $arraySearch),
];
});
...
}
public function store()
{
...
Artisan::call('cache:clear');
...
}
I'll post the proper solution when I find one. Till then I am using this one.
There is a method in Laravel Model class called booted (not boot, which is having a different purpose). This method runs every time something is "saved" (including "updated") or "deleted".
I have used this as following (in a Model; or a Trait, included in a Model):
protected static function booted(): void
{
$item = resolve(self::class);
static::saved(function () use ($item) {
$item->updateCaches();
});
static::deleted(function () use ($item) {
$item->updateCaches();
});
}
"updateCaches" is a method in the Trait (or in the Model), that can have the code to update the cache.

Laravel - Best way to store user and his/her dynamically created relations on submit

Which way should I store User Relations which are made "Dynamically"?
By that i mean. I have People table with record of husband for example now husband can add as relation to him (hasMany) his wife and children. Now I have setup the model and it works
Model
This relations are in same table as data is same for them I only need to know if they belong to husband
public function children(){
return $this->hasMany( get_class($this) ,'people_id','id');
}
public function parent(){
return $this->belongsTo( get_class($this) , 'people_id','id');
}
Now when i go to form and add a person husband there. I would have an option to dynamically add his children() (his wife, his children, his grandma...) and submit once its done.
Now i have tought about saving these children to Session first (put them in array) and on submit to post it to controller. Is this a good way or should i do it some other way?
(I hope i explained this well, if not i will try to explain it better)
I have done it like this i dont know if this way is ok but it works for me. Also to add i have just used name for the DB data as i wanted to get this to work.
Store parent and his/her children (if any) in DB from Session
public function store(AdminCreatePersonRequest $request)
{
$data = $request->all();
$parent = new Person;
$parent->name = $data['name'];
$parent->save();
$children = $request->session()->pull('children');
if ($children != null) {
foreach($children as $childData){
$child = new Person(['name' => $childData['child-name']]);
$parent->children()->save($child);
}
}
return redirect('dashboard')->with('success', ['The user was successfully created']);
}
AJAX
Add child to session
public function add(AdminCreatePersonChildRequest $request)
{
$temp = $request->session()->get('children',[]);
$data = $request->all();
unset($data['_token']);
array_push($temp,$data);
end($temp);
$key = key($temp);
$data += ['id' => $key];
$request->session()->put('children', $temp);
return $data;
}
Remove child from session
public function delete(Request $request, $id) {
$temp = $request->session()->pull('children');
unset($temp[$id]);
$request->session()->put('children', $temp);
return 1;
}
Load children from Session on view create (if any)
public function load(Request $request) //get children from session (if exists)
{
$data = $request->session()->get('children');
if($data != null){
return $data;
}
else {
return 0;
}
}
Clear all children from session
public function clear(Request $request) //remove children from session
{
$request->session()->forget('children');
return 1;
}
And with data form controller you update your view (in my case add <li> to <ul> element)

Resources