i want to save two data (qte and total) in pivot table, how can i do that
here my code
artical model
class Artical extends Model{
protected $table = 'artical';
protected $primaryKey = 'ida';
protected $fillable = [ 'num_art' , 'designation', 'unite','prix_unit',];}
devis model
class Devis extends Model{
protected $table = 'Devis';
protected $primaryKey = 'idd';
protected $fillable = ['type', 'lot', 'prix_total_avant_tax', 'tax_perse', 'tax_amount', 'prix_total_apre_tax', 'notes',];
public function articals()
{
return $this->belongsToMany(Artical::class)->withPivot(['qte'])->withPivot(['total']);
}}
devis_artical table (pivot tavle)
store function
public function store(Request $request)
{
$Devis = Devis::create($request->all());
$articals = $request->input('articals', []);
$quantité = $request->input('quantité', []);
$total = $request->input('total', []);
for ($product=0; $product < count($articals); $product ) {
if ($articals[$product] != '') {
$Devis->articals()->attach($articals[$product], ['qte'=> $quantité[$product]], ['total'=> $total[$product]]);
}
}
return redirect('/Devis');
}
CodePudding user response:
You should read the Laravel documentation - it is one of the best and good source for learning.
class Devis extends Model
{
protected $table = 'Devis';
protected $primaryKey = 'idd';
protected $fillable = ['type', 'lot', 'prix_total_avant_tax', 'tax_perse', 'tax_amount', 'prix_total_apre_tax', 'notes',];
public function articals()
{
return $this->belongsToMany(Artical::class)->withPivot('qte', 'total');
}
}
Laravel Docs - Eloquent Relationships - Retrieving Intermediate Table Columns
And then in controller
public function store(Request $request)
{
$Devis = Devis::create($request->all());
$articals = $request->input('articals', []);
$quantité = $request->input('quantité', []);
$total = $request->input('total', []);
for ($product=0; $product < count($articals); $product ) {
if ($articals[$product] != '') {
$Devis->articals()->attach($articals[$product], [
'qte'=> $quantité[$product],
'total'=> $total[$product]
]);
}
}
return redirect('/Devis');
}