SaschaK
SaschaK

Reputation: 180

Laravel delete relationship and create new in one way?

Hi guys is it possible to do following code in one way?

    $Project->deadline()->delete();
    $Project->deadline()->create($deadline);

Project Model:

public function deadline()
{
    return $this->hasOne('App\Models\UserProjectDeadline','projects_id','id');
}

Upvotes: 0

Views: 87

Answers (1)

Cerwyn
Cerwyn

Reputation: 86

Seems there's no methods for deleting and creating in one line of code, but Laravel offer functions updateOrCreate(array $attributes, array $values = []) that can be used for hasOne relationships.

$Project->deadline()->updateOrCreate($attributes, $deadline); // To update based on the attributes

Or if you want to change/update all the data without any specified attributes

$Project->deadline()->updateOrCreate([], $deadline); // To update all the data

Upvotes: 3

Related Questions