gpt4 book ai didi

php - 如何在所有 Blade View 中获取全局用户配置文件数据?

转载 作者:行者123 更新时间:2023-12-04 10:38:22 25 4
gpt4 key购买 nike

我有用户存储在数据库中的用户配置文件数据,我能够在用户配置文件页面上读取该数据。我想要的是能够在我的应用程序中的任何 View 上获取该数据(变量是 $profile)。最好的方法是什么?任何帮助表示赞赏。这是我的代码。

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

}

最佳答案

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

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

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

关于php - 如何在所有 Blade View 中获取全局用户配置文件数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60052728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com