Ad
I Can Not Access The Array That I Attached To My Pivot Table?
I am using Many to Many relationships between Students
and Packages
which generate student_package
pivot table. And I attached some extra variables, but I can not access them in my view.
Here is my code
Controller
$student->packages()->attach($request->package_id, ['paid' => '1','transaction_id'=>$transaction_id, 'amount' => $request->amount]);
View
@foreach ($student->packages as $package)
<tr>
<td> {{$package->id}} </td>
<td> {{$package->name}} </td>
<td> {{$package->amount}} </td>
<td> {{$package->no_hours}} </td>
<td> {{$package->transaction_id}} </td>
<td> {{$package->paid}} </td>
</tr>
@endforeach
I can't access the extra parameters only. the rest are accessible.
Ad
Answer
1) Add columns that you need to withPivot()
method of belongsToMany
relation:
public function packages()
{
return $this->belongsToMany(Package::class)->withPivot('paid', 'transaction_id', 'amount');
}
2) Then access pivot columns trough ->pivot
property.
@foreach ($student->packages as $package)
<tr>
<td> {{$package->id}} </td>
<td> {{$package->name}} </td>
<td> {{$package->pivot->amount}} </td>
<td> {{$package->no_hours}} </td>
<td> {{$package->pivot->transaction_id}} </td>
<td> {{$package->pivot->paid}} </td>
</tr>
@endforeach
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms
Ad