用户到配置文件 link Laravel 5.6

User to Profile link Laravel 5.6

我是 Laravel 的新手,想了解一下。

我正在尝试 Link 两个 table: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 determines the foreign key of the relationship based on the model name. In this case, the Phone model is automatically assumed to have a user_id foreign key. If you wish to override this convention, you may pass a second argument to the hasOne method:

所以我已经明确定义了要使用的键。

在我的路线中我有:

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

我很习惯用 SQL JOIN 连接两个 table,但在这种情况下,我如何获得用户模型数据和配置文件模型数据?

所以它会像:用户: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); 生成以下内容:

个人资料table

用户table

如您所见,tables 和 user_username 的数据与 username 相同,但由于某些原因,在使用 with:: 时 link两者之间就是没见过。

使用with()方法

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

或者

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