0

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

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

Call to a member function getById() on null

当用户点击“编辑链接”时</p>

<?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';
    }

在模板编辑链接中定义为:

@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. 如果我做

    类设施扩展组件 { ... 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 function edit(FacilityRepositoryInterface $facilityRepository, int $id) { // 你是这个意思吗?... }

我得到错误:

Call to a member function filterWithPagination() on null

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

修改#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

错误描述中有 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",

谢谢!

4

1 回答 1

2

受保护和私有属性不会在 Livewire 更新之间持续存在。一般来说,您应该避免使用它们来存储状态。

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

话虽如此,您可以再次使用依赖注入,只需传递您需要的任何内容(FacilityRepositoryInterface在这种情况下)作为edit方法的第一个参数。

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

更正

我原始答案的最后一点是错误的,您不能在渲染方法中使用 DI。

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

最终代码,应该可以


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
  }
}
于 2020-09-04T20:37:34.320 回答