如果 id 在 Laravel 中有记录,我想获取值或显示零?

I want to get value or zero for display if id has a record in Laravel?

这是关于Laravel的查询。我想从数据库中获取一些数据。我使用了下面的代码,

$ids = [1, 2, 3, 4, 5]
$stock = $item->stocks->whereIn('paperorder_paper_id', $ids)->where('status', 'qc-pass')->get();

简化后我的问题是,如果 id no 1 有一条记录,它应该显示该值,否则应该显示零(“0”)。因为我想把数据放到datatable里面。请看图片。然后清楚地明白我的问题是什么。

预计:

问题,我得到了结果

股票数据库

    Id  Value
    1       
    2   120
    3   50
    4   
    5   1000

下面是我在 blade 文件中的真实代码。但是使用真实代码解释起来很复杂。请帮我解决这个问题。

<td class="border-t border-gray-200 border-dashed">
                                @php
                                    $paperorderids = [];
                                    $paperorder = $item->paperorders->where('paperorder_id', $item->id);

                                    foreach ($paperorder as $key => $value) {
                                        $paperorderids[] = $value->id;
                                    }

                                    // Output: $ids = [1, 2, 3, 4, 5]

                                    $stock = $item->stocks->whereIn('paperorder_papers_id', $paperorderids)->where('status', 'qc-pass');
                                    // Output: 2 arrays only becasue others id has not record thats why I want zero for that.

                                    foreach ($stock as $key => $val) {
                                        echo $val->qty ? $val->qty : 0; 
                                    }
                                    // 
                                @endphp

                                {{-- @foreach ($item->paperorders as $paperorder)

                                    @foreach ($item->stocks as $stock)
                                        @if ($paperorder->id == $stock->paperorder_papers_id && $stock->status === 'qc-pass')
                                            <span class="flex items-center px-6 py-0 text-gray-700">
                                                {{ $stock->qty }}
                                            </span>
                                        @else
                                            <span class="flex items-center px-6 py-0 text-gray-700">
                                                0
                                            </span>
                                        @endif

                                    @endforeach


                                @endforeach --}} 


                            </td>

应该就可以了。

echo in_array($val->id, $ids) ? $val->qty : 0;

感谢您的帮助,这是我的最终代码,它对我有用。

@php
 $stock = $item->stocks->where('status', 'qc-pass');
 $stockids = [];
 foreach ($stock as $key => $sids) { $stockids[] = $sids->paperorder_papers_id;}
@endphp

@foreach ($item->paperorders as $val)
  <span class="flex items-center px-6 py-0 text-gray-700">
     @if (in_array($val->id, $stockids))
     {{ $item->stocks->where('paperorder_papers_id', $val->id)->where('status', 'qc-pass')->sum('qty'); }}
      @else
        0
     @endif
   </span>
@endforeach

Result