将变量传递给其他视图


Pass a variable to an other view

我尝试在另一个视图中加载一个视图(project_template)。我的问题是,我如何将我的$project变量提供给我在主视图中使用"View:make"创建的"project_template"视图。

其实我用->与($project)但是当我尝试在template_view中使用它时,我得到一个非对象错误我还使用了"紧凑",这对我不起作用。

在我的主视图中,我可以毫无问题地使用 $project 变量

视图:

@foreach($projects as $project)
<tr class="gradeA">
   <td>
   <td>{!! $project->description !!}</td>
   <td>
      {!! View::make('shared.tables.project_template')->with($project)->render() !!}
   </td>
</tr>
@endforeach

Project_Template视图:

{!! $project->project_name !!}

错误消息

ErrorException in 0f9dcdc70fd0be3b50d7a39ad7bc90d7 line 4:
Trying to get property of non-object 

感谢您的帮助!

改为:

@foreach($projects as $project)
<tr class="gradeA">
   <td>
   <td>{!! $project->description !!}</td>
   <td>
         @include('shared.tables.project_template')
   </td>
</tr>
@endforeach

只需使用 @include(viewName, [data]) 即可。

这意味着,在您的情况下:

@include('shared.tables.project_template', ['project' => $project])