0

i'm using hasManyThrough eloquent relatoinship laravel, when i'm foreach data in view i got a mess output.

this is my output. https://ibb.co/jymR7GQ

Member model

public function rents()
{
    return $this->belongsTo('App\Rent','member_id');
}

public function memberHistory()
{
    return $this->hasManyThrough(
        'App\RentDetail',
        'App\Rent',
        'member_id',
        'rent_id',
        'id',
        'id',
    );
}

}

RentDetail model

public function rent()
{
    return $this->belongsTo('App\Rent','rent_id');
}

}

Rent model

public function users()
{
    return $this->belongsTo('App\User', 'user_id');
}

public function members()
{
    return $this->hasOne('App\Member','member_id');
}

public function books()
{
    return $this->belongsTo('App\Book','book_id');
}

public function rentDetail()
{
    return $this->hasMany('App\RentDetail','rent_id');
}

}

RentController

public function order()
{   
    $members = Member::all();
    return view('rents.order',compact('members'));
}

}

Order View

@foreach($members as $v)
                    {{$v->memberHistory}}<br>
                @endforeach

i want output similar like this https://ibb.co/L8QFMT5

1 Answers1

0
@foreach($members as $v)
       {{$v->memberHistory->id}}<br>
@endforeach

should give you id, like that you can get all data, and you need to wrap your data in blade/html syntax to create the table

something like

<table>
<tbody>
    @foreach($members as $v)
           <td>How to farming from: Book id{{$v->memberHistory->book_id}}</td>
           <td>{{$v->memberHistory->qty}}</td>
    @endforeach
</tbody>
</table>
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105