Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 7-返回视图如何()->;使用(';一些数据)是否完全有效?_Laravel - Fatal编程技术网

Laravel 7-返回视图如何()->;使用(';一些数据)是否完全有效?

Laravel 7-返回视图如何()->;使用(';一些数据)是否完全有效?,laravel,Laravel,我在控制器中有一个代码 $messages['msg'] = DB::table('contactus')->where('email',Auth::user()->email)->get(); return view('contactmessages')->with($messages); 在刀刃上 @foreach($msg as $key => $message) <tr> <th

我在控制器中有一个代码

$messages['msg']  =  DB::table('contactus')->where('email',Auth::user()->email)->get();
   return view('contactmessages')->with($messages);
在刀刃上

 @foreach($msg as $key => $message)
      <tr>
                  <th>{{$message->id}}</th>
                  <th>{{$message->message}}</th>
                  <th>{{$message->asked_on}}</th>
                  <th>{{$message->answered_on}}</th>
                  <th>{{$message->status}}</th>
              </tr>

            @endforeach
@foreach($msg as$key=>$message)
{{$message->id}
{{$message->message}
{{$message->asked_on}
{{$message->responsed_on}
{{$message->status}
@endforeach
因此,基本上,我发送一个数据数组,通过它的键(在本例中,它是
msg
)访问,该数组已转换为变量。是这样吗?

回答是从抄袭来的,供我自己参考

数组:您可以将数据数组传递给视图,如下所示:

返回视图('post',['post'=>$post])
以这种方式传递信息时,数据应该是具有键/值对的数组。在视图中,您可以使用相应的键访问每个值,例如

with():除了将完整的数据数组传递给view helper函数之外,您还可以使用with方法将单个数据段添加到视图中:

return view('post')->with('post' => $post);
// multiple with method
return view('post')->with('post' => $post)->with('comment' => $comment);
compact():您可以使用compact()来传递数据,而不是使用这种类型的传递数据。compact()是一个内置的php函数,允许您使用变量名及其值创建数组。变量名必须作为字符串参数传递给compact函数,然后,您将收到一个数组,因此compact在视图上传递变量,就像第一个方法一样:

return view('post', compact('post'));
// same as
return view('post', ['post' => $post]);
可以使用compact()而不是with():

刀片可能是

@foreach($messages as $item)
  <tr>
              <th>{{$item->id}}</th>
              <th>{{$item->message}}</th>
              <th>{{$item->asked_on}}</th>
              <th>{{$item->answered_on}}</th>
              <th>{{$item->status}}</th>
  </tr>

@endforeach
@foreach($messages作为$item)
{{$item->id}
{{$item->message}
{{$item->asked_on}
{{$item->回答}
{{$item->status}
@endforeach

基本上听起来是对的,检查一下这个希望helps@sta我照样抄了你的答案,供我参考
@foreach($messages as $item)
  <tr>
              <th>{{$item->id}}</th>
              <th>{{$item->message}}</th>
              <th>{{$item->asked_on}}</th>
              <th>{{$item->answered_on}}</th>
              <th>{{$item->status}}</th>
  </tr>

@endforeach