在编辑方法中使用存储库存储库为空

时间:2020-09-04 14:02:18

标签: laravel laravel-livewire

使用laravel 7 / livewire应用程序,我使用Repository进行了搜索,并获得了数据列表, 在挂载事件中,我分配了受保护的var $ FacilityRepository,它在渲染方法中可以正常工作

但是在编辑方法中它为空,并且出现错误:

Call to a member function getById() on null

用户单击“编辑链接”时

<?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. 如果我愿意

    类设施扩展了组件 { ... 公共$ 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. 我试图将方法编辑声明为:

    公共功能编辑(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>

当我模糊编辑某些字段时,出现了相同的错误:

Call to a member function filterWithPagination() on null

在错误说明中带有网址:

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",

谢谢!

1 个答案:

答案 0 :(得分:1)

受保护的私有属性在Livewire更新之间不存在。通常,应避免使用它们来存储状态。

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

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

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

更正

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

因此要在渲染中使用,请使用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
  }
}