Problem with Trying to get property of non-object Laravel

0

I'm new to programming and stackoverflow and I'm getting this error

  

ErrorException (E_ERROR)       Trying to get property of non-object (View: /home/hernan/laravel/Store/Tienda/resources/views/user/compra_producto/index.blade.php)

I explain what I want to do ... I have a catalog of products where I ready my products

@foreach($productos as $pro)
@if($pro->emp_activo==1)
<div class="col-lg-3 col-sm-3 portfolio-item">
    <div class="card h-100">
        <a href="#"><img class="card-img-top img-rounded" src="{{asset('imagenes/productos/'.$pro->pro_foto)}}"  height="200px" alt=""></a>
        <div class="card-body">
            <h4 class="card-title">
                <a href="#">{{$pro->pro_nom}}</a>
            </h4>
            <p class="card-text">{{$pro->pro_info}}</p>
            <p><b>Tienda:</b> {{$pro->empresa}}<br>
                @if ($pro->pro_ofer_active==1)
                <b>Precio: </b><strike style="color: red;">{{$pro->pro_precio}}</strike> | <b>Oferta: </b>{{$pro->pro_oferta}}
                @else
                <b>Precio: </b>{{$pro->pro_precio}}
                @endif
            </p>
        </div>
        <div class="btn-group" style="margin: auto;">
            <span><a href="{{ url('/comprar',array($pro->pro_id)) }}" type="button" class="btn btn-block btn-success"><i class="fa fa-credit-card" aria-hidden="true"></i> Comprar</a></span>
            <span><button type="button" class="btn btn-block btn-info"><i class="fa fa-thumbs-up" aria-hidden="true"></i> ({{$pro->pro_megusta}}) </button></span>
            <span><button type="button" class="btn btn-block btn-danger"><i class="fa fa-thumbs-down" aria-hidden="true"></i> ({{$pro->pro_nomegusta}})</button></span>
        </div>
    </div>
</div>
@endif
@include('user.producto.modal')
@endforeach

As you can see above there is a button to buy a product and what I do is get the id of that product to work in another view. Then I try to use that id because I must save it in another table but it gives me the error "Trying to get property of non-object" when I try to print it on screen

This is the view where I want the id to work it and that is when I get the error specifically in this line

{{$ pro> pro_id}}

    <div class="container">
    @foreach($producto as $pro)
     <h1>{{$pro->pro_id}}</h1>
    @endforeach
    <h1>Realizar pedido</h1>
    @include('user.compra_producto.form', ['compra' => $compra, 'url' => '/comprar', 'method' => 'POST'])
</div>

I also pass them my driver

public function store(Request $request) {
        $compra = new Mensaje;
        $compra->celular = $request->celular;
        $compra->mensaje = $request->mensaje;
        $compra->nombre = $request->nombre;
        $compra->ciudad = $request->ciudad;
        $compra->leido = 0;
        $compra->producto_pro_id = $request->producto_pro_id;
        $compra->users_id = 1;

        if($compra -> save()) {
            session()->flash('notificacion', 'Pedido realizado con exito!..');
            return redirect()->back();
        } else {
            return view('user.compra_producto.index', ['compra' => $compra ]);
        }
    }

    public function show($id) {
        $compra = new Mensaje;
        $producto = Producto::find($id);
        return view('user.compra_producto.index', compact('compra', 'producto'));
    }

and this is my route

Route::resource('comprar', 'CompraProductoController');

In the show function of my controller I work with two models because the id of the product I want to save it in a table called message ..

Please, if someone can give me a hand with this and apologize beforehand if I am wrong to ask the question .. observation: the id of the product passes into view .. only I can not use it to put it in an h1 for example

    
asked by Hernan Chaparro 22.06.2018 в 21:01
source

1 answer

2

Whenever you use a search condition (either in find() , where() etc ...) use the ->get() method to receive the entire query.

$producto = Producto::find($id)->get();
    
answered by 23.06.2018 / 16:12
source