Laravel 5.3 + Sentinel: BadMethodCallException in Builder.php line 2450

947 views Asked by At

I'm trying to build my first Laravel application by following a few guides on the internet and I'm feeling I'm missing something obvious. Here is the code.

Error

BadMethodCallException in Builder.php line 2450: Call to undefined method Illuminate\Database\Query\Builder::addresses()

User-Model

<?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-model

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class CustomerAddress extends Model
{
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

CustomerAddress-controller

<?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);
    }
}

Error appears after this piece of code:

$request->user()->addresses()->save($address);

Any ideas? Thanks and cheers

1

There are 1 answers

4
kkyeboah On BEST ANSWER
  • In .config/cartalyst.sentinel.php, change 'model' => 'Cartalyst\Sentinel\Users\EloquentUser' to 'model' => 'App\User' to use your user model with the addresses relation defined
  • In ./app/User change User extends Authenticatable to User extends \Cartalyst\Sentinel\Users\EloquentUser to extend sentinel's user to your app's User model
  • Finally, your controller code should now be

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

$request->user()->addresses()->save($address);`

and everything should be peachy