Created
July 19, 2019 03:00
-
-
Save bolechen/0c634a18d46a2aedbeb518a8176f8473 to your computer and use it in GitHub Desktop.
Check if belongsToMany relation exists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits; | |
use Illuminate\Database\Eloquent\Model; | |
trait HasPivotTrait | |
{ | |
/** | |
* 检查多对多关系是否存在. | |
* | |
* @param string $relation | |
* @param Model $model | |
* | |
* @return bool | |
*/ | |
public function hasPivot(string $relation, Model $model) | |
{ | |
return (bool) $this->{$relation}()->wherePivot($model->getForeignKey(), $model->{$model->getKeyName()})->count(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment