Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BeggarSo's avatar

Adding values to arrays

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...

0 likes
3 replies
jekinney's avatar

Holy controller batman. Eloquent, query builder. That code is inconsistent and will be a nightmare to update.

https://laravel.com/docs/5.2/helpers Has a lot of array helpers or visit the php Manuel.

Seeing your using the query builder, yet have eloquent relationships?

https://laravel.com/docs/5.2/collections Works wonders with collection objects.

methods Avalible with eloquent collections. https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Collection.html

Or create your own. https://laravel.com/api/5.2/Illuminate/Support/Collection.html

BeggarSo's avatar

Yeah - just trying out a few different things before tidying it up a bit.

BeggarSo's avatar

I get that it's nasty - so generally you would only use either eloquent or query builder? Not both at random times?

For the index, would iterating over the sessions array and adding a remainingSeats value with array_add work? Just to overload the controller a bit more for example - would something like this be appropriate?

''' $sessions = Session::all(); foreach ($sessions as $session) {

        $studentsInSession = $session->studentsInSession;
        $seats = $session->seats;
        $remainingSeats = $this->seatsLeft($seats, $studentsInSession);
        $session = $session->put('remainingSeats', $remainingSeats);   ------dies here 

    }

''' BadMethodCallException in Builder.php line 2161: Call to undefined method Illuminate\Database\Query\Builder::put()

Please or to participate in this conversation.