我試圖在Laravel中連接兩個模型中的一個默認用戶模型和另一個地址模型,但我在連接這兩個模型時遇到了一些問題。如何在Laravel中連接用戶和地址模型
用戶模型是
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
use App\Address;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function address()
{
return $this->hasOne('App\Address');
}
}
和地址型號是
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\User;
use Session;
class Address extends Model
{
protected $table = 'usercontacts';
public function address()
{
return $this->belongsTo('App\User');
}
}
請問這個正常工作?任何提示都會有所幫助。在此先感謝
良好的起點,https://laravel.com/docs/5.4/eloquent-relationships –