Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Laravel check if related model exists

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 639
    Comment on it

    Below is the situation which I want to solve

    I have an Eloquent model which has a related model:

    public function option() {
        return $this->hasOne('RepairOption', 'repair_item_id');
    }
    
    public function setOptionArrayAttribute($values)
    {
        $this->option->update($values);
    }
    

    when I create the model, it does not necessarily have a related model. When I update it, I might add an option, or not.

    To solve this problem there we go with generic solution working on all the relation types:

    if (count($model->relation))
    {
      // exists
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: