在编辑方法 Repository 中使用 Repository 为空

Using Repository in edit method Repository is null

使用 laravel 7 /livewire 应用程序,我使用 Repository 制作了 crud,我得到了数据列表,好的, 在 mount 事件中,我分配了 protected var $FacilityRepository ,它在 render 方法中工作正常,

但它在编辑方法中为空,我得到错误:

Call to a member function getById() on null

当用户点击“编辑 link”

<?php

namespace App\Http\Livewire\Admin;

use App\library\CheckValueType;
use App\Settings;
use DB;
use Livewire\Component;
use App\Facility;
use Livewire\WithPagination;
use App\Repositories\Interfaces\FacilityRepositoryInterface;


class Facilities extends Component
{
    use WithPagination;

    public $form= [
        'name'=>'',
        'descr'=> '',
        'created_at'=> '',
        'is_reopen'       => false,
    ];

    public $current_facility_id;
    public $filter_name= '';
    public $updateMode = 'browse';

    protected $FacilityRepository;
    public function render()
    {
        $this->facility_rows_count = Facility
            ::getByName($this->filter_name, true)
            ->count();
        $backend_per_page = Settings::getValue('backend_per_page', CheckValueType::cvtInteger, 20);
        \Log::info( 'render -10 $this->FacilityRepository::' . print_r(  json_encode($this->FacilityRepository), true  ) );
        // line above logged as : [2020-09-04 16:46:26] local.INFO: render -10 $this->FacilityRepository::{}

        return view('livewire.admin.facilities.container', [
            'facilityDataRows' => $this->FacilityRepository->filterWithPagination(
                [
                    'name'=>$this->filter_name,
                ],
                $backend_per_page
            ),
            'facility_rows_count'=> $this->facility_rows_count
        ]); // this listing is rendered OK
    }


    public function mount(  FacilityRepositoryInterface $FacilityRepository  ) {
        $this->FacilityRepository = $FacilityRepository;
        \Log::info( '-101mount $this->FacilityRepository::' . print_r(  json_encode($this->FacilityRepository), true  ) );
        // line above logged as : [2020-09-04 16:46:26] local.INFO: -101mount $this->FacilityRepository::{}
    }


    public function edit($id)
    {
        \Log::info( '-1 edit $id::' . print_r(  json_encode( $id ), true  ) );
        \Log::info( '-1 edit $this->FacilityRepository::' . print_r( $this->FacilityRepository, true  ) );
        // line above logged as : [2020-09-04 16:46:28] local.INFO: -1 edit $this->FacilityRepository::
        // AND ERROR NEXT
        $this->form = $this->FacilityRepository->getById($id)->toArray();
        \Log::info( '-1023 $this->form ::' . print_r(  json_encode( $this->form ), true  ) );


        $this->current_facility_id = $id;
        $this->form['created_at'] = getCFFormattedDateTime($this->form['created_at']);
        $this->emit('facility_opened',[ 'mode'=>'edit', 'id'=>$id ]);
        $this->updateMode = 'edit';
    }

在模板编辑中 link 定义为:

@foreach($facilityDataRows as $nextFacilityDataRow)
    <tr>
        <td class="text-right m-0">
            <a wire:click="edit({{$nextFacilityDataRow->id}})"
                class="p-1 a_edit_item_{{$nextFacilityDataRow->id}} a_link">
                {{$nextFacilityDataRow->id}}
            </a>
        </td>

...

为什么会出错以及如何解决?

修改#2:

  1. 如果我做

    class 设施扩展组件 { ... public $FacilityRepository; }

我收到错误:

Livewire component's [admin.facilities] public property [FacilityRepository] must be of type: [numeric, string, array, null, or boolean]. Only protected or private properties can be set as other types because JavaScript doesn't need to access them.
  1. 我试图将方法编辑声明为:

    public 函数编辑(FacilityRepositoryInterface $facilityRepository,int $id) { // 你是这个意思吗? ... }

我收到错误:

Call to a member function filterWithPagination() on null

on method filterWithPagination,当我显示数据列表时,它在 render 方法中使用。 哪种方式是正确的?

修改 #3: 如果要修改:

public function render(FacilityRepositoryInterface $facilityRepository)
{

我收到错误:

Declaration of App\Http\Livewire\Admin\Facilities::render(App\Repositories\Interfaces\FacilityRepositoryInterface $facilityRepository) should be compatible with Livewire\Component::render()

?

修改#4: 在模式下打开页面我有 2 个定义为惰性的输入,比如

            <dd class="horiz_divider_right_23" wire:model="form.title.lazy" x-data="{ name: '{{$form['name']}}'}">
                
                <input
                    x-model="name"
                    x-on:blur="$dispatch('name', name)"
                    id="name"
                    class="form-control editable_field admin_control_input"
                    placeholder="Enter descriptive name"
                    autocomplete=off
                >
                @error('form.name')
                <div class="validation_error">{{ clearValidationError($message,['form.'=>'']) }}</div> @enderror
            </dd>

当我在 blur ervent 上编辑一些字段时,我遇到了同样的错误:

Call to a member function filterWithPagination() on null

with url 错误描述:

VM5783:1 POST http://local-hostels3.com/livewire/message/admin.facilities 500 (Internal Server Error)

其中 http://local-hostels3.com 是我的本地主机

我是否以某种方式重写消息方法?

"laravel/framework": "^7.0",
"livewire/livewire": "^1.3",

谢谢!

protected and private properties DO NOT persist between Livewire updates. In general, you should avoid using them for storing state.

https://laravel-livewire.com/docs/properties/#important-notes

也就是说,您可以再次使用依赖注入,只需传递您需要的任何内容(在本例中为 FacilityRepositoryInterface)作为 edit 方法的第一个参数。

同样适用于 render 方法,因此您可以完全跳过 mount

更正

我原回答的最后一点是错误的,你不能在render方法中使用DI。

所以在render中使用,使用mount方法,在edit中使用,通过第一个参数引入。如果渲染在使用编辑后抱怨没有它,请保存到受保护的 属性 内部编辑。

最终代码,应该可以工作


class Facilities extends Component
{
  protected $FacilityRepository;

  public function mount(FacilityRepositoryInterface $FacilityRepository)
  {
    $this->FacilityRepository = $FacilityRepository;
  }

  public function render()
  {
    // use $this->FacilityRepository->...
  }

  public function edit(FacilityRepositoryInterface $FacilityRepository, $id)
  {
    $this->FacilityRepository = $FacilityRepository;
    // rest of the edit method from your code
  }
}

添加开机方式

public function boot(FacilityRepositoryInterface $FacilityRepository)
{
    $this->FacilityRepository = $FacilityRepository;
}