【问题标题】:I want to check name of that particular company's shift exist or not?我想检查该特定公司的班次名称是否存在?
【发布时间】:2021-09-29 13:29:34
【问题描述】:

我有多对多关系,我正在尝试检查这家公司的班次名称是否已存在于数据库中。

关系如下。

public function shifts()
{

        return $this->belongsToMany('App\Models\Shift');

}

public function companies()
{

      return $this->belongsToMany('App\Models\Company');

}

【问题讨论】:

  • 有什么问题,你的代码在哪里,你试过什么?你写,我们帮忙,我们不给你写代码。

标签: php database laravel eloquent eloquent-relationship


【解决方案1】:

您可以通过以下方式进行检查:

$company = Company::find(id)->with('shifts')->where('shiftname','shiftname')->get();

它将返回记录或null。

【讨论】:

    【解决方案2】:

    你可以检查一下

    $company = Company::find(1);
    
    $company->shifts()->where('shift_name', "yourShiftName")->get();
    

    【讨论】:

      猜你喜欢
      • 2023-01-04
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 2020-03-10
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多