当前位置: 首页 > 知识库问答 >
问题:

调用数组上的成员函数link()

朱建弼
2023-03-14

我使用Laravel 8与Livewire。

我创建了这个来显示项目列表,用户也可以应用过滤器。在加载数据时,每页显示10条记录。然而,当我尝试单击下一页或特定的页码时,我得到错误调用成员函数链接()数组...

我也在使用存储库,它被注入了livewire组件。

use App\Models\Repositories\Interfaces\LojaAdesaoRepositoryInterface;
use Livewire\Component;
use Livewire\WithPagination;

This is my component

class Adesao extends Component
{
    use WithPagination;

    protected $repository;    
    public $cnpj;
    public $razaoSocial;
    public $nomeFantasia;
    public $regulamentacaoId;
    public $regiaoId;
    public $faturamentoId;
    private $filtros = [
        "cnpj" => "",
        "razao_social" => "",
        "nome_fantasia" =>  "",
        //"regulamentacao_id" =>  "",
        //"regiao_id" =>  "",
        //"faturamento_id" =>  ""
    ];

    private $lojas = [];
    
    protected $listeners = ['regiaoMudanca' => 'regiaoMudanca', 'faturamentoListener' => 'faturamentoListener', 'regulamentacaoListener' => 'regulamentacaoListener'];

    public function mount(LojaAdesaoRepositoryInterface $respository)
    {
        
        $this->repository = $respository;
        $lojas = $this->retornaListaInicial();
        $this->setLojas($lojas);
    }

    public function render(LojaAdesaoRepositoryInterface $respository)
    {   
        $this->repository = $respository;
        $lojas = $this->getLojas();
        
        return view('livewire.lojas.adesao',['lojasAdesao' => $lojas]);
    }

    public function regiaoMudanca($value){
        $this->filtros['regiao_id'] = $value;
    }

    public function faturamentoListener($value){
        $this->filtros['faturamento_id'] = $value;
    }

    public function regulamentacaoListener($value){
        
        $this->filtros['regulamentacao_id'] = $value;
    }

    public function submitForm(LojaAdesaoRepositoryInterface $respository){
        
        $this->resetPage();
        $this->repository = $respository;
        $this->filtros["cnpj"] = $this->cnpj;
        $this->filtros["razao_social"] = $this->razaoSocial;
        $this->filtros["nome_fantasia"] =  $this->nomeFantasia;
        
        $lojas = $this->repository->filterBy($this->filtros);
        $this->setLojas($lojas);
        
    }

    private function retornaListaInicial(){
        return $this->repository->filterBy([]);
    }

    private function setLojas($lojas){
        //dd($lojas->links());
        $this->lojas = $lojas;
    }

    private function getLojas(){
        return $this->lojas;
    }
}
<div>
<x-nav-bar.nav/>
<form wire:submit.prevent="submitForm()" method="get">
<div class="container mx-auto px-4 sm:px-8">
    <div class="flex justify-center pt-8 sm:justify-start sm:pt-0 text-gray-900 dark:text-white">
    LOJA ADESÃO
    </div>
    
        <div class="grid grid-cols-1 md:grid-cols-3 border-t border-b border-gray-200 dark:border-gray-700">
            
            <div class="p-3">
                <div class="flex items-center">
                    <label class="block font-semibold text-gray-900 dark:text-white">CNPJ<label>
                    <input type="text" wire:model.defer="cnpj" maxlength="15" placeholder="CNPJ" name="cnpj" id="cnpj" class="w-full h-4 px-3 py-5 mt-2 hover:outline-none focus:outline-none border-none focus:ring-1 bg-white text-sm dark:bg-gray-800  rounded-md">
                    
                </div>
            </div>
            <div class="p-3">
                <div class="flex items-center">
                    <label class="block font-semibold text-gray-900 dark:text-white">Razão Social<label>
                    <input type="text" wire:model.defer="razaoSocial" maxlength="60" placeholder="Razão Social" name="razaoSocial" id="razaoSocial" class="w-full h-4 px-3 py-5 mt-2 hover:outline-none focus:outline-none border-none focus:ring-1 bg-white text-sm dark:bg-gray-800 rounded-md">                
                </div>
            </div>
            <div class="p-3">
                <div class="flex items-center">
                    <label class="block font-semibold text-gray-900 dark:text-white">Nome Fantasia<label>
                    <input type="text" maxlength="60" wire:model.defer="nomeFantasia" placeholder="Nome Fantasia" name="nomeFantasia" id="nomeFantasia" class="w-full h-4 px-3 py-5 mt-2 hover:outline-none focus:outline-none border-none focus:ring-1 bg-white text-sm dark:bg-gray-800 rounded-md">
                </div>
            </div>
        
            <div class="p-3">
                <div class="flex items-center">
                    <label class="block font-semibold text-gray-900 dark:text-white">Regulamentação<label>
                    <livewire:dropdown-list.regulamentacao />
                </div>
            </div>
            <div class="p-3">
                <div class="flex items-center">
                    <label class="block font-semibold text-gray-900 dark:text-white">Faturamento<label>
                    <livewire:dropdown-list.faturamento />
                </div>
            </div>
            
            <div class="p-3">
                <div class="flex items-center">
                    <label class="block font-semibold text-gray-900 dark:text-white">Região<label>
                    <livewire:dropdown-list.regiao />
                </div>
            </div>
            <div class="p-3 col-start-3">                
                <button type="submit" class="text-gray-700 py-2 px-6 rounded-lg dark:text-gray-200 hover:bg-gray-300 dark:hover:bg-gray-700 focus:outline-none">Aplicar Filtro</button>
                <button type="reset" class="text-gray-700 py-2 px-6 rounded-lg dark:text-gray-200 hover:bg-gray-300 dark:hover:bg-gray-700 focus:outline-none">Limpar Filtros</button>
            </div>        
        </div>
    
