Question:
Retrieve data using livewire and blade - Laravel

Retrieve data using livewire and blade - Laravel


Problem:

I am creating a feature for user to submit cases, i could save the case to the database but i am unable to retrieve it from the database. here's the code.


  1. manage-case.php component

<?php


namespace App\Http\Livewire;


use App\Models\Cases;

use Livewire\Component;


class ManageCase extends Component

{

    public $cases;

    public $title;

    public $description;

    public $status;

    public $feedback;



    public function render()

    {

        // Load the Livewire view with the current user's cases

        return view('livewire.manage-case');

    }

    public function addCase()

    {

        

        $cases = new Cases();

        $cases->title = $this->title;

        $cases->description = $this->description;

        $cases->status = 'pending';

        $cases->feedback = 'pending for admin reply';

        $cases->id = auth()->user()->id;


        $cases->save();

        $this->resetFields();

        return redirect()->route('manage-case');

    }


    public function getUserCases()

    {

        // Retrieve cases belonging to the authenticated user

        $this->cases = Cases::where('id', auth()->user()->id)->get();

    }


    private function resetFields()

    {

        // Reset input fields to their default values

        $this->title = '';

        $this->description = '';

        $this->status = '';

        $this->feedback = '';

    }

}


  1. manage-case.blade

<div class="flex">

    <div class="w-1/5">

        <ul class="list-none text-xl flex flex-col items-center rounded-lg bg-gray-200 p-4">

            <li class="mt-6 mb-6"><a href="" class="block hover:bg-gray-300 transition-colors duration-200 p-2 rounded-lg">Orders</a></li>

            <li class="mt-6 mb-6"><a href="{{route('manage-location')}}" class="block hover:bg-gray-300 transition-colors duration-200 p-2 rounded-lg">Addresses</a></li>

            <li class="mt-6 mb-6"><a href="#" class="block hover:bg-gray-300 transition-colors duration-200 p-2 rounded-lg">Insurance</a></li>

            <li class="mt-6 mb-6"><a href="#" class="block hover:bg-gray-300 transition-colors duration-200 p-2 rounded-lg">Review</a></li>

            <li class="mt-6 mb-6"><a href="{{route('manage-case')}}" class="block hover:bg-gray-300 transition-colors duration-200 p-2 rounded-lg">Cases</a></li>

        </ul>

        

    </div>

    <div class="w-4/5">

        <div>

            <label for="title" class="input-label w-32">Title</label><br>

            <input type="text" wire:model="title" class="input-field">

        </div>

        <div>

            <label for="description" class="input-label w-32">Description</label><br>

            <input type="text" wire:model='description' class="input-field rounded-sm">

        </div>

        <div>

            <button class="" wire:click='addCase'>Submit</button>

        </div>

    

        

    </div>


    

    <div class="mt-10">

        <div class="flex flex-wrap">

            @if ($cases !== null && count($cases) > 0)

                @foreach ($cases as $case)

                    <div class="w-full md:w-1/2 lg:w-1/3 px-2 mb-4">

                        @livewire('retrieve-case', ['caseid' => $case->caseID], key($case->caseID))

                    </div>

                @endforeach

            @else

                <p>no record found</p>

            @endif

        </div>

    </div>

    

</div>


  1. retrieve-case.php component

<?php


namespace App\Http\Livewire;


use Livewire\Component;

use App\Models\Cases;



class RetrieveCase extends Component

{

    public $cases;

    public $caseID;

    public $title;

    public $description;

    public $status;

    public $id;


    public function mount($caseID){

        $this->cases = Cases::find($caseID);

        $this->caseID = $caseID;

        $this->title = $this->case->title;

        $this->description = $this->case->description;

        $this->status = $this->case->status;

        $this->id = $this->case->id;


    }

    

    public function render()

    {

        return view('livewire.retrieve-case');

    }


    public function deleteCase(){

        $this->cases->delete();

        $this->emit('caseDeleted');

    }


    public function getUserCases()

    {

        // Retrieve cases belonging to the authenticated user

        $this->cases = Cases::where('id', auth()->user()->id)->get();

    }    

}


  1. retrieve-case.blade

<!-- resources/views/livewire/retrieve-case.blade.php -->


<div>

    <h2>Case Details</h2>


    @if ($case)

        <ul>

            <li>

                <strong>Title:</strong> {{ $case->title }}<br>

                <strong>Description:</strong> {{ $case->description }}<br>

                <strong>Status:</strong> {{ $case->status }}<br>

                <strong>ID:</strong> {{ $case->id }}<br>


                <!-- Button to delete the case -->

                <button wire:click="deleteCase">Delete</button>

            </li>

        </ul>

    @else

        <p>No case found.</p>

    @endif

</div>


I tried changing the foreach loop and it still doesnt work, i expect it to be able to retrieve the submitted cases and list them out using foreach loop


  1. Migration

<?php


use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;


return new class extends Migration

{

    /**

     * Run the migrations.

     */

    public function up(): void

    {

        Schema::create('cases', function (Blueprint $table) {

            $table->id('caseID');

            $table->string('title');

            $table->string('description');

            $table->string('feedback');

            $table->string('status');

            $table->timestamps();

            $table->foreignId('id')->constrained('users')->cascadeOnDelete();


        });

    }


    /**

     * Reverse the migrations.

     */

    public function down(): void

    {

        Schema::dropIfExists('cases');

    }

};


  1. model

<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;


class Cases extends Model

{

    use HasFactory;

    protected $table = 'cases';

    protected $primaryKey = 'caseID';

}


Solution:

Try this.


Always put the data to the view.


  1. manage-case.php component

public function render()

{

    // Load the Livewire view with the current user's cases

    $cases = Cases:all();

    return view('livewire.manage-case',['cases'=>$cases]);

}


Suggested blogs: 

>Invoking Python script from scons and pass ARGLIST

>How to save python yaml and load a nested class?-Python

>How you can send email from a shared inbox in Python

>How to read frame from ffmpeg subprocess in Python?

>What are the steps to fix error while downloading PDF file- Python


Nisha Patel

Nisha Patel

Submit
0 Answers