php - hasMany returns null -
i'm on laravel 4.2, trying objects associative relation yet reason getting null
.
models:
class keg extends eloquent { protected $fillable = ['beer_distribution_id', 'status', 'keg_size']; public function beer_distribution() { return $this->belongsto('beerdistribution'); } }
class beerdistribution extends eloquent { protected $fillable = ['beer_id', 'distributor_id']; public function kegs() { return $this->hasmany('keg'); } }
my query:
$distirbution = keg::find($tap->keg_id)->beer_distribution
this query returns null: know keg object found , know object has beer_distribution_id.
i tried specifying foreign key in model so:
class keg extends eloquent { protected $fillable = ['beer_distribution_id', 'status', 'keg_size']; public function beer_distribution() { return $this->belongsto('beerdistribution', 'beer_distribution_id'); } }
i thought might necessary since model camel case.
i know super simple, , in fact querying many relations succesfully through out application, reason 1 not working. thoughts on might missing?
the problem function name of relationship. laravel expects in camel case: beerdistribution()
.
change , should good. (you don't have specify foreign key, laravel convert camelcase snake_case)
Comments
Post a Comment