</div>
</form>
<div class="container mx-auto px-4 sm:px-8">
    <div class="py-8">
        <div class="-mx-4 sm:-mx-8 px-4 sm:px-8 py-4 overflow-x-auto">
        <div class="inline-block min-w-full shadow-md rounded-lg overflow-hidden">
            <table class="min-w-full leading-normal border border-gray-200">
                <thead>
                    <tr>
                        <th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-500 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                            <input type="checkbox" name="all" id="all" class="rounded  appearance-none checked:bg-blue-600 checked:border-transparent">
                        </th>
                        <th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-500 text-white text-left text-xs font-semibold uppercase tracking-wider">
                            CNPJ
                        </th>
                        <th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-500 text-white text-left text-xs font-semibold uppercase tracking-wider">
                            Razão Social
                        </th>
                        <th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-500 text-white text-left text-xs font-semibold uppercase tracking-wider">
                            Nome Fantasia
                        </th>
                        <th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-500 text-white text-left text-xs font-semibold uppercase tracking-wider">
                            Regulamentação
                        </th>
                        <th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-500 text-white text-left text-xs font-semibold uppercase tracking-wider">
                            Região
                        </th>
                        <th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-500 text-white text-left text-xs font-semibold uppercase tracking-wider">
                            Faturamento
                        </th>
                        <th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-500 text-white text-center text-xs font-semibold uppercase tracking-wider">
                            Ação
                        </th>                        
                    </tr>
                </thead>
                <tbody>
                    @foreach ($lojasAdesao as $lojaAdesao)
                        <tr class="">
                            <td class="px-5 py-5 border-b border-gray-200 bg-white text-sm  dark:bg-gray-800">
                                <input type="checkbox" class="rounded  appearance-none checked:bg-blue-600 checked:border-transparent">
                            </td>
                            <td class="px-5 py-5 border-b border-gray-200 text-xs dark:bg-gray-800">
                                <p class="text-white whitespace-no-wrap">{{ $lojaAdesao->cnpj }}</p>
                            </td>
                            <td class="px-5 py-5 border-b border-gray-200 bg-white text-xs dark:bg-gray-800">
                                <p class="text-white whitespace-no-wrap">{{ $lojaAdesao->razao_social }}</p>
                            </td>
                            <td class="px-5 py-5 border-b border-gray-200 bg-white text-xs dark:bg-gray-800">
                                <p class="text-white whitespace-no-wrap">{{ $lojaAdesao->nome_fantasia }}</p>
                            </td>
                            <td class="px-5 py-5 border-b border-gray-200 bg-white text-xs dark:bg-gray-800">
                                <p class="text-white whitespace-no-wrap">{{ $lojaAdesao->regulamentacao->descricao }}</p>
                            </td>
                            <td class="px-5 py-5 border-b border-gray-200 bg-white text-xs dark:bg-gray-800">
                                <p class="text-white whitespace-no-wrap">{{ $lojaAdesao->endereco->estado->regiao[0]['descricao'] }}</p>
                            </td>
                            <td class="px-5 py-5 border-b border-gray-200 bg-white text-xs  dark:bg-gray-800">
                                <p class="text-white whitespace-no-wrap">{{ $lojaAdesao->faturamento->descricao }}</p>
                            </td>
                            <td class="px-5 py-5 border-b border-gray-200 bg-white text-center text-sm dark:bg-gray-800">
                                <button class="text-gray-700 dark:text-gray-200 hover:bg-gray-300 dark:hover:bg-gray-700 px-6 rounded-lg focus:outline-none">Atualizar</button>
                            </td>
                        </tr>
                    @endforeach
            </tbody>
        </table>        
        </div>
        {{$lojasAdesao->links()}}
    </div>
    </div>
</div>
</div>
<div class="container mx-auto px-4 sm:px-8">
    <div class="grid grid-cols-1 md:grid-cols-4 dark:border-gray-700">
        <div class="p-3">
            <div class="flex items-center">
                <button class="text-gray-700 py-2 px-6 rounded-lg dark:text-gray-200 hover:bg-gray-300 dark:hover:bg-gray-700 focus:outline-none">Selecionar Pesquisador(a)</button>
            </div>
        </div>
        <div class="p-3">
            <div class="flex items-center">
                <button class="text-gray-700 py-2 px-6 rounded-lg dark:text-gray-200 hover:bg-gray-300 dark:hover:bg-gray-700 focus:outline-none">Importar</button>
            </div>
        </div>
        
    </div>
