mercredi 8 septembre 2021

Laravel fetch all checkboxes include checked from pivot table with additional column

I'm trying to fetch all my checkboxes with additional column from pivot table.

Model: Stock

class Stock extends Model
{
   public function products(){
    return $this->belongsToMany('App\Product','products_stock')->withPivot('qtySpent')->withTimestamps();
}
}

Model: Products

class Product extends Model
{
   public function stocks(){
    return $this->belongsToMany('App\Stock','products_stock')->withPivot('qtySpent')->withTimestamps();
}
}

Pivot: product_stock

Schema::create('product_stock', function (Blueprint $table) {
         $table->bigIncrements('id');        
    $table->integer('product_id');
    $table->foreign('product_id')
          ->references('id')
          ->on('products')->onDelete('cascade');
   
    $table->integer('stock_id');
    $table->foreign('stock_id')
          ->references('id')
          ->on('stocks')->onDelete('cascade');               
            $table->integer('qtySpent')->nullable();
            $table->timestamps();
    });

For the moment, I have 2 records in my pivot table:

 id| product_id | stock_id | qtySpent
    =====================================
    1   1              1          100
    2   1              2          200

But, in my stocks table I have 3 records:

 id| restaurant_id | name     | qty | qtyType
    =====================================
    1   16         Mozzarela    20    KG
    2   16         Olives       10    KG
    3   16         Beer         100   Liter

Now, I need to show all these stocks records as checkboxes linked for each with qtySpent(from pivot table) as textboxes. But, the stocks records that are in pivot table to be checked and qtySpent that are linked with these stocks records to be filled with their values.

For the moment I have these code to fetch records.

$thisRestaurantId = Auth::user()->sFor; 
@foreach (Product::where('toRes',$thisRestaurantId)->get() as $pro)
    @foreach(Stock::where('restaurant_id','=', $thisRestaurantId)->get() as $stock)
    <tr style="display: inline-grid;">
        <td>
            <input data-id="" type="checkbox" class="ingredient-enable"   >
            <label class="form-check-label"></label>
        </td>
        <td><input value=""  data-id="" name="stocks[]" type="text" class="ingredient-amount form-control" placeholder="Menge">
        </td>
    </tr>                                  
    @endforeach
@endforeach

This code is showing 3 checkboxes(two from them are checked) and it is showing 3 Empty textboxes. So, the qtySpent values are not showing for these 2 checkboxes checked(above(stocks) as checkboxes, bottom(qtySpent) as textboxes).

|✔| Mozzarela      |✔| Olives        |  | Beer
    
|    |             |    |           |      |

In this case, the result should be like this:

|✔| Mozzarela      |✔| Olives        |  | Beer

|100  |             |200  |           |      |

Thank you in advance




Aucun commentaire:

Enregistrer un commentaire