我试图在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 –