如何获取所有刀片视图中可用的全局用户配置文件数据?

马尔

我的用户个人资料数据存储在数据库中,并且能够在用户个人资料页面上读取该数据。我想要的是能够在我的应用程序中的任何视图上获取该数据(变量为$ profile)。最好的方法是什么?任何帮助表示赞赏。这是我的代码。

UserProfileController.php

<?php

namespace App\Http\Controllers;

use App\User;
use App\UserProfile;
use Illuminate\Support\Facades\Auth;

class UserProfileController extends Controller
{
    /**
    * Display the specified resource.
    *
    * @param \App\Property $property
    * @return \Illuminate\Http\Response
    */
    public function show($name)
    {
        $viewer = User::find(Auth::user()->id);

        $owner = User::where('name', $name)->first();

        // Check if user with given username exists
        if($viewer->id !== $owner->id)
        {
            return abort(404);
        }

        if(!$profileId = User::getIdFromName($name)){
            return abort(404);
        }

        $profile = UserProfile::profileDetails($profileId, $viewer->id);

        //dd($profile);

        return view('profile.show', compact('profile'));
    }
}

UserProfile.php

<?php

namespace App;

use App\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;

class UserProfile extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public static function profileData($profileId, $viewerId)
    {
        $user = User::find($viewerId);
        $profile = DB::table('users as u')
            ->select(DB::raw("
                data.*,
                u.id, u.gender_id, u.name, u.email, u.age, u.premium
            "))
            ->leftJoin('user_profiles as data', 'data.user_id', '=', 'u.id')
            ->where('u.id', $profileId)
            ->first();

        return $profile;
    }

    public static function profileDetails($profileId, $viewerId)
    {
        $profile = static::profileData($profileId, $viewerId);

        if ($profile) {
            if ($viewerId != $profileId) {
                # Viewer is displaying another user's profile.
                $myProfile = false;
                } else {
                    $myProfile = true;
                }
            }

            # Populate profile data array
            $profile = [
                'viewerId' => $viewerId,
                'profile_id' => $profileId,
                'myProfile' => $myProfile,
                'status' => Value::fieldLabel('status', $profile->status),
                'first_name' => $profile->first_name,
                'last_name' => $profile->last_name,
                'job' => $profile->job,
                'distance' => $profile->distance,
                'city' => $profile->city,
                'interested_in' => Value::fieldLabel('interested_in', $profile->interested_in),
                'age_from_preference' => $profile->age_from_preference,
                'age_to_preference' => $profile->age_to_preference,
                'looking_for' => Value::fieldLabel('looking_for', $profile->looking_for),
                'personal_quote' => $profile->personal_quote,
                'bio_about' => $profile->bio_about,
                'postal_code' => $profile->postal_code,
                'country' => $profile->country,
                'height' => $profile->height,
                'orientation' => Value::fieldLabel('orientation', $profile->orientation),
                'sexual_preferences' => Value::fieldLabel('sexual_preferences', $profile->sexual_preferences),
                'body_type' => Value::fieldLabel('body_type', $profile->body_type),
                'relationship_status' => Value::fieldLabel('relationship_status', $profile->relationship_status),
                'children' => Value::fieldLabel('children', $profile->children),
                'smoke' => Value::fieldLabel('smoke', $profile->smoke),
                'education' => Value::fieldLabel('education', $profile->education),
                'astro' => Value::fieldLabel('astro', $profile->astro),
                'hobbies' => $profile->hobbies,
                'workplace' => $profile->workplace,
                'sports' => Value::fieldLabel('sports', $profile->sports),
                'movies_series' => Value::fieldLabel('movies_series', $profile->movies_series),
                'music' => Value::fieldLabel('music', $profile->music),
                'food' => Value::fieldLabel('food', $profile->food),
                'literature' => Value::fieldLabel('literature', $profile->literature),
                'mobile_number' => $profile->mobile_number,
            ];

        return $profile;
    }

}
马尔卡兹(Malkhazi Dartsmelidze)

您可以使用View::share()方法在所有视图中共享变量。

由于您要共享经过身份验证的用户数据,因此最好使用中间件:

class SomeMiddleware {
    public function handle($request, Closure $next) {
        $viewer = User::find(Auth::user()->id);

        $owner = User::where('name', $name)->first();

        // Check if user with given username exists
        if($viewer->id !== $owner->id) {
            return abort(404);
        }

        if(!$profileId = User::getIdFromName($name)){
            return abort(404);
        }

        $profile = UserProfile::profileDetails($profileId, $viewer->id);

        View::share('profile_details', $profile);
    }

    /**
     * You can get in view like this:
     * {{ $profile_details }}
     */
}

**方法2 **

AppServiceProvider您可以使用view()->composer()例如:

...
public function boot(){
    view()->composer('*', function ($view) {
        if(!Auth::check()) {
            abort(401);
        }
        $viewer = User::find(Auth::user()->id);

        $owner = User::where('name', $name)->first();

        // Check if user with given username exists
        if($viewer->id !== $owner->id) {
            return abort(404);
        }

        if(!$profileId = User::getIdFromName($name)){
            return abort(404);
        }

        $profile = UserProfile::profileDetails($profileId, $viewer->id);

        View::share('profile_details', $profile);

        $view->with('profile_details', $profile);    
    });  
}
...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何声明所有刀片文件可用的全局变量?

清除所有Sitecore体验配置文件数据

如何从会话数据创建Laravel全局变量并在所有视图中可用?

使用php和mysql从所有用户的朋友中检索用户配置文件数据

如何在全局级别(为所有用户)创建gnome终端配置文件?

如何从ASP.NET MVC自定义IdentityUser模型获取用户配置文件数据?

根据用户ID保存配置文件数据

如何使用Smack使用XMPP设置/获取配置文件数据

如何删除 Ubuntu 上的 NW 配置文件数据?

Jive 7:如何更改配置文件数据/动作?

如何在所有用户配置文件中获取不同的文件夹名称

如何删除除我指定的用户配置文件之外的所有 Windows 用户配置文件?

Xcode配置文件数据警告

首次登录时如何要求和存储其他用户配置文件数据?

使用Django中基于函数的视图阻止不需要的用户访问其他用户的配置文件数据

如何在Linux中的所有用户中仅grep用户创建的配置文件?

获取用户尚未对SQL查询进行投票的所有配置文件

Django注册视图如何从用户表单中获取用户以将配置文件分配给用户

asp.net azure活动目录用户配置文件数据

计算点数并将结果保存在用户配置文件数据库中

使用REST的SharePoint2013内部部署检索用户配置文件数据插件

从用户配置文件中的 firestore 获取数据

如何查询数据库以获取产品订单到用户配置文件

我如何获取所有 json 文件数据 - 离子角度

WEB API 2:在oauth RegisterExternal(facebook)期间获取配置文件数据

获取所有视图中的用户上下文 django?

如何获取所有Spring配置文件名称?

如何获取至少包含所有标签的配置文件(has_and_belongs_to_many 关系)

如何确保所有新用户都具有与当前用户相同的配置文件设置?