php 如何传递数据以在 Laravel 中查看?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18341792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:22:14  来源:igfitidea点击:

How to pass data to view in Laravel?

phplaravel

提问by Sam Pettersson

Im passing data to my blade view with return View::make('blog', $posts);and in my blade view I'm trying to run an @foreach ($posts as $post)I end up with an error saying that $postsisn't defined.

我将数据传递给我的刀片视图,return View::make('blog', $posts);并在我的刀片视图中我试图运行一个@foreach ($posts as $post)我最终得到一个错误,说$posts没有定义。

My question is how would the $postsarray be called?

我的问题是如何$posts调用数组?

回答by Mostafa Elgaafary

You can pass data to the view using the withmethod.

您可以使用with方法将数据传递给视图。

return View::make('blog')->with('posts', $posts);

回答by miken32

It's probably worth mentioning that as of Laravel 5, passing data to the view is now done like this:

可能值得一提的是,从 Laravel 5 开始,将数据传递给视图现在是这样完成的:

return view("blog", ["posts"=>$posts]);

Or:

或者:

return view("blog", compact("posts"));

Documentation is available here.

文档可在此处获得

回答by Parvesh Kumar Tandon

If you want to pass just one variableto view, you may use

如果你只想传递一个变量来查看,你可以使用

In Controller

控制器中

return view('blog')->withTitle('Laravel Magic method.');

In view

视图中

<div>
  Post title is {{$title}}.
</div>

If you want to pass multiple variables to view, you may use

如果要传递多个变量来查看,可以使用

In Controller

控制器中

return view('blog')->withTitle('Laravel magic method')->withAuthor('Mister Tandon');

In view

视图中

<div>
   Post title is {{$title}}.Author Name is {{$author}}
</div>

回答by iconoclast

You can also pass an array as the second argument after the view template name, instead of stringing together a bunch of ->with()methods.

您还可以在视图模板名称之后传递一个数组作为第二个参数,而不是将一堆->with()方法串在一起。

return View::make('blog', array('posts' => $posts));

Or, if you're using PHP 5.4 or better you can use the much nicer "short" array syntax:

或者,如果您使用的是 PHP 5.4 或更高版本,则可以使用更好的“短”数组语法:

return View::make('blog', ['posts' => $posts]);

This is useful if you want to compute the array elsewhere. For instance if you have a bunch of variables that every controller needs to pass to the view, and you want to combine this with an array of variables that is unique to each particular controller (using array_merge, for instance), you might compute $variables(which contains an array!):

如果您想在其他地方计算数组,这很有用。例如,如果您有一堆变量,每个控制器都需要传递给视图,并且您想将其与每个特定控制器独有的变量数组(array_merge例如,使用)结合起来,您可能会计算$variables(其中包含数组!):

return View::make('blog', $variables);

(I did this off the top of my head: let me know if a syntax error slipped in...)

(我做了这件事:如果出现语法错误,请告诉我......)

回答by venkatskpi

Tips1:

提示1:

Using With(), This is a best practice

使用With(),这是最佳实践

return view('about')->withName('Author Willson')->withJob('Writer');
return View::make('home')->with(compact('about'))
return View::make('home')->with('comments', $comments);

Tips2:

提示2:

Using compact()

使用紧凑()

return view(about, compact('post1','post2'));

Tips3:

提示3:

Using Second Parameters:

使用第二个参数:

return view("about", ["comments"=>$posts]);

回答by li bing zhao

use TCG\Voyager\Models\Jobtype;

class FormController extends Controller
{
public function index()
{
   $category = Jobtype::all();
   return view('contact', compact('category'));

}
}
  • use your model
  • assign it to a variabel
  • pass the object to the view Here is an example:
  • 使用你的模型
  • 将它分配给一个变量
  • 将对象传递给视图 这里是一个例子:

回答by kamal

controller:

控制器:

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);
}

index:

指数:

<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

Hope this helps! :)

希望这可以帮助!:)

回答by Jay Momaya

You can pass data to the view using the with method.

您可以使用 with 方法将数据传递给视图。

return view('greeting', ['name' => 'James']);

回答by Sagar jadhav

You can also do like this

你也可以这样做

$arr_view_data['var1'] = $value1;
$arr_view_data['var2'] = $value2;
$arr_view_data['var3'] = $value3;

return view('your_viewname_here',$arr_view_data);

And you access this variable to view as $var1,$var2,$var3

并且您访问此变量以查看为$var1, $var2,$var3

回答by user9389401

You can also write for passing multiple data from your controller to a view

您还可以编写将多个数据从控制器传递到视图

 return \View::make('myHome')
            ->with(compact('project'))
            ->with(['hello'=>$hello])
            ->with(['hello2'=>$hello2])
            ->with(['hello3'=>$hello3]);