laravel Livewire wire:click未触发该函数。
laravel Livewire wire:click未触发该函数。
我想用laravel livewire做一个SPA,我想使用wire:click来触发组件中的一个函数,但是它不起作用,如果代码混乱,对不起,这是我第一次在这里发布,我不确定应该发布什么样的代码来解决这些问题,谢谢。
main.blade.php
@section('content')Categories
Add NewCategory @include('admin.includes.alerts.errors') @include('admin.includes.alerts.success') @if ($showCreateForm) @livewire('admin.category.create') @endif
ID | Image | Name | Slug | Status | Parent | Action |
---|---|---|---|---|---|---|
{{$category->id}} | {{--{{storage_path(app\livewire-tmp\$category->image)}}" /> | --}}{{$category->name}} | {{$category->slug}} | {{$category->isActive()}} | {{ !empty($category->parent) ? $category->parent->name:'' }} | Edit Delete |
ID | Image | Name | Slug | Status | Parent | Action |
The Component Main.php
paginate(12); return view('livewire.admin.category.main',[ 'categories' => $categories, ]) ->layout('layouts.admin'); } public function createCategory() { $this->showCreateForm = !$this->showCreateForm; } public function update_Category($id) { $categories = Category::whereId($id); if ($categories) { $this->emit('getCategoryid' , $categories); $this->showEditForm = !$this->showEditForm; $this->showCreateForm = false; } } public function delete_Category($id) { $this->showCreateForm = !$this->showCreateForm; } }
- //// Update ////
i tried iRestWeb Answer, I think it the right answer but i dont even understand what happening its 100% javascript related and its not my field of expertise , so here's my full code i hope some one understand , and again sorry if my code messy and give you hard time , thank youu.
create.blade.php
Create.php
'required|unique:categories,slug', 'name' => 'required', 'image'=> 'nullable|image|max:1024' ]; protected $categories; public function render() { $categories = Category::orderBy('id','desc')->paginate(12); return view('livewire.admin.category.create' , [ 'categories' => $categories, ]); } public function create() { $this->validate(); $data = [ 'name' => $this->name, 'slug' => $this->slug, 'is_active'=> $this->is_active, 'image'=> $this->image, 'parent_id'=> $this->parent_id, ]; //image upload try { if ($image = $this->image) { $filename = Str::slug($this->name).'.'.$image->getClientOriginalExtension(); $path = public_path('assets/image/'.$filename); Image::make($image->getRealPath())->save($path,100); } Category::create($data); $this->reset(); return $this->addError('success' , 'Created Successfuly'); } catch (\Throwable $th) { return $this->addError('error', 'Something Wrong Happens'); } } }
edit.blade.php
Edit.php (uncompleted Task)
'getID']; public function mount() { $this->categories = Category::whereId($this->cat_id)->first(); } public function render() { $categories = Category::all(); return view('livewire.admin.category.edit' , [ 'categories' => $categories, ]); } public function update($id) { } public function getID($categories) { $this->categories = $categories; // Data $this->slug = $this->$categories['slug']; $this->name = $this->$categories['name']; $this->image = $this->$categories['image']; $this->old_image = $this->$categories['old_image']; $this->parent_id = $this->$categories['parent_id']; $this->is_active = $this->$categories['is_active']; } }