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

rojonunoo's avatar

Querying relationship

I am new to Laravel and i have these relationship

public function fees(){
    return $this->hasMany('App\Fees');
} 

public function up() {

    Schema::create('fees', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('student_id')->unsigned();
        $table->string('payment_method');
        $table->string('bank_name')->nullable();
        $table->string('account_no')->nullabe();
        $table->decimal('balance');
        $table->decimal('amount');
        $table->date('date');
        $table->timestamps();


        $table->foreign('student_id')
            ->references('id')->on('students')
            ->onDelete('cascade')
            ->onUpdate('cascade');

    });

public function students()

{

    return $this->belongsTo('App/Student');

}

public function up() {

    Schema::create('students', function (Blueprint $table) {

        $table->engine = 'InnoDB';

        $table->increments('id');
        $table->string('name');
        $table->date('dob');
        $table->string('last_school_attended');
        $table->string('avatar')->default();
        $table->decimal('student_fees');
        $table->softDeletes('deleted_at');
        $table->timestamps();

    });

}

i want to get all students who have data stored in the fees table i tried this but i get everything i can't seem to get singled out values like getting only the amount coloumn

public function index()

{

    $students = Student::all();
    $payments = Fees::all();


    foreach($students  as $student)
    {
        if(count($student->fees))
        echo $student->fees;
    }

}

when i try to do $student->fees->amount says Undefined property: Illuminate\Database\Eloquent\Collection::$amount

0 likes
6 replies
richpeers's avatar

edit: i miss read the queation. will edit properly later.

assuming those relationships are in the models, and you are actually passing the value to the database via say a form, make sure to set all the attributes that are mass assignable in the models like :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'dob',
        'student_fees'
    ];

    /**
     * Get the fees for the student.
     */
    public function fees()
    {
        return $this->hasMany('App\Fees');
    } 

    
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Fees extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'payment_method',
        'bank_name',
        'account_no',
        'balance',
        'amount',
        'date'
    ];

    /**
     * Get the student the fees belongs to.
     */
    public function student()
    {
        return $this->belongsTo('App\Student');
    } 

    
}

reference https://laravel.com/docs/5.4/eloquent#mass-assignment

also,

$table->unsignedInteger('student_id')->unsigned();

should be

$table->integer('student_id')->unsigned();
rojonunoo's avatar

@richpeers the fees table database has data inside and i can't seem to fetch the student details out

Schema::create('fees', function (Blueprint $table) {

    $table->increments('id');
    $table->unsignedInteger('student_id')->unsigned();
    $table->string('payment_method');
    $table->string('bank_name')->nullable();
    $table->string('account_no')->nullabe();
    $table->decimal('balance');
    $table->decimal('amount');
    $table->date('date');
    $table->timestamps();


    $table->foreign('student_id')
        ->references('id')->on('students')
        ->onDelete('cascade')
        ->onUpdate('cascade');


});

$payments = Fees::all();

    foreach($payments as $payment)  {
        echo  $payment->student_id .'   '. $payment->amount .  '<br>';

}

but i can't get the student's name through the id.

i hope my question is clearer now ..:-(

igsavenko's avatar
$students = Student::with('fees')->all();

foreach($students as $student) {
    echo  '<b>'.$student->name.'</b><br>';
    foreach ($student->fees as $fee) {
         echo $fee->amount.'<br>';
    }
    echo '--------';
}
rojonunoo's avatar

@igsavenko hi i tried thats the error i am getting Call to undefined method Illuminate\Database\Query\Builder::all()

richpeers's avatar
Level 8

try ->get() instead of ->all()

$students = Student::with('fees')->get();

then as per @igsavenko above

foreach($students as $student) {
    echo  '<b>'.$student->name.'</b><br>';
    foreach ($student->fees as $fee) {
         echo $fee->amount.'<br>';
    }
    echo '--------';
}
2 likes

Please or to participate in this conversation.