Laravel 5.3 + Sentinel:Builder.php第2450行中的BadMethodCallException

时间:2016-12-24 02:47:37

标签: php laravel laravel-5 laravel-5.3 sentinel

我试图通过互联网上的一些指南来构建我的第一个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);

有什么想法吗?谢谢和欢呼

1 个答案:

答案 0 :(得分:0)

  • 在.config / cartalyst.sentinel.php中,将'model' => 'Cartalyst\Sentinel\Users\EloquentUser'更改为'model' => 'App\User'以使用定义了addresses关系的用户模型
  • 在./app/User将User extends Authenticatable更改为User extends \Cartalyst\Sentinel\Users\EloquentUser,以便将哨兵的用户扩展到您应用的用户模型
  • 最后,您的控制器代码现在应该是

`$ address = new CustomerAddress();  $ address-&gt; address = $ request-&gt; input('address');

$请求 - &gt;用户() - &GT;地址() - &GT;保存($地址);`

一切都应该是桃子的