我通过return View::make('blog', $posts);
将数据传递给我的blade视图,在我的blade视图中,我想运行@foreach ($posts as $post)
,但是我最终遇到一个错误,指出$posts
未定义。
我的问题是如何调用$posts
数组?
我通过return View::make('blog', $posts);
将数据传递给我的blade视图,在我的blade视图中,我想运行@foreach ($posts as $post)
,但是我最终遇到一个错误,指出$posts
未定义。
我的问题是如何调用$posts
数组?
with
方法将数据传递给视图。return View::make('blog')->with('posts', $posts);
从Laravel 5开始,2013年被接受的答案中的View::make()
门面方法已被弃用。现在使用view()
辅助函数来向视图传递数据,示例如下:
return view("blog", ["posts"=>$posts]);
或者,同样的事情:
return view("blog", compact("posts"));
如果需要,这可以与with()
方法结合使用:
return view("blog", compact("posts"))->with("message", "Comment posted");
文档可以在这里找到。
return view("blog", compact("posts"))->with("success", "Hooray!");
- miken32如果你只想传递一个变量到视图中,可以使用:
在控制器中:
return view('blog')->withTitle('Laravel Magic method.');
在视野中
<div>
Post title is {{$title}}.
</div>
如果您想将多个变量传递给视图,可以使用。return view('blog')->withTitle('Laravel magic method')->withAuthor('Mister Tandon');
在视野中
<div>
Post title is {{$title}}.Author Name is {{$author}}
</div>
除了将一堆->with()
方法串联在一起之外,您还可以将数组作为视图模板名称后面的第二个参数传递。
return View::make('blog', array('posts' => $posts));
或者,如果您正在使用PHP 5.4或更高版本,则可以使用更好的“短”数组语法:
return View::make('blog', ['posts' => $posts]);
如果您想在其他地方计算数组,则此方法很有用。例如,如果您有一堆变量,每个控制器都需要将其传递给视图,并且您想将其与每个特定控制器唯一的变量数组(例如使用array_merge
)结合起来,您可以计算$variables
(它包含一个数组!):
return View::make('blog', $variables);
(我是凭记忆做的,请告诉我是否有语法错误...)
array('posts' => $posts)
我从未见过这种类型的数组,这是匿名数组吗?它的名称在哪里?我知道的是这个数组有键值对。 - Bilal Maqsood提示1:
使用With(),这是最佳实践
return view('about')->withName('Author Willson')->withJob('Writer');
return View::make('home')->with(compact('about'))
return View::make('home')->with('comments', $comments);
提示2:
使用compact()函数
return view(about, compact('post1','post2'));
技巧3:
使用第二个参数:
return view("about", ["comments"=>$posts]);
控制器:
use App\your_model_name;
funtion index()
{
$post = your_model_name::all();
return view('index')->with('this_will_be_used_as_variable_in_views',$post);
}
索引:
<h1> posts</h1>
@if(count($this_will_be_used_as_variable_in_views)>0)
@foreach($this_will_be_used_as_variable_in_views as $any_variable)
<ul>
<p> {{$any_variable->enter_table_field}} </p>
<p> {{$any_variable->created_at}} </p>
</ul>
@endforeach
@else
<p> empty </p>
@endif
use TCG\Voyager\Models\Jobtype;
class FormController extends Controller
{
public function index()
{
$category = Jobtype::all();
return view('contact', compact('category'));
}
}
您也可以像这样处理
$arr_view_data['var1'] = $value1;
$arr_view_data['var2'] = $value2;
$arr_view_data['var3'] = $value3;
return view('your_viewname_here',$arr_view_data);
你可以通过 $var1
、$var2
和 $var3
访问此变量并查看其内容。
您还可以以另一种方式完成同样的操作,
如果您使用的是 PHP 5.5 或更高版本,则可以按照以下方式进行操作:
return view(index, compact('data1','data2')); //as many as you want to pass
<div>
You can access {{$data1}}. [if it is variable]
</div>
@foreach($data1 as $d1)
<div>
You can access {{$d1}}. [if it is array]
</div>
@endforeach
$users = RoleModel::get();
return view('super-admin',compact('users'));
array('posts' => $posts)
,或者您可以使用已接受答案中所示的with()
方法。 - fideloperreturn view('blog',compact('posts'));
- panjeh