用户到个人资料链接Laravel 5.6

时间:2018-04-17 12:13:20

标签: php laravel laravel-5.6

我是Laravel的新手,想要一些清晰度。

我正在尝试关联两个表:Users => Profiles

Users看起来像这样:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('username');
    $table->string('displayName');
    $table->string('email')->unique();
    $table->string('role')->nullable();
    $table->string('department')->nullable();
    $table->string('location')->nullable();
    $table->string('directDialIn')->nullable();
    $table->string('mobileNumber')->nullable();
    $table->string('managedByUsername')->nullable();
    $table->string('managedByDisplayName')->nullable();
    $table->timestamps();
});

Profiles看起来像这样:

Schema::create('profiles', function (Blueprint $table) {
    $table->increments('id');
    $table->string('user_username')->index();
    $table->mediumText('skills');
    $table->mediumText('background');
    $table->mediumText('socialProfiles');
    $table->string('displayPicture')->default('../../assets/images/user_pic.jpg');
    $table->string('mangedByUsername');
    $table->boolean('changesPending')->default(0);
    $table->timestamps();
}); 

相关模型

class User extends Authenticatable
{
    use Notifiable;

    /**
     * Primary key to use
     */
    protected $primaryKey = 'username';

    /**
     * Tell Eloquent to not auto increment
     */
    public $incrementing = false;

    /**
     * Table to use
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username', 'displayName', 'email', 
        'role', 'department', 'location', 
        'directDialIn', 'mobileNumber', 'managedByUsername',
        'managedByDisplayName'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * Get the profile associated with this user
     */
    public function profile()
    {
        return $this->hasOne(Profile::class, 'user_username');
    }
}

class Profile extends Model
{
    /**
    * Get the user that has this Profile
    */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_username');
    }
}

根据文件:

  

Eloquent根据模型名称确定关系的外键。在这种情况下,自动假定Phone模型具有user_id外键。如果要覆盖此约定,可以将第二个参数传递给hasOne方法:

所以我明确定义了要使用的密钥。

在我的路线中,我有:

Route::get('/profile/{user}', 'ProfileController@index');

我习惯于只使用SQL JOIN连接两个表,但在这种情况下,如何同时获取用户模型数据和Profile Model数据?

所以它就像:用户:Dave然后是Dave的个人资料。

另外,如果Dave没有填写他的个人资料,我可以只显示他的用户数据吗?

ProfilesController我有这个:

public function index(Request $request)
{
    // Get this user's username from the session
    $username = $request->session()->get('User_name');

    /**
     * Get the Profile and associated User by pulling from the database via user_username
     * Will return a profile with an attached user
     */
    $profile = Profile::with('user')->where('user_username', $username)->first();

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

但是,当我转出Profile的结果时,关联的User为空。

我觉得这很奇怪,因为我可以使用Artisan tinker,如下所示:

$profile = App\Profile::find(7);

然后......

$profile->user

用户与ID为7的个人资料相关。

A dd($profile);表示以下内容:

enter image description here

个人资料表

enter image description here

用户表

enter image description here

正如您所看到的,数据是两个表格,而user_usernameusername相同但由于某些原因,使用with::时,两者之间的链接就不见了

1 个答案:

答案 0 :(得分:1)

使用with()方法

$profile_with_user = Profile::with('user')->where('user_username', $username)->first();

或者

$user_with_profile = User::with('profile')->where('username', $username)->first();