laravel Laravel5 如何传递数组来查看
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35326115/
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
Laravel5 how to pass array to view
提问by aahhaa
I am confuse as how to pass variable from controller to view. I know there is a lot about this already on stockoverflow but unable to find one that works, please point me in the right direction. Thank you.
我很困惑如何将变量从控制器传递到视图。我知道在 stockoverflow 上已经有很多关于此的内容,但无法找到有效的方法,请指出正确的方向。谢谢你。
CONTROLLER
控制器
class StoreController extends Controller
{
public function index()
{
$store = Store::all(); // got this from database model
return view('store', $store);
}
{
VIEW
看法
// try a couple of differnt things nothing works
print_r($store);
print_r($store[0]->id);
print_r($id);
回答by Jilson Thomas
public function index()
{
$store = Store::all(); // got this from database model
return view('store')->with('store', $store);
}
You've three options in general.
一般来说,你有三个选择。
return view('store')->with('store', $store);
return view('store')->withStore($store);
return view('store')->with(compact('store'));
return view('store')->with('store', $store);
return view('store')->withStore($store);
return view('store')->with(compact('store'));
1.Creates a variable named store
which will be available in your view
and stores the value in the variable $store
in it.
1.创建一个名为的变量store
,该变量将在您的可用view
并将变量$store
中的值存储在其中。
2.Shorthand for the above one.
2.上述的简写。
3.Also a short hand and it creates the same variable name as of the value passing one.
3.也是一个简写,它创建与传递一个值相同的变量名。
Now in your view
you can access this variable using
现在,您view
可以使用访问此变量
{{ $store->name }}
{{ $store->name }}
it will be same for all 3 methods.
所有 3 种方法都是一样的。
回答by Can Celik
Try this
尝试这个
public function index()
{
return view('store', ['store' => Store::all()]);
}
Then you can access $store in your review.
然后您可以在您的评论中访问 $store。
回答by Alvaro ML
It works for me. You can use 'compact' like this:
这个对我有用。您可以像这样使用“紧凑”:
CONTROLLER:
控制器:
class StoreController extends Controller
{
public function index()
{
$store = Store::all(); // 'don't forget' use App\Driver;
return view('store', compact('store'));
}
}
Don't forget the library: use App\Store;
不要忘记图书馆: use App\Store;
VIEW:
看法:
In the view you have one array with all the data just calling {{ $store }}
If you want an specific data use something like:
在视图中,您有一个包含所有数据的数组,只需调用{{ $store }}
如果您想要特定数据,请使用以下内容:
@foreach($store as $data)
{{ $data->name }}
@endforeach
Don't forget to use the "Forms & HTML" of Laravel, here is the link of the documentation: https://laravel.com/docs/4.2/html
不要忘记使用 Laravel 的“Forms & HTML”,这里是文档的链接:https://laravel.com/docs/4.2/html
回答by James O'Neill
Both of the answers provided so far will solve your problem but it's also worth knowing that your original approach of providing $store
as the second argument to view()
was very nearly correct. What you were missing is that the argument needs to be an array of the variables you want to pass to the view.
到目前为止提供的两个答案都将解决您的问题,但同样值得知道的是,您将提供$store
作为第二个参数的原始方法view()
几乎是正确的。您缺少的是参数必须是要传递给视图的变量数组。
I would probably have gone with return view('store', compact('store'));
but you could also have used return view('store', ['store' => $store]);
.
我可能会去,return view('store', compact('store'));
但你也可以使用return view('store', ['store' => $store]);
.
回答by BKF
Try this :
尝试这个 :
class StoreController extends Controller
{
public function index()
{
$store = Store::all(); // got this from database model
return view('store')->withStore($store);
}
}
View :
{{$store->id}}
回答by Meet Shah
In larval v5.6.3, I am passing Array from Controller to View like this,
在 larval v5.6.3 中,我像这样将 Array 从 Controller 传递到 View,
$data = array(
'title' => 'Welcome to Laravel',
'services' => ['Web Design', 'Programming', 'SEO']
);
return view('pages.index', compact('data'));
And in HTML you can read Array as,
在 HTML 中,您可以将 Array 读作,
@if(count($data['services']) > 0)
<ul>
@foreach($data['services'] as $service)
<li>{{$service}}</li>
@endforeach
</ul>
@endif
回答by Victor Hugo Avelar
Even when the other methods work great, the suggested way, is using the compact() helper as a parameter.
即使其他方法效果很好,建议的方法是使用 compact() 助手作为参数。
public function index()
{
$stores = Store::all(); // got this from database model
return view('store', compact('stores'));
}
In laravel 5.2 the "with()" method is also used to flash data to your view, the compact helper can handle multiple variables.
在 laravel 5.2 中,"with()" 方法也用于将数据闪存到您的视图中,紧凑型助手可以处理多个变量。
Source: Laravel docs and personal experience.
来源:Laravel 文档和个人经验。
回答by zEELz
I had same challenge using
我有同样的挑战使用
return view('store', compact('store'));
But I was rather using <?php foreach()... ?>
inside the blade file to test. I changed to {{ }}
blade syntax and everything worked fine now.
但我宁愿使用<?php foreach()... ?>
刀片文件内部进行测试。我改用{{ }}
刀片语法,现在一切正常。