我试图通过互联网上的一些指南来构建我的第一个Laravel应用程序,我感觉我错过了一些明显的东西。这是代码。
错误
Builder.php第2450行中的BadMethodCallException:调用undefined 方法Illuminate \ Database \ Query \ Builder :: addresses()
用户-模型
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Sentinel;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'email', 'password',
];
protected $hidden = [
'password',
'remember_token'
];
public function addresses()
{
return $this->hasMany('App\CustomerAddress');
}
}
CustomerAddress模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class CustomerAddress extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
CustomerAddress控制器
<?php
namespace App\Http\Controllers;
use App\CustomerAddress;
use Illuminate\Http\Request;
class CustomerAddressController extends Controller
{
public function create(Request $request)
{
$address = new CustomerAddress();
$address->address = $request['address'];
$request->user()->addresses()->save($address);
}
}
在这段代码之后出现错误:
$request->user()->addresses()->save($address);
有什么想法吗?谢谢和欢呼
答案 0 :(得分:0)
'model' => 'Cartalyst\Sentinel\Users\EloquentUser'
更改为'model' => 'App\User'
以使用定义了addresses
关系的用户模型User extends Authenticatable
更改为User extends \Cartalyst\Sentinel\Users\EloquentUser
,以便将哨兵的用户扩展到您应用的用户模型`$ address = new CustomerAddress(); $ address-&gt; address = $ request-&gt; input('address');
$请求 - &gt;用户() - &GT;地址() - &GT;保存($地址);`
一切都应该是桃子的