how to save many data in pivot table in laravel

bakir_nano

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)

enter image description here


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');
}
Donkarnash

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');
}

Laravel Docs - Eloquent Relationships - Attaching Detaching

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel Many to Many, how to insert/get data inside the pivot table?

Saving Data into Pivot Table - Many To Many Relationship - Laravel

Laravel get data from pivot table many to many relationship

Laravel: limit many-to-many eager loading by pivot table data

Store data in Pivot table in many to many relationship in laravel crud

How to insert pivot table in case of many to many relation? (Laravel 5.3)

Laravel 8: How to seed a pivot table in many to many relationship

How to query a pivot table data in MySQL (many to many relationship)

How to find data on pivot table of a Many To Many relationship

How to update data of pivot table in Many To Many relationship

Get data from pivot table after save in Laravel 5.7

laravel - How to get data by pivot table

Laravel Many To Many Pivot Table different Database

Laravel Many-to-Many Pivot Table

Laravel Removing Pivot data in many to many relationship

retrieve data from pivot table many to many relation ship laravel 5

how to save many records in a table Laravel Eloquent (no relationship)

laravel one-to-many on pivot table

Laravel Adding One-To-Many on a Pivot table

Accessing Pivot table data in laravel

Laravel attach data to pivot table

Laravel - Save additional column value to pivot table

Laravel error on save pivot table in transaction mode

Laravel 5.8: How to show a column information of a pivot table in Many To Many relationship

In Laravel, how can I access data from a pivot table?

How to add data to additional column in pivot table in laravel

Laravel: How do I get data from this pivot table?

How to Retrieve data from Pivot table in Laravel using Datatables

How to return the data of a pivot table in the same resource LARAVEL