//ads:
?>
Access relation of pivot table in Laravel
PHP Snippet 1:
class ProductBill extends Pivot {
public function process() {
return $this->belongsTo(Process::class);
}
}
PHP Snippet 2:
class Bill extends Model {
function products() {
return $this->belongsToMany(\App\Product::class)
->withPivot('process_id')
->using(ProductBill::class)
->withTimestamps();
}
}
PHP Snippet 3:
<td>{{$product->pivot->process->name}}</td>
PHP Snippet 4:
public function processName($processId) {
return Process::where('id', $processId)->pluck('name')->first();
}
PHP Snippet 5:
<td>{{$product->processName($product->pivot->process_id) }}</td>
PHP Snippet 6:
Process::find($product->pivot->process_id)->name;
PHP Snippet 7:
$bill->load('productPivots', 'productPivot.process', 'productPivot.product');
@foreach($bill->productPivots as $productPivot)
<tr>
<td>{{$productPivot->product->barcode}}</td>
<td>{{$productPivot->process->name}}</td>
</tr>
@endforeach