Show the view?
Sep 28, 2019
5
Level 1
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.
Please or to participate in this conversation.