Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

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

ediepolpa

New Coder
Hello,

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
 

New Threads

Buy us a coffee!

Back
Top Bottom