Laravel5如何通过数组查看

Laravel5如何通过数组查看

本文介绍了Laravel5如何通过数组查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何将变量从控制器传递到视图感到困惑.我知道在库存溢出方面已经有很多关于此的内容,但找不到有效的方法,请向我指出正确的方向.谢谢你.

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.

控制器

 class StoreController extends Controller

 {
    public function index()
    {
        $store = Store::all(); // got this from database model
        return view('store', $store);
    }
 {

查看

// try a couple of differnt things nothing works

print_r($store);
print_r($store[0]->id);
print_r($id);

推荐答案

public function index()
{
  $store = Store::all(); // got this from database model

  return view('store')->with('store', $store);
}

一般来说,您有三个选择.

You've three options in general.

  1. return view('store')->with('store', $store);

return view('store')->withStore($store);

return view('store')->with(compact('store'));

1.创建一个名为store的变量,该变量将在您的view中可用,并将值存储在其中的变量$store中.

1.Creates a variable named store which will be available in your view and stores the value in the variable $store in it.

2.速记.

3.也是一种简写,它创建的变量名与传递值的变量名相同.

3.Also a short hand and it creates the same variable name as of the value passing one.

现在在您的view中,您可以使用

Now in your view you can access this variable using

{{ $store->name }}

这对于所有3种方法都是相同的.

it will be same for all 3 methods.

这篇关于Laravel5如何通过数组查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:34