Laravel:在视图刀片中解析这个json数据?

时间:2016-01-14 01:16:49

标签: javascript php jquery json laravel

有人可以帮助我,我试图将值传递给我的视图,但我总是得到一个*未定义的变量:结果*

目前这是我的观点

@foreach($result as $row)
    <tr>
        <td>{{ $row->firstname }}</td>
        <td>{{ $row->lastname }}</td>
        <td>{{ $row->account_type}}</td>
        <td>{{ $row->gender }}</td>
        <td>{{ $row->college }}</td>
        <td>{{ $row->address }}</td>
    </tr>

@endforeach

这是我的控制器

$response = ['status' => false, 'message' => 'Invalid request'];

        try {

            if (strlen($data['output']) > 0) {

                $data = $data['output'];
                $result = DB::table('user_details')
                ->select('firstname', 'lastname', 'account_type', 'gender', 'college', 'address')
                ->where('firstname', 'LIKE', '%' . $data . '%')
                ->orWhere('lastname', 'LIKE', '%' . $data . '%')
                ->orWhere('username', 'LIKE', '%' . $data . '%')
                ->get();              

                if ($result) {
                    $response = $result;
                }
            }

        } catch (Exception $e) {
            $response['message'] = $e->getMessage();
        }

       return view('search')->with('result', json_decode($response, true));

我想显示类似

的内容
First Name : juan
Last Name : dela cruz
Account Type : student
Gender : male
College : CON
Address : doon doon lang

1 个答案:

答案 0 :(得分:0)

更改此行:

return view('search')->with('result', json_decode($response, true));

致:

$data['result'] = $response; // if $result is right. 
// send another variable
return view('search')->with($data);