php - 如何在所有刀片视图中获取全局用户配置文件数据?
问题描述
我有用户将用户个人资料数据存储在数据库中,并且我能够在用户个人资料页面上读取该数据。我想要的是能够在我的应用程序的任何视图上获取该数据(变量是 $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;
}
}
解决方案
您可以使用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);
});
}
...
推荐阅读
- c# - Visual Studio 2019 设计器,在设计器中设置数组属性会导致弹出“属性值无效”
- amazon-dynamodb - 查询 DynamoDB 时如何指定 ReadCapacityUnits
- scala - 没有变量的Scala轮询循环
- visual-studio-code - 仅对一个单词或短语禁用 vscode 智能感知?
- javascript - 将局部变量作为 Object 属性读取
- python - 如何创建一个列是另一列的移位版本,而忽略熊猫中的重复项?
- reactjs - Dispatch 多个 http 请求 React/Redux
- python - TypeError:Movie() 得到了一个意外的关键字参数“演员”
- python - 在python中将初始化变量作为列表返回
- keyboard - 是否可以在不使用我的物理键盘的情况下在虚拟机上运行 pyautogui?