0

I tried to read an one record of the table on the view.

The command:

{{ $user_search = DB::table('OUSR')->where('U_NAME', 'Ana Silva')->first() }}

When the site execute this command return the error:

htmlspecialchars() expects parameter 1 to be string, object given

I checked on debugger and select is all right.

Anyone can help me ?

lagbox
  • 48,571
  • 8
  • 72
  • 83
Paulo Fonseca
  • 49
  • 1
  • 1
  • 2
  • `first()` returns an object or `null` so the error is accurate ... `{{ ... }}` uses `htmlspecialchars` ... you can't pass that to it; the `{{ }}` are for "echoing" data – lagbox Nov 30 '20 at 21:00
  • you are echoing `$user_search` which is an object. Either use `{% %}` if you want to use `$user_search` further down or get a field of the object – JensV Nov 30 '20 at 21:00
  • 1
    You really shouldn't be doing DB queries in your blade template. That should be happening in your controller or elsewhere. – mikeroq Nov 30 '20 at 21:04
  • If you want to use the value of query somewhere down in the blade view then you should @php $user_search = DB::table('OUSR')->where('U_NAME', 'Ana Silva')->first(@endphp. However it is not advisable to have query statements in the view. All the queries to fetch records from database needs to be done in controller class or at least before the view is returned as response – Donkarnash Nov 30 '20 at 21:11

1 Answers1

2

As others mentioned, the first() at the end, returns an object. so you can read the properties (or in this case columns) like this:

{{ $user_search = DB::table('OUSR')->where('U_NAME', 'Ana Silva')->first()->id }}

or other table columns:

{{ $user_search = DB::table('OUSR')->where('U_NAME', 'Ana Silva')->first()->U_NAME }}
Rouhollah Mazarei
  • 3,969
  • 1
  • 14
  • 20