How to set $fillable attributes for another table with hasOne relationship in a Laravel/Ardent model?

3.1k views Asked by At

Suppose, I have these ardent models

class User extends \LaravelBook\Ardent\Ardent
{
     public $autoHydrateEntityFromInput = true;
     protected $fillable = array('username', 'password', 'address');
     protected $table = 'Users';
     public static $relationsData = array(
     'location'  => array(self::HAS_ONE, 'Location');
}

class Location extends \LaravelBook\Ardent\Ardent
{
     protected $fillable = array('address');
     protected $table = 'Locations';         
}

Now, when I write a controller code like this,

 $user = new User;
 $user->address = Input::get('address');
 $user->push();

It doesn't save the address data to address table

1

There are 1 answers

2
Dwight On

You don't show the Party model?

Furthermore, Input::get('address') does nothing, it just returns the address from the input.

I'm assuming here, but I suppose you'd want something like this:

$user = new User;
$user->locations()->create(Input::only('address'));

That will create a new location for the user, passing in the address from the input.


If you're trying to use Ardent's autohydration, this might do the trick:

// Autohydrate the location model with input.
$location = new Location;

// Associate the new model with your user.
$user->locations()->save($location);