I would like to display a value ($seatsLeft) that is calculated by subtracting a records $seats value from the count of Students in the session (from the attribute)
This works for single records (@show) but i have no idea how to add it the array @index
Session model
'''
class Session extends Model
{
protected $fillable = ['name', 'day', 'timeslot', 'seats', 'location', 'description'];
public function students()
{
return $this->belongsToMany('App\Student')->withTimestamps();
}
public function getStudentsListAttribute()
{
return $this->students->lists('id')->all();
}
public function getStudentsInSessionAttribute()
{
return $this->belongsToMany('App\Student')->count();
}
}
'''
and the Sessionscontroller:
'''
namespace App\Http\Controllers;
use DB;
use Auth;
use App\User;
use App\Student;
use App\School;
use App\Session;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Utilities\Day;
use App\Http\Utilities\Timeslot;
use App\Http\Controllers\Controller;
class sessionsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
parent::__construct();
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$school_id = $this->user->school_id;
$students = Student::where('school_id', '=', $school_id)->get();
$days = DB::table('sessions')->select('day')->groupBy('day')->get();
$sessions = collect(DB::table('sessions')->orderBy('timeslot')->get());
$sessions = $sessions->groupBy('day')->toArray();
return view('sessions.index', compact('days', 'students'))->with('sessions', $sessions);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$session = Session::findOrFail($id);
$studentsInSession = $session->studentsInSession;
$seats = $session->seats;
$remainingSeats = $this->seatsLeft($seats, $studentsInSession);
$school_id = $this->user->school_id;
$students = Student::where('school_id', '=', $school_id)->get();
$students = $students->lists('name', 'id')->toArray();
return view('sessions.show', compact('session', 'students', 'studentsList', 'studentsInSession', 'remainingSeats'));
}
/**
* Return the number of seats left in a session
*
* @param $seats
* @param $studentsInSession
* @return integer
*/
public function seatsLeft($seats, $numberNewStudents)
{
$remainingSeats = $seats - $numberNewStudents;
return $remainingSeats;
}
}
very confused so any help welcome...