存在值时显示徽章 Laravel

Show badge when value is exists Laravel

我有 2 个表:standartsquestions 其中问题的 standart_id 与标准的 id 相同.

当当前数据等于标准的id时,我想用徽章显示状态是填充还是空的。如何实现?

我已经尽力找到一些参考资料,但没有找到我想要的。

这是我的 blade:

    @foreach($standarts as $v)
        <tr>
            <td>Standart {{$v->id}}</td>
            <td class="text-capitalize">{{$v->name}}</td>
            <td>{{$v->year}}</td>
            <td class="text-capitalize">{{$v->type}}</td>
            @if($check == null)
                <td>
                    <span class="badge bg-danger tips"
                          data-bs-toggle="popover" title="Empty">
                        Empty
                    </span>
                </td>
            @else()
                <td>
                    <span type="button" class="badge bg-danger tips"
                          data-bs-toggle="popover" title="Filled">
                        Filled
                    </span>
                </td>
            @endif
        </tr>
    @endforeach

型号:

    public function index(Question $question)
    {
        $standarts = DB::table('standarts')
            ->select(DB::raw('DATE_FORMAT(created_at,"%Y") as year, id, user_id, type, name'))
            ->get();

        $check = Question::where('standart_id', '=', Standart::get('id'))->first();

        return view('admin.dashboard', compact('standarts', 'check'));
    }

使用 eloquent 关系:

class Standart extends Model
{
    public function questions()
    {
        return $this->hasMany(Question::class);
    }
}

控制器:

public function index(Question $question)
    {
        $standarts = Standart::with(['questions'])->get();

        return view('admin.dashboard', compact('standarts'));
    }

Blade 视图:

    @foreach($standarts as $v)
        <tr>
            <td>Standart {{$v->id}}</td>
            <td class="text-capitalize">{{$v->name}}</td>
            <td>{{$v->year}}</td>
            <td class="text-capitalize">{{$v->type}}</td>
            @if(!$v->questions->count())
                <td>
                    <span class="badge bg-danger tips"
                          data-bs-toggle="popover" title="Empty">
                        Empty
                    </span>
                </td>
            @else()
                <td>
                    <span type="button" class="badge bg-danger tips"
                          data-bs-toggle="popover" title="Filled">
                        Filled
                    </span>
                </td>
            @endif
        </tr>
    @endforeach