</div>
</div>
use App\Models\Repositories\Interfaces\LojaAdesaoRepositoryInterface;
use App\Models\LojaAdesao;
use App\Models\Repositories\Base\BaseRepository;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\DB;

use function PHPUnit\Framework\isNull;

class LojaAdesaoRepository extends BaseRepository implements LojaAdesaoRepositoryInterface
{
    use HasFactory;
    protected $model = null;

    /**
    * UserRepository constructor.
    *
    * @param Campos $model
    */
    public function __construct(LojaAdesao $model)
    {
        parent::__construct($model);
        $this->model = $model;
    }

    public function filterBy(array $filtros, int $pageSize=10) {
        DB::enableQueryLog(); 
        $result= $this->model->where(function($q) use ($filtros){
                foreach($filtros as $key => $value)
                {                    
                    if (  $this->ehCampoLike($key) && !empty($value) ){
                        $q->where($key, 'like', '%' . $value . '%');
                    }
                    else{
                        if (!empty($value)  ){
                            $q->where($key,'=', $value);
                        }
                    }
                }
            })->orderBy('cnpj', 'asc')->paginate($pageSize);

            //dd(DB::getQueryLog());
            return $result;
    }

    private function ehCampoLike(string $filtro): bool{
        if ( $filtro == "cnpj" || $filtro == "razao_social" || $filtro == "nome_fantasia"){
            return true;
        }
        return false;
    }
}

共有1个答案

东深
2023-03-14

简短回答

将其从mount移动到render方法

$lojas = $this->retornaListaInicial();
$this->setLojas($lojas);

解释

请注意这两种方法mountrender,虽然mount在组件启动时调用一次,但render在每次服务器调用时都调用。因此,当您更改页面时,只调用render,并且$lojas=$this-

    public function mount(LojaAdesaoRepositoryInterface $respository)
    {
        
        $this->repository = $respository;
        $lojas = $this->retornaListaInicial();
        $this->setLojas($lojas);
    }

    public function render(LojaAdesaoRepositoryInterface $respository)
    {   
        $this->repository = $respository;
        $lojas = $this->getLojas();
        
        return view('livewire.lojas.adesao',['lojasAdesao' => $lojas]);
    }

 类似资料:
  • 本文向大家介绍浅析成员函数和常成员函数的调用,包括了浅析成员函数和常成员函数的调用的使用技巧和注意事项,需要的朋友参考一下 在Coordinate类中,有一个Display()成员函数和一个Display() const常成员函数,代码如下 Display()成员函数和一个Display() const常成员函数是互为重载的,那么如果我们直接像下面这样调用该方法,会调用的是哪个呢? 那么运行下程序

  • 问题内容: 因此,我正在重构代码以实现更多的OOP。我设置了一个类来保存页面属性。 稍后,我像这样调用set_page_title()函数 当我收到错误消息时: 调用非对象上的成员函数set_page_title() 那我想念什么呢? 问题答案: 这意味着它不是对象的实例。我们可以看到您用来初始化变量的代码吗? 当您期望特定的对象类型时,还可以使用PHP的类型提示功能 Docs 在违反逻辑时得到错

  • 问题内容: 我的查询有问题。我最近改用了PDO,这对我来说是新的。现在,我需要在查询中使用JOIN来从另一个表中获取数据,而无需执行两次执行。 问题是,我收到此错误: 我不明白问题是什么。这是我的代码的样子: 或简称为查询: 我想要的是,从用户ID为session且请求为0的朋友那里获取所有行。但是然后我想获取用户中那些朋友的ID,以便我可以链接到他们的个人资料。注意 感谢您提供任何反馈意见,不必

  • 我想创建一个函数来检查数据库中是否已存在用户。该函数存储在名为 checks 的 php 文件中并且必须返回一个值。 我的数据库.php文件: 我在另一个名为 的 php 文件中调用 错误出现在这个句子上:$sth=$con- 我在< code>checks.php文件中试过: < li >将代码从< code>database.php移动到checks.php < li >设置< code >错

  • 函数attach()有问题。我已经创建了3个表。1表用于添加电影。表2适用于电影类别。3表是电影ID和类别ID的集合。多对多的组合。 当我想添加一个新的视频类别从表格这我有错误- 视频控制器。php- 视频php模型 类别php模型 User.php模型(也许你需要一个User.php模型解决问题) 迁移-创建\u类别\u视频\u表。php 迁移-创建类别表。php 移民-create_video

  • 问题内容: 我正在尝试调用文字函数,但出现奇怪的行为。 考虑下面返回的代码。 当我写的时候尝试以下方法。 我得到了预期的结果,但是当我尝试时出现此错误。 语法错误:意外的令牌非法 JavaScript如何评估无法理解的表达式,为什么会出现此错误? 问题答案: DecimalLiteral :: DecimalIntegerLiteral . DecimalDigits(opt) ExponentP