未定义属性:Illuminate \ Database \ MySqlConnection-查询保留参数,但是出现此错误。怎么了

问题描述 投票:0回答:1

我正在使用Laravel以便在屏幕上存储和打印一组产品。

下面我将显示控制器和视图的代码。

我有控制器,因此将视图称为“产品”。

public function filter(Request $request)
{

    //
    $nome = $request->input('nome');
    $prods = DB::table('produtos')->select('produtos.id AS id', 'produtos.nome as nome',
    'produtos.preco AS preco', 'produtos.numero_nf AS numero_nf', 'produtos.cc AS cc')->where('produtos.nome','=',$nome);
    return view('/produtos', compact('prods'));
}

查看产品是:

@extends('layout.app', ["current" => "produtos"])

@section('body')

<div class="container">
<div class="card-border">
    <div class="card-body">
        <h3 class="card-title">Produtos Cadastrados</h3>

<br><br>


<div class="card-body">
                    <form method="POST" action="/filtrar_produtos">
                        @csrf

                            <div class="form-group row">

                            <div style="padding-left: 15px">
                                <input style="height: 35px" id="filtro" type="filtro" class="col-md-4 text-md-right " name="filtro" required autocomplete="filtro" autofocus>
                            </div>

                                <div class="col-md-6 offset-md-4">


                                    <button style="height: 35px" type="submit" class="btn btn-primary">
                                        {{ __('Filtrar') }}
                                    </button>

                                </div>
                            </div>
                    </form>
                </div>





        <table class="table table-striped table-bordered table-hover">
                    <tr>
                        <th> Id
                        </th>
                        <th>
                            Nome
                        </th>
                        <th>
                            Número da NF
                        </th>
                        <th>
                            Preço
                        </th>
                        <th>
                            Descrição
                        </th>
                        <th>
                            Ação
                        </th>           
                    </tr>

                    @foreach($prods as $prod)
                    <tr>
                        <td> {{$prod->id}}
                        </td>
                        <td> {{$prod->nome}}
                        </td>
                        <td> {{$prod->numero_nf}}
                        </td>
                        <td> {{$prod->preco}}
                        </td>
                        <td> {{$prod->descricao}}
                        </td>
                        <td>
                            <a href="/editar_produto/{{$prod->id}}" class="btn btn-sn btn-primary">Editar</a>
                            <a href="/produto/apagar/{{$prod->id}}" class="btn btn-sn btn-danger">Apagar</a>
                        </td>
                    </tr>
                    @endforeach
            </table>
    </div>
</div>
</div>
@endsection

我得到的错误是:

未定义的属性:Illuminate \ Database \ MySqlConnection :: $ id(视图:C:\ xampp \ htdocs \ laravel \ workflow-novo \ resources \ views \ produtos.blade.php)

怎么了?

php sql laravel eloquent
1个回答
0
投票

您需要执行查询。

$prods = DB::table('produtos')->select('produtos.id AS id', 'produtos.nome as nome',
    'produtos.preco AS preco', 'produtos.numero_nf AS numero_nf', 'produtos.cc AS cc')->where('produtos.nome','=',$nome);

不返回任何行。

->get()添加到末尾。

$prods = DB::table('produtos')->select('produtos.id AS id', 'produtos.nome as nome',
    'produtos.preco AS preco', 'produtos.numero_nf AS numero_nf', 'produtos.cc AS cc')
    ->where('produtos.nome','=',$nome)
    ->get();
© www.soinside.com 2019 - 2024. All rights reserved.