Hello,
I'm trying to fetch all my checkboxes with additional column from pivot table.
Model: Stock
Model: Products
Pivot: product_stock
For the moment, I have 2 records in my pivot table:
But, in my stocks table I have 3 records:
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.
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).
In this case, the result should be like this:
Thank you in advance
I'm trying to fetch all my checkboxes with additional column from pivot table.
Model: Stock
PHP:
class Stock extends Model
{
public function products(){
return $this->belongsToMany('App\Product','products_stock')->withPivot('qtySpent')->withTimestamps();
}
}
Model: Products
PHP:
class Product extends Model
{
public function stocks(){
return $this->belongsToMany('App\Stock','products_stock')->withPivot('qtySpent')->withTimestamps();
}
}
Pivot: product_stock
Code:
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:
Code:
id| product_id | stock_id | qtySpent
=====================================
1 1 1 100
2 1 2 200
But, in my stocks table I have 3 records:
Code:
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.
PHP:
$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="{{ $stock->id }}" type="checkbox" class="ingredient-enable" {{ $pro->stocks->contains('pivot.stock_id', $stock->id) ? 'checked' : ''}} >
<label class="form-check-label">{{$stock->name}}</label>
</td>
<td><input value="{{ $stock->pivot->qtySpent ?? null }}" {{ $pro->stocks->contains('pivot.qtySpent', $stock->id) ? null : 'disabled' }} data-id="{{ $stock->id }}" name="stocks[{{ $stock->id }}]" 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).
Code:
|✔| Mozzarela |✔| Olives | | Beer
| | | | | |
In this case, the result should be like this:
Code:
|✔| Mozzarela |✔| Olives | | Beer
|100 | |200 | | |
Thank you in advance