Answer the question
In order to leave comments, you need to log in
How to automate checks for the existence of a record in a relationship?
Laravel. Ubuntu server. Model User. Associated with many models like so
public function files()
{
return $this->hasMany('App\Models\File', 'user_id', 'id');
}
public function hasModel($model)
{
return count($this->$model) > 0;
}
if ($user->hasModel('files')) {
..
}
count(): Parameter must be an array or an object that implements Countable
public function hasModel($model)
{
return $this->$model()->exists();
if (!empty($this->$model()->first())) {
return true;
} return false;
return $this->$model()->first()->count() > 0;
return count($this->$model) > 0;
}
public function hasFile()
{
// Что вы здесь обычно пишете?
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question