Problems using Query Builder in Laravel

Asked

Viewed 126 times

0

I have manipulated the BD data with the creation of Models, but in my latest project I’m tending to make use of Query Builder, but it’s going wrong.

I would like to understand what is missing so that the code works perfectly. To start I declared the stabbing 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 take the data this way

{{$putentes->nome}}

But this returning 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 using Query Builder instead of Models in projects?

1 answer

0


The way you’re doing $putentes is an array. You need to iterate these guys before accessing the property name:

Assuming you’re using Blade:

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

Whether or not to use the Builder query depends heavily on the case. What is your reason for not using the Model?

If you don’t have a justifiable reason, you’ll just be generating more code to do the same thing you would with the model.

  • Thanks for the contribution, but I solved the question by adding the first() method at the end of the query, so it returns only one line and not a set of lines in the form of Array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.