无法在laravel中使用livewire和blade检索数据

q8l4jmvw  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(114)

我正在为用户创建一个提交病例的功能,我可以将病例保存到数据库中,但无法从数据库中检索。这是密码。

1.manage-case.php组件

<?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 = '';
    }
}

2.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>

3.retrieve-case.php组件

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

    
}

4.回收箱.刀片

<!-- 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>

我试着改变foreach循环,但它仍然不起作用,我希望它能够检索提交的案例,并使用foreach循环列出它们

5.迁移

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

6.型号

<?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';
}
t9aqgxwy

t9aqgxwy1#

试试这个.
始终将数据放入视图。
1.manage-case.php组件

public function render()
{
    // Load the Livewire view with the current user's cases
    $cases = Cases:all();
    return view('livewire.manage-case',['cases'=>$cases]);
}

相关问题