Problems using Query Builder in Laravel

0

I have manipulated the DB data with the creation of Models, but in my most recent project I am tending to make use of Query Builder, but this is going wrong.

I would like to understand what is missing so that the code works perfectly. To begin I declare the facade to use DB in my controller. Here is the code.

public function putente(Request $request)
{
     $putentes = DB::table('utentes')->where('nome',$request->get('putente'))->orWhere('bi_n', $request->get('putente'))->get();
     if(count($putentes)>0)
     {
         return view('home', ['putentes'=>$putentes]);
     }
 }

In my view I try to get the data this way

{{$putentes->nome}}

But you're returning this error

ErrorException in db040389b09b4e77a2f0e7fafa9a269acda44af2.php line 45:
Trying to get property of non-object (View: C:\xampp\htdocs\admc\resources\views\home.blade.php)

Is there a problem with Query Builder projects instead of Models?

    
asked by anonymous 19.03.2016 / 14:41

1 answer

0

The way you are doing $putentes is an array. You must iterate these faces before you access the name property:

Assuming you're using Blade:

@foreach($putentes as $putente)
   {{$putente->nome}}
@endforeach

Whether or not to use the query builder depends a lot on the case. What is your reason for not using the Model?

If you do not have a good reason, you'll just be generating more code to do the same thing you would do with the model.

    
20.03.2016 / 02:25