Php Laravel-如何将数据变量从2个独立的livewire组件控制器发布到单个刀片文件中,以实现CRUD功能?
我想将在App/Http/Livewire目录中的Php Laravel-如何将数据变量从2个独立的livewire组件控制器发布到单个刀片文件中,以实现CRUD功能?,php,laravel,laravel-8,Php,Laravel,Laravel 8,我想将在App/Http/Livewire目录中的MenuItemList.php和SubCategoryList.php中启动的变量发布到单个刀片文件,即菜单项.blade.php。因此,我可以从单个视图,即menu item.blade.php,进一步使用上述表的CRUD功能,即菜单项和子类别 当我用不同的视图在不同的路径上运行它时,它的工作是完美的,但是当我尝试将它们组合成一个并尝试在单个刀片文件中查看所有内容时,它会给我各种各样的错误。这是我的源代码 App\Models\MenuIte
MenuItemList.php
和SubCategoryList.php
中启动的变量发布到单个刀片文件,即菜单项.blade.php
。因此,我可以从单个视图,即menu item.blade.php
,进一步使用上述表的CRUD功能,即菜单项
和子类别
当我用不同的视图在不同的路径上运行它时,它的工作是完美的,但是当我尝试将它们组合成一个并尝试在单个刀片文件中查看所有内容时,它会给我各种各样的错误。这是我的源代码
App\Models\MenuItem.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class MenuItem extends Model
{
use HasFactory;
protected $table = "menu_items";
protected $fillable = ['sub_category_id', 'item_name', 'item_description'];
}
你能分享menu-item.blade.php标记吗?@najmusakib Mate,我也为上述文件添加了源代码。
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use Livewire\WithPagination;
use App\Models\MenuItem;
class MenuItemList extends Component
{
use WithPagination;
public $search;
public $itemId,$sub_category_id,$item_name,$item_description;
public $isOpen = 0;
public function render()
{
$searchParams = '%'.$this->search.'%';
return view('livewire.menu-item', [
'menuitemlist' => MenuItem::where('item_name','like', $searchParams)->latest()->paginate(5)
]);
}
public function showModal() {
$this->isOpen = true;
}
public function hideModal() {
$this->isOpen = false;
}
public function store(){
$this->validate(
[
'sub_category_id' => 'required',
'item_name' => 'required',
]
);
MenuItem::updateOrCreate(['id' => $this->itemId], [
'sub_category_id' => $this->sub_category_id,
'item_name' => $this->item_name,
'item_description' => $this->item_description
]);
$this->hideModal();
session()->flash('info', $this->itemId ? 'Post Update Successfully' : 'Post Created Successfully' );
$this->itemId = '';
$this->sub_category_id = '';
$this->item_name = '';
$this->item_description = '';
}
public function edit($id){
$menuitem = MenuItem::findOrFail($id);
$this->itemId = $id;
$this->sub_category_id = $menuitem->sub_category_id;
$this->item_name = $menuitem->item_name;
$this->item_description = $menuitem->item_description;
$this->showModal();
}
public function delete($id){
MenuItem::find($id)->delete();
session()->flash('delete','Post Successfully Deleted');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class SubCategory extends Model
{
use HasFactory;
protected $table = "sub_categories";
protected $fillable = ['category_id', 'sub_category_name'];
}
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use Livewire\WithPagination;
use App\Models\SubCategory;
class SubCategoryList extends Component
{
use WithPagination;
public $search;
public $itemId,$category_id,$sub_category_name;
public $isOpen = 0;
public function render()
{
$searchParams = '%'.$this->search.'%';
return view('livewire.sub-category', [
'subcategorylist' => SubCategory::where('sub_category_name','like', $searchParams)->latest()->paginate(5)
]);
}
public function showModal() {
$this->isOpen = true;
}
public function hideModal() {
$this->isOpen = false;
}
public function store(){
$this->validate(
[
'category_id' => 'required',
'sub_category_name' => 'required',
]
);
SubCategory::updateOrCreate(['id' => $this->itemId], [
'category_id' => $this->category_id,
'sub_category_name' => $this->sub_category_name
]);
$this->hideModal();
session()->flash('info', $this->itemId ? 'Post Update Successfully' : 'Post Created Successfully' );
$this->itemId = '';
$this->category_id = '';
$this->sub_category_name = '';
}
public function edit($id){
$subcategory = SubCategory::findOrFail($id);
$this->itemId = $id;
$this->category_id = $subcategory->category_id;
$this->sub_category_name = $subcategory->sub_category_name;
$this->showModal();
}
public function delete($id){
SubCategory::find($id)->delete();
session()->flash('delete','Post Successfully Deleted');
}
}
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Livewire\MenuItemList;
use App\Http\Livewire\SubCategoryList;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/menu-item', MenuItemlist::class);
Route::get('/sub-category', SubCategorylist::class);
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
<x-slot name="header">
<h2 class="text-xl font-semibold leading-tight text-gray-800">
Manage Menu Item
</h2>
</x-slot>
<div class="py-12">
<div class="mx-auto max-w-5xl sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow sm:rounded-lg px-4 py-4 my-8 text-sm">
<div class="flex mb-4">
<div class="w-full md:w-1/2 px-3 mb-6 md:mb-0">
<button wire:click="showModal()" class="px-4 py-2 my-3 font-bold text-white bg-blue-500 rounded hover:bg-blue-700 focus:outline-none">Create Menu Item</button>
</div>
<div class="w-full md:w-1/2 px-3 mb-6 md:mb-0">
<input wire:model="search" type="text" class="shadow appearance-none border rounded w-full py-2 px-2 my-3 text-blue-900 focus:outline-none" placeholder="Search Post...">
</div>
</div>
@if($isOpen)
@include('livewire.create')
@endif
@if(session()->has('info'))
<div class="bg-green-500 border-2 border-green-600 rounded-b mb-2 py-3 px-3">
<div>
<h1 class="text-white font-bold">{{ session('info') }}</h1>
</div>
</div>
@endif
@if(session()->has('delete'))
<div class="bg-red-500 border-2 border-red-600 rounded-b mb-2 py-3 px-3">
<div>
<h1 class="text-white font-bold">{{ session('delete') }}</h1>
</div>
</div>
@endif
<table class="w-full table-fixed">
<thead>
<tr class="bg-gray-100">
<th class="w-20 px-4 py-2">Sr. No.</th>
<th class="px-4 py-2">Sub-Category ID</th>
<th class="px-4 py-2">Item Name</th>
<th class="px-4 py-2">Item Description</th>
<th class="px-4 py-2">Action</th>
</tr>
</thead>
<tbody>
@foreach($menuitemlist as $key=>$menuitem)
<tr style="background-color: {{ $key % 2 == 0 ? '#fdfdfd': '#f5f5f5' }};">
<td class="px-4 py-2">{{ $menuitemlist->firstitem() + $key }}</td>
<td class="px-4 py-2">{{ $menuitem->sub_category_id }}</td>
<td class="px-4 py-2">{{ $menuitem->item_name }}</td>
<td class="px-4 py-2">{{ $menuitem->item_description }}</td>
<td class="px-4 py-2">
<button wire:click="edit({{ $menuitem->id }})" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-1 px-4 rounded">
Edit
</button>
<button wire:click="delete({{ $menuitem->id }})" class="bg-red-500 hover:bg-red-700 text-white font-bold py-1 px-4 rounded">
Delete
</button>
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="mt-4">
{{$menuitemlist->links()}}
</div>
</div>
</div>
</div>