无法在视图中传递数据(laravel 4)

时间:2017-03-04 11:23:17

标签: php laravel-4

我试图在视图中从控制器传递一个数组,但它没有显示错误,因为它重新识别变量但数据不存在,同时当我在控制器中尝试dd()时,我有数据。 以下是我的代码:

//控制器

public function showMessage(){
    $id = Input::get('recordid');

    foreach ($id as $chat_id) {
        $history = Chat::where('chat_id','=', $chat_id)->get();  

        return View::make('chat.chatview', compact($history));
    } 
}

//视图

@extends ('master')
@section ('content')

<div class="container">
    <h1 style="color: white;">Chat History</h1>
    <?php if (isset($history)) { ?>
           @foreach($history as $row)
           <table>
               <tr>
                   <td><?php echo $history ;?></td>
               </tr>
           </table>
           @endforeach
    <?php } ?>
</div>
@stop

//路线

Route::get('chathistory', function(){
    return View::make('chat/chatview');});

我得到的数组来自另一个视图,代码如下: //视图

@foreach($chat as $row)
                <tr>
                    <td>{{ $row->chat_id }}</td>
                    <td>{{ $row->fullname }}</td>
                    <td>{{ $row->email }}</td>
                    <td>{{ $row->transcript_text }}</td>
                    <td>{{ $row->duration }}</td>
                    <td>
                        <input type="submit" value="History" class="btn pull-left" name="status[]" id="status[]" onclick="javascript:changeStatus('{{$row->chat_id}}','{{$arr}}')"/>
                    </td>
                </tr>
                        <input name="recordid[]" id="recordid[]" type="hidden">
                        <?php $arr++; ?>
                        @endforeach
                        <script type="text/javascript">
                                    function changeStatus(id, arr){
                                        $('[id^=recordid]').eq(arr).val(id);
                                    }
                        </script> 

//路线

Route::get('individual', function(){
    $chat = Chat::all();
    return View::make('chat/individual')->with('chat', $chat);});

Route::post('individual','HomeController@showMessage');

2 个答案:

答案 0 :(得分:0)

更改以下行:

return View::make('chat.chatview', compact($history));

return View::make('chat.chatview', ['history' => $history]);

return View::make('chat.chatview')->with(compact('history'));

再试一次。

答案 1 :(得分:0)

在此更改

return View::make('chat.chatview')->with('history',$history);