0

I want to get data "report_to" from database. But i got error when I try to print variable in my view page. It's said "ErrorException htmlspecialchars() expects parameter 1 to be string, array given".

This is my controller.

function editroledetails(Request $request)
    {
        $user = \Auth::user();
        $userphone = 0;
        $reportTo = DB::select(DB::raw("SELECT report_to FROM customer_type WHERE username='19331986' "));
        $data = [
            'editUsername' => $request->editUsername,
            'editNik' => $request->editNik,
            'editEmail' => $request->editEmail,
            'editRegIdentities' => $request->editRegIdentities,
            'editID' => $request->editID
        ];

        return view('editroledetails', compact('user', 'userphone', 'data', 'reportTo'));
    }

This is my view

<div class="alert alert-info">
            <h3>{{$reportTo}}</h3>
        </div>

I expect the view will display the variable that is "Name", but I got error htmlspecialchars() expects parameter 1 to be string, array given.

3 Answers3

0

Change

$reportTo = DB::select(DB::raw("SELECT report_to FROM customer_type WHERE username='19331986' "));

to

$reportTo = DB::select(DB::raw("SELECT report_to FROM customer_type WHERE username='19331986' "))->get();

and now $reportTo is an array you may need to use @foreach or @for. or if you are sure you will get only one record, you can use first() like

$reportTo = DB::select(DB::raw("SELECT report_to FROM customer_type WHERE username='19331986' "))->first();
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
0

it means and it is clear that $reportTo is a results set of the query, change your query to $reportTo = DB::select(DB::raw("SELECT report_to FROM customer_type WHERE username='19331986' "))->first(); and then access your DB column like $reportTo['colname']

0

[SOLVED] Thank you for the answer, Im already solved it.

This the code

<?php echo $reportTo[0]->report_to; ?>

This is print_r of $reportTo

This is my reference