У меня есть пользователь с данными профиля пользователя, хранящимися в базе данных, и я могу прочитать эти данные на странице профиля пользователя. Я хочу иметь возможность извлекать эти данные (переменная $ 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;
}
}