php - How to get reference table data in Laravel 5.1 -
i created model account
accountgroup_id
refer account_group
model. call route
this
route::get('test', function () { return \app\account::get()->account_group; });
account model has belogsto relationship account_group
class account extends model { protected $fillable = ['accountgroup_id', 'accountno', 'accountname','address','contactno']; public function account_group() { return $this->belongsto('app\account_group'); } }
account_group model has hasmany relationship account
class account_group extends model { protected $fillable =['name','under']; public function account() { return $this->hasmany('app\account','accountgroup_id'); } }
but after calling route; got following error.
undefined property: illuminate\database\eloquent\collection::$account_group
first, second class should named accountgroup
.
and reading error give clue what's - \app\account::get()
returns collection of account
objects, each of have accountgroup
. need choose specific account
want, you'll able access account_group
property on it:
\app\account::find(1)->account_group; // account id 1 \app\account::first()->account_group; // first account record
Comments
Post a Comment