首页 > 解决方案 > 如何在所有刀片视图中获取全局用户配置文件数据?

问题描述

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

用户配置文件控制器.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'));
    }
}

用户配置文件.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;
    }

}

标签: phplaravel

解决方案


您可以使用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);    
    });  
}
...

推荐阅读