php - how to adapt one to many in my controller? -
i have 2 models (user , services). want display services of user. action show me services :
public function index() { $services = service::all(); // load view , pass nerds return view::make('services.index')->with('services', $services); }
and in user model add function :
public function service() { return $this->hasmany('service'); }
then in service model :
public function user() { return $this->hasone('user'); }
so please if has idea, appreciative :)
the relationships looking for 1-to-many relationship hasmany()
, belongsto()
.
with in mind, in service
model, define relationship so...
public function user() { return $this->belongsto('user'); }
it doesn't matter in scenario because aren't using it, might save confusion later.
now users's services, can use user
model.
$services = user::find(1)->services;
or if want logged in users's services...
$services = auth::user()->services;
Comments
Post a Comment