所以,我目前在我的一个项目中使用Laravel Livewire。但是,当我尝试通过 magic mathod 从一个组件向另一个组件发出事件时$refresh,它刷新了(在 xhr 请求中获得了 dom)整个组件,但前端没有实时更新。
ProductReviewForm.php 成分:
public function save()
{
//some functionalities ....
$this->emit('reviewSectionRefresh');
}
Run Code Online (Sandbox Code Playgroud)
ProductReviewSection.php 成分:
protected $listeners = [
'reviewSectionRefresh' => '$refresh',
];
public function render()
{
$this->review_lists = ProductReview::orderBy('id', 'DESC')->get();
return view('livewire.desktop.product-review-section');
}
Run Code Online (Sandbox Code Playgroud)
所以我想reviewSectionRefresh在我save()从第一个组件调用函数时发出要发出的事件,这应该被$listeners其他组件监听。这工作正常。同样在 xhr 中,我得到了刷新的 dom,但前端的组件没有更新。希望任何与 Livewire 合作的人都可以对此有所帮助。
我尝试使用 Livewire 直接绑定到模型属性,但收到此错误,有人可以帮忙吗?预先感谢。
初始化之前不得访问类型化属性 App\Http\Livewire\V2\Settings\Locations::$country
class Locations extends Component{
public Country $country;
use WithPagination;
protected $rules = [
'country.name' => 'required|min:100',
'country.note' => 'required|min:100',
];
public function SaveCountry(){
$this->validate();
$this->country->save();
$this->reset();
session()->flash('info','The country have been added successful.');
}
public function render(){
return view('livewire.v2.settings.locations',[
'countries' => Country::orderBy('order_num','desc')->paginate(10),
]);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一张桌子products并job_statuses使用这个包:laravel-job-status
队列作业完成后,有一个job_statuses名为statuspackage made this column 的列finished!
所以我在products表中创建了一个名为job_status_id(relation with job_statuses)的列,只是为了将作业状态 ID 保存在产品表中,以查看此作业是否完成!
简单地,我创建了一个组件,Livewire用于在作业完成后刷新组件时刷新单个产品:
class ProductIndex extends Component
{
public $product;
public function mount($product)
{
$this->product = $product;
}
public function render()
{
return view('livewire.merchant.product.index');
}
}
Run Code Online (Sandbox Code Playgroud)
内部product-index组件:
@if ($product->job_status->status == 'finished')
// show real image?
@else
// show loader
@endif
Run Code Online (Sandbox Code Playgroud)
我的刀片:
@foreach ($products as $product)
<livewire:merchant.product.product-index :product="$product" :key="$product->id">
@endforeach
Run Code Online (Sandbox Code Playgroud)
如果状态已完成,如何刷新产品组件?
我无法使用 laravel-livewire 在 Laravel 8 路由上运行我的代码。
班级在Livewire\LandingPage.
我得到的错误是
属性 [livewire] 不存在
这是我的路线
<?php
use Illuminate\Support\Facades\Route;
Route::livewire('/' , 'LandingPage');
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Run Code Online (Sandbox Code Playgroud) 我将Livewire和Alpine与Laravel 8一起使用。
我有一个带有数据表(jQuery)和引导模式的页面。
该表填充了模型实例列表中的一些数据。
当我单击表中的按钮时,它会打开模式并允许编辑相应的记录。
这部分正在按预期工作。
然而,Datatable库与 Livewire 一起使用要困难得多,因此我决定将整个Datatable的范围限制在<div>withwire:ignore属性中。
因此,如果我想在进行修改后刷新数据表$refresh,我无法使用魔法,因为数据表当前的范围位于wire:ignore.
所以我正在考虑在编辑完成后重新加载整页,但我不知道如何做到这一点,return redirect()->back()不起作用......
这就是我的save方法,当模态编辑完成时调用该方法:
public function save()
{
MyModel::where('id', $this->edited_id)->update([...]);
$this->clearSelection();
return redirect()->back(); // This is not working
}
Run Code Online (Sandbox Code Playgroud)
这是我的桌子:
<div wire:ignore>
<table class="dt-table table">
<thead>
<tr>
<th>Actions</th>
<th>id</th>
<th>name</th>
<th>other</th>
</tr>
</thead>
<tbody>
@foreach ($models as $m)
<tr>
<td>
<button type="button" class="btn btn-primary" data-toggle="modal" …Run Code Online (Sandbox Code Playgroud) 我有一段代码想要重用。我读过这篇Laravel 清洁代码文章和另一篇Laravel 服务模式文章,其中我意识到可以通过使用服务类在应用程序的多个位置重用代码。
在本例中,我MyService在新文件夹中创建了一个新类app/Services/MyService。
namespace App\Services;
class MyService
{
public function reuse_code($param){
return void;
}
}
Run Code Online (Sandbox Code Playgroud)
当我想通过 Livewire 类组件内的构造函数调用该类时,问题就出现了,如下所示:
<?php
namespace App\Http\Livewire;
use App\Services\MyService;
use Livewire\Component;
use Livewire\WithPagination;
class LivewireTable extends Component
{
use WithPagination;
private $myClassService;
public function __construct(MyService $myService)
{
$this->myClassService = $myService;
}
public function render()
{
$foo = $this->myClassService->reuse_code($param);
return view('my.view',compact('foo'));
}
}
Run Code Online (Sandbox Code Playgroud)
显示的错误如下:
传递给 App\Http\Livewire\LivewireTable::__construct() 的参数 1 必须是 App\Services\MyService 的实例,给定字符串
(但是,如果我使用一个特质,就没有问题。但我担心我的特质会与以前的经验发生冲突)
我如何解决它?我缺少什么?
我有两个组件 Posts 和 Post,Posts 显示帖子,通过单击图像,我想在另一个组件中显示单击的帖子的数据。
在下面发布类和组件:
组件视图:
<div class="post" x-data="{open:false}">
@foreach($posts as $post)
<div>
<h1>{{ $post->name }}</h1>
<h3>{{ $post->body }}</h3>
<img @click="open = !open" wire:click="showPost({{ $post->id }})" src="{{ $post->image }}" alt="">
</div>
@endforeach
<livewireL:post>
</div>
Run Code Online (Sandbox Code Playgroud)
组件类:
class Posts extends Component
{
public $posts, $post;
public function mount(){
$this->posts = Post::all();
}
public function showPost($id){
$post = Post::find($id);
$this->post = $post;
}
public function render()
{
return view('livewire.posts');
}
}
Run Code Online (Sandbox Code Playgroud)
这是我想在这个组件中显示点击数据的Post 组件和类,我已经尝试过 $emit 和许多文档,但没有结果。
我要呈现该数据的组件视图:
<div x-show="open">
<h1>{{ $post->name …Run Code Online (Sandbox Code Playgroud) 我有 2 个 livewire 组件,第一个只显示购物车的会话变量,第二个只是在购物车中添加项目(带有 sku、标题、价格和数量的非常原始的表单)。
<livewire:shoppingcart :cart="$CartId">
<livewire:order-add-product-form :orderAddProductCartId="$CartId">
Run Code Online (Sandbox Code Playgroud)
这两个组件本身都运行良好。但是当我从第二个组件添加项目时,它会更新购物车会话变量,但视图永远不会更新。我必须刷新页面才能在购物车视图中查看会话变量。
是否可以将两个组件连接在一起。那么,当我从一个组件在购物车中添加项目时,它会自动更新其他组件的视图吗?
谢谢
我在我的项目中使用 Laravel Livewire,我wire:loading用于在单击时加载状态。我在 foreach 循环中迭代了所有任务,但加载状态适用于所有组件。这是代码。
刀片文件
GitLab:https ://gitlab.com/tasklog/tasklog/-/blob/master/resources/views/livewire/home/tasks.blade.php
<button type="button" wire:click="togglePraise({{ $task->id }}, {{ $task->user->id }})">
<span class="small text-black-50 font-weight-bold">
{{ $task->task_praise->count() }}
</span>
<div wire:loading wire:target="togglePraise">
Processing...
</div>
</button>
Run Code Online (Sandbox Code Playgroud)
控制器文件
GitLab:https ://gitlab.com/tasklog/tasklog/-/blob/master/app/Http/Livewire/Home/Tasks.php
public function togglePraise($id, $user_id)
{
if (Auth::check()) {
if (Auth::user()->id === $user_id) {
session()->flash('message', 'Forbidden!');
return;
}
$isPraised = TaskPraise::where([
['user_id', Auth::user()->id],
['task_id', $id],
])->count();
if ($isPraised === 1) {
TaskPraise::where([
['user_id', Auth::user()->id],
['task_id', $id],
])->delete();
return true;
} else {
TaskPraise::create([
'task_id' …Run Code Online (Sandbox Code Playgroud) 我在本地的 Mac 上开发。大苏尔的最新版本。
今天,我通过 Forge 通过 Ubuntu 服务器将我的应用程序部署到生产环境中,但遇到了一个我以前从未见过的错误,并且无法在网上找到答案。我可以看到很多人在抱怨它,但是任何人在其他答案中所说的都是指向没有解决方案甚至没有解释的问题的链接,所以这就是我提出一个新问题的原因。
确切的错误是这样的;
Unable to locate a class or view for component [layouts.base]. (View: /home/forge/default/releases/20201204084441/resources/views/layouts/app.blade.php)
在我的应用程序中,我有;
app\View\Components\Layouts\App.php
看起来像这样;
<?php
namespace App\View\Components\Layouts;
use Illuminate\View\Component;
class App extends Component
{
public function render()
{
return view('layouts.app');
}
}
Run Code Online (Sandbox Code Playgroud)
那我也有;
resources\views\layouts\app.blade.php
<x-layouts.base>
<!-- contents -->
</x-layouts.base>
Run Code Online (Sandbox Code Playgroud)
(也几乎相同的基础)
在 Mac 上完美运行。一旦我在 Ubuntu 上部署它,我就会收到上面的错误,即“无法找到具有这些名称的类或视图”。
有人可以指导我如何解决这个问题,因为到目前为止我完全不知道,尽管知道区分大小写可能是其他问题的问题,但我找不到任何实际的解决方案或方法来解决这个。