首页 > 解决方案 > 在视图布局中解析数据 laravel 6

问题描述

我想在管理页面上制作个人资料照片,这张照片在 layouts.template 文件中,我怎样才能$profil将其发送到 layouts.template 页面?

@if($profil->upload!=null)
<img src="{{asset('backend/assets/img/{{$profil->upload}}.jpg')}}" alt="..." class="avatar-img  rounded-circle">
@else
<img src="{{asset('backend/assets/img/mlane.jpg')}}" alt="..." class="avatar-img  rounded-circle">
@endif

我在 UserController 中创建了 $profile

$auth = Auth::user()->id;
$profil = Profil_user::where('user_id',$auth)->first();

如何让我$profil在 layouts.template 中使用

标签: laraveleloquentlaravel-6

解决方案


您可以在下面文件中的所有视图中共享变量

app/Providers/AppServiceProvider.php

public function boot()
    {
        //Define your user auth call here

        if(confition){
            $profile_pic = 'admin.png';
        } else {
            $profile_pic = 'default.png';
        }
        \View::composer('*', function($view){
                $view->with('profile_pic', $profile_pic);
        });
    }

推荐阅读