Laravel - Pass more than one variable to view

前端 未结 11 1493
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 18:54

I have this site and one of its pages creates a simple list of people from the database. I need to add one specific person to a variable I can access.

How do I modif

相关标签:
11条回答
  • 2020-11-29 19:38

    This Answer seems to be

    bit helpful while declaring the large numbe of variable in the function

    Laravel 5.7.*

    For Example

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

    When you see the last line of the returns it not looking good

    When You Project is Getting Larger its not good

    So

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

    As You see all the variables as declared as array of $viewShareVars and Accessed in View

    But My Function Becomes very Larger so i have decided to make the line as very simple

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

    the native php function get_defined_vars() get all the defined variables from the function

    and array_keys will grab the variable names

    so in your view you can access all the declared variable inside the function

    as {{$todayPostActive}}

    0 讨论(0)
  • 2020-11-29 19:40
        $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));
    
    0 讨论(0)
  • 2020-11-29 19:41

    Passing multiple variables to a Laravel view

    //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]);
    

    Read here about Passing Data to Views in Laravel

    0 讨论(0)
  • 2020-11-29 19:41

    Please try this,

    $ms = Person::where('name', 'Foo Bar')->first();
    $persons = Person::order_by('list_order', 'ASC')->get();
    return View::make('viewname')->with(compact('persons','ms'));
    
    0 讨论(0)
  • 2020-11-29 19:43

    Just pass it as an array:

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

    Or chain them, like @Antonio mentioned.

    0 讨论(0)
提交回复
热议问题