简体   繁体   English

Laravel 5.3 View Composer-更改从控制器传递到视图的数据

[英]Laravel 5.3 View Composer - Changing data passed from controller to view

Im getting a bit confused at how to work View Composers in Laravel and i thought i might ask some advice. 我对如何在Laravel中使用View Composers感到有些困惑,我想我可能会问一些建议。

For example, my website has plans, in plans table i have the usual ID etc etc but i also have 'space_allowed' which is represented my a number, i have represented 'unlimited' as NULL so that i dont have to change any of my (count) functionalities. 例如,我的网站有计划,在计划表中我具有通常的ID等,但我也有“ space_allowed”(代表我的数字),我已将“ unlimited”(无限)表示为NULL,因此我不必更改任何(计数)功能。

So, in my controller i have something like this 所以,在我的控制器中,我有这样的东西

$plans = Plan::where('is_active', 1)->get();
return view('pricing', ['plans' => $plans]);

Now when im in the view i just @foreach over the plans and spit out the details. 现在,当我在视图中时,我只是@foreach计划并吐出细节。

BUT, since im storing 'unlimited' space as NULL, the view then reflects that. 但是,因为我将“无限”空间存储为NULL,所以视图随后将其反映出来。

So i figure that since ill be referencing this all over the site, it would be a good idea to check if the value is null, and if it is, return 'unlimited' instead, and if the value isnt null, then just spit it out as normal in one place. 因此,我认为由于在整个站点上都将引用此字段,因此最好检查该值是否为null,如果是,则返回“ unlimited”,而如果该值不为null,则将其吐出正常地放在一个地方。

Hence the idea for the view composer. 因此,视图作曲家的想法。

So i have created the ComposerServiceProvider.php, added the boot and register methods and inside the boot method i have the following: 因此,我创建了ComposerServiceProvider.php,添加了启动和注册方法,并且在启动方法内部,我具有以下内容:

View::composer('*', function ($view) {
});

So i think im almost there. 所以我认为我快到了。

But im lost on how to implement the code that goes in that closure to check if the storage_allowed is null 但是我迷失了如何实现该闭包中用于检查storage_allowed是否为null的代码

Any help would be greatly appreciated. 任何帮助将不胜感激。

You can do this in view composer only by duplicating a lot of data, so just do this check in Blade template: 您只能通过复制大量数据在view composer中执行此操作,因此只需在Blade模板中执行以下操作:

@foreach ($plans as $plan)
    ....
    {{ empty($plan->space) ? 'Unlimited' : $plan->space }}
    ....
@endforeach

Something that is sometimes useful is to add attributes purely for display. 有时有用的事情是添加纯粹用于显示的属性。 I normally postfix these with "nice" so in your case above 我通常用“ nice”将其后缀,因此在您的情况下

class Plan extends Model {

    public function getSpaceNiceAttribute(){
        return is_null($this->space) ? 'Unlimited' : $this->space;
    }

}

Then in your template you can just use {{ $plan->space_nice }} 然后,您可以在模板中使用{{ $plan->space_nice }}

The benefit of this method is that it keeps the output consistent anywhere you need this logic rather than replicated it everywhere in your templates and allows for more conditionals (eg if you wanted to display "none" instead of "0" you could make the the in just this function and all your templates will reflect it). 这种方法的好处是,它可以在需要此逻辑的任何位置使输出保持一致,而不是在模板中的任何位置复制它,并允许更多条件(例如,如果要显示“ none”而不是“ 0”,则可以使仅此功能,您的所有模板都会反映出来)。

Probably a bit late for you but I'll just post for future reference. 可能对您来说有点晚了,但我只发布以供将来参考。 Whatever you pass to the view from your controller is accessible in the closure that you access in the composer method. 从控制器传递到视图的任何内容都可以在composer方法中访问的闭包中访问。 So, for example if you had a page variable that you passed along from the controller, you would be able to access it from the closure like this: 因此,例如,如果您有一个从控制器传递来的页面变量,则可以像下面这样从闭包中访问它:

public function compose(View $view)
{
    $page = $view->getData()['page'];
    $data = ['menu' => $this->generateSideBarHTML($page)];
    $view->with($data);
}

Now, you can access the $menu from your blade template. 现在,您可以从刀片模板访问$菜单。 For your example it would be something like this: 对于您的示例,它将是这样的:

public function compose(View $view)
{
    $plans = $view->getData()['plans'];
    //Iterate on the plans here...
    $view->with($data);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM