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 IlluminateDatabaseQueryBuilder::addresses()
User-Model
<?php namespace App; use IlluminateNotificationsNotifiable; use IlluminateFoundationAuthUser 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('AppCustomerAddress'); } }
CustomerAddress-model
<?php namespace App; use IlluminateDatabaseEloquentModel; class CustomerAddress extends Model { public function user() { return $this->belongsTo('AppUser'); } }
CustomerAddress-controller
<?php namespace AppHttpControllers; use AppCustomerAddress; use IlluminateHttpRequest; 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
Advertisement
Answer
- In .config/cartalyst.sentinel.php, change
'model' => 'CartalystSentinelUsersEloquentUser'
to'model' => 'AppUser'
to use your user model with theaddresses
relation defined - In ./app/User change
User extends Authenticatable
toUser extends CartalystSentinelUsersEloquentUser
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