Laravel Livewire组件出现Using $this when not in object context错误求解决方案
解决Laravel Livewire「Using $this when not in object context」错误
问题场景
为在项目仪表盘按分类展示产品汇总信息,我在dashboardinfo目录下创建了Livewire组件CatwiseSummaryComponent,组件代码如下:
<?php namespace App\Http\Livewire\Dashboardinfo; use Livewire\Component; use Livewire\WithPagination; class CatwiseSummaryComponent extends Component { protected $paginationTheme = 'bootstrap'; use WithPagination; $title = "Categorywise Summary"; return view('livewire.counter',\[ 'catwiseproducts' => DB::table('products') ->join('product_categories', 'products.product_categories_id', '=', 'product_categories.id')->select( 'product_categories.id', 'product_categories.catName as Products', DB::raw('count(products.product_categories_id) as Total'), DB::raw('sum(products.unitprice) as Totalprice')) ->groupBy('products.product_categories_id') ->orderBy('Products','asc') ->paginate(7),'title'=>$title \]); } }
同时编写了组件视图文件catwise-summary-component.blade.php:
<div class="table-responsive"> <table class="table align-middle mb-0"> <tr> <th>Product Category</th> <th></th> <th></th> <th></th> <th>Total Number</th> <th>Total Price</th> </tr> <tbody> @foreach($catwiseproducts as $prod) <tr> <td> {{$prod->Products}} </td> <td></td> <td></td> <td></td> <td class="fw-bold text-pink">{{$prod->Total}}</td> <td class="fw-bold text-secondary">{{number_format($prod->Totalprice,2)}}</td> </tr> @endforeach </tbody> </table> <p>{{$catwiseproducts->links()}}</p> </div>
通过<livewire:dashboardinfo.catwise-summary-component/>调用组件时,出现错误:
Using $this when not in object context (View: C:\xampp\htdocs\assetgate\resources\views\vendor\livewire\bootstrap.blade.php)
解决方案
错误根源是组件类的代码结构不符合Livewire规范,以下是修正步骤:
1. 将逻辑代码放入render方法
Livewire组件的视图渲染和数据查询逻辑必须放在render方法内,不能直接写在类的全局作用域下。
2. 引入DB门面
使用DB类前需要引入对应的门面。
3. 修正视图路径
组件视图文件是catwise-summary-component.blade.php,返回视图时应使用正确的路径livewire.dashboardinfo.catwise-summary-component,而非错误的livewire.counter。
修正后的组件代码
<?php namespace App\Http\Livewire\Dashboardinfo; use Livewire\Component; use Livewire\WithPagination; use Illuminate\Support\Facades\DB; // 引入DB门面 class CatwiseSummaryComponent extends Component { protected $paginationTheme = 'bootstrap'; use WithPagination; // 组件的核心渲染方法 public function render() { $title = "Categorywise Summary"; return view('livewire.dashboardinfo.catwise-summary-component', [ 'catwiseproducts' => DB::table('products') ->join('product_categories', 'products.product_categories_id', '=', 'product_categories.id') ->select( 'product_categories.id', 'product_categories.catName as Products', DB::raw('count(products.product_categories_id) as Total'), DB::raw('sum(products.unitprice) as Totalprice') ) ->groupBy('products.product_categories_id', 'product_categories.id', 'product_categories.catName') // 补充groupBy字段,避免严格模式报错 ->orderBy('Products','asc') ->paginate(7), 'title' => $title ]); } }
额外优化(可选)
- 视图中的空
<th>可以删除,优化表格结构; - 如果开启了MySQL严格模式,
groupBy需要包含select中的所有非聚合字段,因此补充了product_categories.id和product_categories.catName到groupBy中,避免SQL报错。
内容的提问来源于stack exchange,提问作者Karmasangsthan Bank
相关产品推荐
相关产品推荐

