【问题标题】:Laravel - Pass more than one variable to viewLaravel - 传递多个变量以查看
【发布时间】:2013-12-05 07:41:09
【问题描述】:

我有这个站点,它的一个页面从数据库中创建了一个简单的人员列表。我需要将一个特定的人添加到我可以访问的变量中。

如何修改 return $view->with('persons', $persons); 行以将 $ms 变量也传递给视图?

    function view($view)
    {
        $ms = Person::where('name', 'Foo Bar');

        $persons = Person::order_by('list_order', 'ASC')->get();

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

【问题讨论】:

    标签: php laravel laravel-3


    【解决方案1】:

    这就是你的做法:

    function view($view)
    {
        $ms = Person::where('name', '=', 'Foo Bar')->first();
    
        $persons = Person::order_by('list_order', 'ASC')->get();
    
        return $view->with('persons', $persons)->with('ms', $ms);
    }
    

    你也可以使用compact():

    function view($view)
    {
        $ms = Person::where('name', '=', 'Foo Bar')->first();
    
        $persons = Person::order_by('list_order', 'ASC')->get();
    
        return $view->with(compact('persons', 'ms'));
    }
    

    或者一行完成:

    function view($view)
    {
        return $view
                ->with('ms', Person::where('name', '=', 'Foo Bar')->first())
                ->with('persons', Person::order_by('list_order', 'ASC')->get());
    }
    

    甚至将其作为数组发送:

    function view($view)
    {
        $ms = Person::where('name', '=', 'Foo Bar')->first();
    
        $persons = Person::order_by('list_order', 'ASC')->get();
    
        return $view->with('data', ['ms' => $ms, 'persons' => $persons]));
    }
    

    但是,在这种情况下,您必须以这种方式访问​​它们:

    {{ $data['ms'] }}
    

    【讨论】:

    • 是的,我在这段时间里做到了这一点。但是现在当我尝试在视图中访问它时,例如{{ $ms->name }} 我得到Undefined property: Laravel\Database\Eloquent\Query::$name 我错过了什么?
    • 您在方法的第一行缺少->first()
    • pjmil116 刚刚编辑添加了->first() Raphael 正在评论的内容。
    • 嗯,是的。我刚刚意识到这个项目是 laravel 3,因此我很沮丧。不得不将查询语法更改为$ms = DB::first('select * from people where name = "Foo Bar"');
    • Laravel 4 和 3 的语法,对于像这样的常见事物,几乎相同。您仍然可以使用Person::where('name', '=', 'Foo Bar')->first()。已编辑。
    【解决方案2】:

    只需将其作为数组传递:

    $data = [
        'name'  => 'Raphael',
        'age'   => 22,
        'email' => 'r.mobis@rmobis.com'
    ];
    
    return View::make('user')->with($data);
    

    或者像@Antonio 提到的那样将它们链接起来。

    【讨论】:

    • 比链接'with'优雅得多
    • 推荐的方式是使用compact方法来传递变量。感觉更优雅。
    • 我认为部分选择将基于数据。对于一个简单的变量,这可以工作,但对于嵌套数据,变量紧凑或最适合在视图中进行操作。
    【解决方案3】:

    使用紧凑型

    function view($view)
    {
        $ms = Person::where('name', '=', 'Foo Bar')->first();
    
        $persons = Person::order_by('list_order', 'ASC')->get();
        return View::make('users', compact('ms','persons'));
    }
    

    【讨论】:

      【解决方案4】:

      遇到了类似的问题,但是如果你不一定要返回带有视图文件的视图,你可以这样做:

      return $view->with(compact('myVar1', 'myVar2', ..... , 'myLastVar'));
      

      【讨论】:

        【解决方案5】:

        将多个变量传递给 Laravel 视图

        //Passing variable to view using compact method    
        $var1=value1;
        $var2=value2;
        $var3=value3;
        return view('viewName', compact('var1','var2','var3'));
        
        //Passing variable to view using with Method
        return view('viewName')->with(['var1'=>value1,'var2'=>value2,'var3'=>'value3']);
        
        //Passing variable to view using Associative Array
        return view('viewName', ['var1'=>value1,'var2'=>value2,'var3'=>value3]);
        

        在这里阅读Passing Data to Views in Laravel

        【讨论】:

          【解决方案6】:

          为了将多个数组数据从控制器传递到视图,试试吧。这是工作。在此示例中,我从表中传递主题详细信息,主题详细信息包含类别 id,如果类别 id 是从另一个表类别中获取的,则详细信息如名称。

          $category = Category::all();
          $category = Category::pluck('name', 'id');
          $item = Subject::find($id);
          return View::make('subject.edit')->with(array('item'=>$item, 'category'=>$category));
          

          【讨论】:

            【解决方案7】:

            请试试这个,

            $ms = Person::where('name', 'Foo Bar')->first();
            $persons = Person::order_by('list_order', 'ASC')->get();
            return View::make('viewname')->with(compact('persons','ms'));
            

            【讨论】:

              【解决方案8】:
                  $oblast = Oblast::all();
                  $category = Category::where('slug', $catName)->first();
                  $availableProjects = $category->availableProjects;
                  return view('pages.business-area')->with(array('category'=>$category, 'availableProjects'=>$availableProjects, 'oblast'=>$oblast));
              

              【讨论】:

                【解决方案9】:

                这个答案似乎是

                有点帮助同时在函数中声明大量变量

                Laravel 5.7.*

                举例

                public function index()
                {
                    $activePost = Post::where('status','=','active')->get()->count();
                
                    $inActivePost = Post::where('status','=','inactive')->get()->count();
                
                    $yesterdayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(-1))->get()->count();
                
                    $todayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(0))->get()->count();
                
                    return view('dashboard.index')->with('activePost',$activePost)->with('inActivePost',$inActivePost )->with('yesterdayPostActive',$yesterdayPostActive )->with('todayPostActive',$todayPostActive );
                }
                

                当您看到返回的最后一行时,它看起来不太好

                当你的项目变得越来越大时它不好

                所以

                public function index()
                    {
                        $activePost = Post::where('status','=','active')->get()->count();
                
                        $inActivePost = Post::where('status','=','inactive')->get()->count();
                
                        $yesterdayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(-1))->get()->count();
                
                        $todayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(0))->get()->count();
                
                        $viewShareVars = ['activePost','inActivePost','yesterdayPostActive','todayPostActive'];
                
                        return view('dashboard.index',compact($viewShareVars));
                    }
                

                如您所见,所有变量都声明为 $viewShareVars 数组并在视图中访问

                但我的功能变得非常大,所以我决定制作这条线 非常简单

                public function index()
                    {
                        $activePost = Post::where('status','=','active')->get()->count();
                
                        $inActivePost = Post::where('status','=','inactive')->get()->count();
                
                        $yesterdayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(-1))->get()->count();
                
                        $todayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(0))->get()->count();
                
                        $viewShareVars = array_keys(get_defined_vars());
                
                        return view('dashboard.index',compact($viewShareVars));
                    }
                

                原生php函数get_defined_vars()从函数中获取所有定义的变量

                array_keys 将获取变量名

                所以在您看来,您可以访问函数内所有声明的变量

                作为{{$todayPostActive}}

                【讨论】:

                • 你救了我的命,谢谢!我认为这是一个很好的解决方案。
                【解决方案10】:

                很简单:)

                <link rel="icon" href="{{ asset('favicon.ico')}}" type="image/x-icon" />
                

                【讨论】:

                • 与其他答案相比,这个似乎有点不合适。
                【解决方案11】:

                with函数和single参数:

                    $ms = Person::where('name', 'Foo Bar');
                    $persons = Person::order_by('list_order', 'ASC')->get();
                    return $view->with(compact('ms', 'persons'));
                

                with函数和array参数:

                    $ms = Person::where('name', 'Foo Bar');
                    $persons = Person::order_by('list_order', 'ASC')->get();
                    $array = ['ms' => $ms, 'persons' => $persons];
                    return $view->with($array);
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2019-12-19
                  • 2020-07-29
                  • 1970-01-01
                  • 2016-07-06
                  • 1970-01-01
                  • 2021-12-05
                  • 1970-01-01
                  • 2020-07-24
                  相关资源
                  最近更新 更多