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

david2000's avatar

I always have the same student which appears after each recording

When I add a recording, I always have the same student which appears ???

I think the problem is perhaps in my function index() ?

public function index(Request $request)
  {   
     $user = $request->user();
     $payments = Payment::query()
     ->when($user->hasRole('admin') !== true, function (Builder $query) use ($user) {
     $query->where('email', $user->email);
     })
     ->when($request->has('search'), function (Builder $query) use ($request) {
    $query->where('name', 'like', '%' . $request->input('search') . '%');
         })->with('students:id,name') 
    ->paginate(5);

    return view('admin.payments.index', compact('payments'))
    ->with('display_search', $user->hasRole('admin'));
    }

For information my table students has 3 fields (id, name, email) and the table payments has 6 fields (id, date_payment, number_seance, price, total, fk_student).

My relationships:

Model Student:

protected  $fillable = ['user_id', 'name', 'email', 'payment_id'];

public function payments(){

        return $this->hasMany('App\Payment', 'fk_student');
    }

  public function user()
    {
        return $this->belongsTo('App\User', 'id', 'payment_id');
    }

Model Payment:

protected  $fillable = ['date_payment', 'fk_student', 'number_seance',  'price', 'total'];
    
    public function students(){

    return $this->belongsTo('App\Student', 'fk_student');
    }

    public function user()
    {
        return $this->belongsTo('App\User', 'id', 'payment_id');
    }

Model User

public function students()
    {
        return $this->hasOne('App\Student', 'user_id', 'id');
    }

    public function payments()
    {
        return $this->hasOne('App\Student', 'payment_id', 'id');
    }

Index blade

<th>Date payment</th>
<th>Number seance</th>
<th>Price</th>
<th>Total</th>
 <th>Name</th>

</tr>
</thead>
@foreach($payments as $payment)
<tr>
   <td> {{$payment->date_payment->format('d/m/Y') }}</td>
   <td> {{$payment->number_seance}}</td>
   <td> {{$payment->price}}</td>
   <td> {{$payment->total}}</td>
   <td> {{$payment->students->first()->name}}</td> 
   <td>

Thank you for your help.

0 likes
5 replies
david2000's avatar

I have edtied my first message.

Thank you

Snapey's avatar

Payment model, change this to student() and change it in the query and view. You can also drop first()

    public function student()
    {
        return $this->belongsTo('App\Student', 'fk_student');
    }

payments table should have student_id column ?

then

   <td> {{$payment->student->name}}</td> 

and make sure you use eager loading in the controller

1 like
david2000's avatar

Thank you a lot, but I think my model "Student" is doesnt' correct ?

Snapey's avatar
Snapey
Best Answer
Level 122

remove the fk_student

1 like

Please or to participate in this conversation.