Meaulnes
Meaulnes

Reputation: 487

How to fix a strange behaviour with laravel livewire and pagination?

I started with Laravel 8 and livewire a few days ago. I still have a lot to discover but I am on my way.

Lastly, I encountered a behaviour I have trouble understanding.

My goal

I want to create a page to CRUD posts. What I want is to have a post list that is paginated displayed at the bottom of the page, a button to create a new post and the possibility to click a button on each post line to edit the post. I also want the editor of the post displayed at the top of the page while the list is hidden (but this last possibility is not absolutely necessary).

I could manage to have this working as long as the post list is not paginated but not with pagination.

To do this I use a liveewire component whose code is herebelow:

Component's code in app/Http/livewire/posts/Posts.php

<?php
namespace App\Http\Livewire\Posts;
use App\Models\Post;
use Livewire\Component;
class Posts extends Component
{
    public $posts;
    public $links;
    public $post_id,$title, $abstract, $body,$category,$diaporama_dir,
           $beg_date,$end_date,$close_date,$receive_registration,
           $sticky,$user_id,$inscription_directive;   
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public  function render()
    {   
        $this->mode='list';
        $this->posts=Post::select('id','title')->orderBy('created_at','DESC')->paginate(15)->toArray();
        $this->links=$this->posts['links'];
        //dd($this->links);
        $this->user_id=auth()->user()->id;
        return view('livewire.posts.posts');
    }
    public function donothing(){

    }
    /*
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function resetInputFields(){
        $this->title = '';
        $this->body = '';
        $this->title='';
        $this->abstract='';
        $this->body='';
        $this->category='';
        $this->diaporama_dir='';  
        $this->beg_date='';
        $this->end_date='';
        $this->close_date='';
        $this->receive_registration='';
        $this->sticky='';
        
        $this->inscription_directive='';
    }
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function store()
    {
        
        $validatedData = $this->validate([
            'title' => 'required',
            'body' => 'required',
            'abstract'=>'required',
            'category'=>'',
            'diaporama_dir'=>'',
            'beg_date'=>'sometimes',
            'end_date'=>'sometimes',
            'close_date'=>'sometimes',
            'receive_registration'=>'',
            'sticky'=>'',
            'user_id'=>'required',
            'inscription_directive'=>''
        ]);
        Post::create($validatedData);
        session()->flash('message', 'Bravo ! Votre article a été enregistré.');
       // $this->resetInputFields(); //user may want to keep the input stable
    }
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function edit($id)
    {
        $post = Post::findOrFail($id);
        $this->post_id = $id;
        $this->title = $post->title;
        $this->abstract=$post->abstract;
        $this->body=$post->body;
        $this->category=$post->category;
        $this->diaporama_dir=$post->diaporama_dir;  
        $this->beg_date=$post->beg_date;
        $this->end_date=$post->end_date;
        $this->close_date=$post->close_date;
        $this->receive_registration=$post->receive_registration;
        $this->sticky=$post->sticky;
        $this->inscription_directive=$post->inscription_directive;

        $this->dispatchBrowserEvent('notify','je passe en mode edit');//to switch browser page to edit mode
    }


        /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function update()
    {
        $validatedData = $this->validate([
            'title' => 'required',
            'body' => 'required',
            'abstract'=>'required',
            'category'=>'',
            'diaporama_dir'=>'',
            'beg_date'=>'sometimes',
            'end_date'=>'sometimes',
            'close_date'=>'sometimes',
            'receive_registration'=>'',
            'sticky'=>'',
            'user_id'=>'required',
            'inscription_directive'=>''
        ]);

        $post = Post::find($this->post_id);
        $post->update([
            'title' => $this->title,
            'body'=> $this->body,
            'abstract'=> $this->abstract,
            'category'=> $this->category,
            'diaporama_dir'=>$this->diaporama_dir,
            'beg_date'=>$this->beg_date,
            'end_date'=>$this->end_date,
            'close_date'=>$this->close_date,
            'receive_registration'=>$this->receive_registration,
            'sticky'=>$this->sticky,
            'user_id'=>$this->user_id,
            'inscription_directive'=>$this->inscription_directive
        ]);
      
        session()->flash('message', "Bravo ! L'article a été mis à jour.");

       // $this->resetInputFields();//user may like to keep the input fields stable

    }
}

and views are these:

View 1 : the main page in resources/views/livewire/posts/posts.blade.php

<div class="container m-auto  w-10/12">
<div x-data="{ mode: 'list' }">
    @if (session()->has('message'))
        <div class="bg-green-200 p-4 w-full my-8 text-xl text-orange-500">
            {{ session('message') }}
        </div>
    @endif
    <div x-on:notify.window="mode = 'update'">
        <div x-show="mode==='update'">
            @include('livewire.posts.update')
        </div>

        <div x-show="mode === 'edit'">
            @include('livewire.posts.create')
        </div>
    </div>

   {{-- <div x-show="mode === 'list'" class="">--}} 
       <div>
        <div>
            <button x-on:click="mode = 'edit'" class="bg-red-400 px-2 py-1 border rounded-lg mt-2">Nouvel
                article</button>

        </div>
        <table class=" bg-green-400 w-full table table-bordered mt-5 ">
            <thead>
                <tr class="bg-red-50 mb-2">
                    <th>Id.</th>
                    <th>Titre</th>
                    <th width="150px">Action</th>
                </tr>
            </thead>
            <tbody>

                @for ($i = 0; $i < $posts['per_page']; $i++)
                    <tr class="bg-red-400 mb-2 p-2 space-y-2 border-8 border-red-50 ">
                        <td>{{ $posts['data'][$i]['id'] }}</td>
                        <td>{{ $posts['data'][$i]['title'] }}</td>
                        <td>{{--les actions--}}
                            <button wire:click="edit({{ $posts['data'][$i]['id'] }})"
                                class="btn btn-primary btn-sm">Edit</button>
                            {{-- <button wire:click="delete({{ $post->id }})"
                                class="btn btn-danger btn-sm">Delete</button>--}}
                        </td>
                    </tr>
                @endfor
            </tbody>
        </table>
        <div class="flex flex-row mt-2">
            @for ($i = 0; $i < count($links); $i++)
                <div class="flex p-2 mr-2 border w-max-content">
                     <a href="{{$links[$i]['url'] }}">{{$links[$i]['label']}}</a>

                </div>
            @endfor
        </div>
    </div>
</div>

2- create include in resources/views/livewire/posts/create.blade.php

<div class="container bg-green-500 p-4">
    <form>
        This is the create form
        <input type="hidden" name="user_id" wire:model="user_id" >
        <div class="flex flex-col md:flex-row" >
        <div class=" mt-2 flex flex-col w-max-content">
            <label for="category">Catégorie:</label>
            <select class="" name="category" id="category" wire:model="category">
                <option value="Sans">Sans</option>
                <option value="Annoncement">Annonce d'un événement</option>
            </select>
            @error('category') <span class="text-danger">{{ $message }}</span>@enderror
        </div>
        <div class=" mt-2 flex flex-col flex-auto ml-4">
            <label for="title">Title:</label>
            <input type="text" class="form-control" name="title" id="title" placeholder="Saisissez un titre"
                wire:model="title" value="">
            @error('title') <span class="text-danger">{{ $message }}</span>@enderror
        </div>

    </div>
    <div class="flex flex-col md:flex-row" >
        <div class=" mt-2 flex flex-col w-max-cbeg_date ">
            <label for="beg_date">Date de début</label>
            <input type="text" class="" name="beg_date" id="beg_date" wire:model="beg_date">          
            @error('beg_date') <span class="text-danger">{{ $message }}</span>@enderror
        </div>
        <div class=" mt-2 flex flex-col w-max-cbeg_date ml-4">
            <label for="end_date">Datend_date</label>
            <input type="text" class="" name="end_date" id="end_date" wire:model="end_date">
            @error('end_date') <span class="text-danger">{{ $message }}</span>@enderror
        </div>
        <div class=" mt-2 flex flex-col w-max-cbeg_date ml-4">
            <label for="close_dclose">Date limite</label>
            <input type="text" class="" name="close_date" id="close_date" wire:model="close_date">
            @error('close_date') <span class="text-danger">{{ $message }}</span>@enderror
        </div> 
        <div class=" mt-2 flex flex-col w-max-content ml-4">
            <label for="receive_registration">Accepte les inscriptions:</label>
            <select class="" name="receive_registration" id="receive_registration" wire:model="receive_registration">
                <option value="no">Non</option>
                <option value="yes">Oui</option>
            </select>
            @error('receive_registration') <span class="text-danger">{{ $message }}</span>@enderror
        </div>
        <div class=" mt-2 flex flex-col flex-auto ml-4">
            <label for="title">Dossier du diaporama</label>
            <input type="text" class="form-control" name="diaporama_dir" id="diaporama_dir" placeholder="ex: admin/1"
                wire:model="diaporama_dir">
            @error('diaporama_dir') <span class="text-danger">{{ $message }}</span>@enderror
        </div>
    </div>
    <div class="mt-2 flex flex-col">
        <label for="abstract">Résumé</label>
        <textarea  class="form-control" name="abstract" id="abstract" wire:model="abstract"
            placeholder="Saisissez votre article"></textarea>
        @error('abstract') <span class="text-danger">{{ $message }}</span>@enderror
    </div>

    <div class="mt-2 flex flex-col">
        <label for="body">Corps de l'article</label>
        <textarea  class="form-control" name="body" id="body" wire:model="body" rows=30
            placeholder="Saisissez votre article"></textarea>
        @error('body') <span class="text-danger">{{ $message }}</span>@enderror
    </div>
    <button wire:click.prevent="store()" class="bg-red-400 px-2 py-1 border rounded-lg mt-2">Enregistrer</button>
    <button wire:click.prevent="resetInputFields()" class="bg-red-400 px-2 py-1 border rounded-lg mt-2">Effacer tout</button>
    <button @click.prevent="mode = 'list'" class="bg-red-400 px-2 py-1 border rounded-lg mt-2 ml-16">Retour à la liste</button>
</form>

update include

The update include is exactly the same as the create include except an additional hidden field for the post id.

What is happening?

At startup, I mean when I visit the localhost:8000/posts page, the page 1 is correctly displayed and the links at the bottom of the page are like localhost:8000/posts?page=3 whichever the number of the page may be.

From this page I can normally go to another page using the bottom links, and this several times.

The troube arises when I click a link to edit a post. The post is correctly sent back by the server but instantly we are switched to page 1 of the paginated posts and the bottom links take a strange form such as localhost:8000/livewire/message/posts.posts?page=3 wichever the page number may be.

The trouble arises also when, after having displayed the create form, I type a first char in the fields. In fact it seems that it arises each time a sync is required.

How can I fix this?

Upvotes: 1

Views: 10059

Answers (1)

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

replace

<div class="flex flex-row mt-2">
    @for ($i = 0; $i < count($links); $i++)
        <div class="flex p-2 mr-2 border w-max-content">
             <a href="{{$links[$i]['url'] }}">{{$links[$i]['label']}}</a>

        </div>
    @endfor
</div>

with

<div class="flex flex-row mt-2">
    {{ $posts->links() }}
</div>

and in component

use WithPagination;

ref link https://laravel-livewire.com/docs/2.x/pagination

Upvotes: 4

Related Questions