php - Laravel - Count registrations status per event -


struggling relationships - using laravel 5.4, have 3 tables

  • event

    • id
    • name
    • other fields..
  • registration

    • id
    • event_id
    • status_id
    • other fields...
  • status

    • id
    • name
    • other fields...

status model

public function registrations()     {         return $this->hasmany(registration::class);     } 

event model

public function registrations()     {         return $this->hasmany(registration::class);     } 

registration model

public function statuses()     {         return $this->belongsto(status::class, 'status_id');     }      public function events()     {         return $this->belongsto(event::class, 'event_id');     } 

and eventscontroller looks

public function findregistrations($id)     {         $viewevent = event::find($id)->registrations;         return view('events.details', compact('viewevent'));      } 

i need count number of statuses per event, because have different statuses

  • new
  • reviewed
  • approved
  • rejected

    and need display each status number on blade template, example

  • new (5)

  • reviewed (9)
  • approved (4)
  • rejected (10)

how accomplish using eloquent?

you use db like

add on top of eventscontroller :

use db; 

then :

public function getstatusforevent($id) {     $status = db::table('status')         ->join('registrations', 'registrations.status_id', '=', 'status.id')         ->where('registrations.event_id', '=', $id)         ->get();      return $status;  } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -