简体   繁体   中英

Undefined property: Illuminate\Database\MySqlConnection - The query holds the parameter, however I am getting this error. What is wrong

I am using Laravel in order to store and print a set of products on the screen.

Below I will show the code of the controller and of the view.

I have the controller, which calls the view 'produtos'.

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

The View Produtos is:

@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

The error I get is:

Undefined property: Illuminate\\Database\\MySqlConnection::$id (View: C:\\xampp\\htdocs\\laravel\\workflow-novo\\resources\\views\\produtos.blade.php)

What is wrong?

You need to execute the query.

$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);

does not return any rows.

add ->get() to the end.

$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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM