Search code examples
laraveleloquentvoyager

Laravel Eloquent get data where comparison is true


I am creating a mini inventory stock for a fashion store. I am using Laravel/Voyager with BREAD and everything fine. I have 2 tables Sizes and Products with a column in common product_code.

I would like to get the results from Sizes where column product_code = Product 'product_code'. I have this query in the controller:

$product_code = Product::all();
$allSizes = Size::where('product_code', ($product_code->product_code));

and in browse.blade.php I have:

@foreach ($allSizes as $size)
    <tr>
        <td align="right">{{$size->size_name}}</td>
        <td align="right">{{$size->stock}}</td>
    </tr>
@endforeach 

I guess the where statement is not working as supposed to. I want to get the corresponding stock based on product_code for each size from table Sizes


Solution

  • Try This

    $product_code = Product::pluck('product_code')->toArray();
    $allSizes = Size::whereIn('product_code', $product_code)->get();
    

    and in browse.blade.php:

    @foreach ($allSizes as $size)
        <tr>
            <td align="right">{{$size->size_name}}</td>
            <td align="right">{{$size->stock}}</td>
        </tr>
    @endforeach