Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MahmoudAdelAli's avatar

Delete or update one record from has many relation

hello , i have 2 issues here with has many relationship , first i want to delete on record not all so i use detach

 public function inviteDestroy(Round_invitation $invitation, $id)
  {
    $invitation->invitations()->detach($id);
    return back()->with('success', "Invitation Has Been Deleted");
  }

but i get error

 Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::detach() 

so after search i found the detach for pivot tables but to delete use where , so what's right way ? , after that i using update like that

Form         action="{{route('invitations.invite.updated',$province_invitation)}}">
And Controller
  public function inviteUpdate(InvitationUpdateRequest $request, Invitation $province_invitation)
  {
    $attributes = $request->validated();
    $province_invitation->update($attributes);
    return back()->with('success', "Invitation Has Been Updated");
  }

so here i call the all model to update where id was sent from the blade normally but i think the right way is update from relation but after many search i don't find any way to update one record

0 likes
20 replies
vincent15000's avatar

The detach() method should work.

To help you it would be helpful to know your models and relationships.

MahmoudAdelAli's avatar

@vincent15000

Invitation
'category', 'description', 'number', 'minimum_score', 'date', 'round_invitation_id'
Round_invitation
'name', 'description', 'img', 'provinces', 'type', 'federal'

and in Round Invitation Model

  public function invitations()
  {
    return $this->hasMany(Invitation::class);
  }
tykus's avatar

detach is a BelongsToMany relationship method; so it will not work on a HasMany. Do you want to leave the Invitation with NULL for the round_invitation_id, or do you want it deleted?

MahmoudAdelAli's avatar

@tykus

Invitation has round_invitation_id,

and Round invitation has many Invitations,

so i want to delete one invitation from round invitations ,

tykus's avatar

@MahmoudAdelAli does not answer the question... what happens to the invitations record in the database?

MahmoudAdelAli's avatar

@tykus

  public function inviteDestroy(Round_invitation $invitation, $id)
  {

    $invitation->invitations()->whereId($id)->delete();
    return back()->with('success', "Invitation Has Been Deleted");
  }

and this code should work but i redirect back with success and nothing happen

MahmoudAdelAli's avatar

@tykus i don't know how to explain this so i'll put the table columns better , the father is

Round_invitation  ( This Is Separated table not pivot or relation between round and invitation)

'name', 
'description', 
'img',
 'provinces',
 'type',
 'federal'

The Child is

Invitation
    'category', 'description', 'number', 'minimum_score', 'date', 'round_invitation_id'

so why i need to set invitation to null i want to delete invitation from round of invitations

MahmoudAdelAli's avatar

@tykus and i know the name not right but it' should be (rounds of invitations ) so i named it Round_invitations inside database

tykus's avatar

@MahmoudAdelAli am I not being clear about the question???

What should happen to the Invitation record in the database?

1 like
MahmoudAdelAli's avatar

@tykus the question is clear but i think i talk different language :D the answer is : deleted whole this question cause i want to delete record from relation and detach not work ! so i use where

 $rounds->invitations()->whereId($id)->delete();

and it not works too .

tykus's avatar

@MahmoudAdelAli ok, finally!

Is the Route Model binding working correctly - what is dd($rounds) ? And does the Invitation actually belong to the Round_invitation

Of course, you also have the option to delete like this:

Invitation::where('id', $id)->where('round_invitation_id', $rounds)->delete();
MahmoudAdelAli's avatar

@tykus

App\Models\Round_invitation {#2152 ▼
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #classCastCache: []
  #attributeCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: array:6 [▶]
  #guarded: array:1 [▶]

And Model is

class Round_invitation extends Model
{
  protected $fillable = [
    'name', 'description', 'img', 'provinces', 'type', 'federal'
  ];

  public function province()
  {
    return !is_null($this->img) || !empty($this->img) ? asset('storage/' . $this->img) : default_avatar();
  }

  public function invitations()
  {
    return $this->hasMany(Invitation::class);
  }
}

tykus's avatar
tykus
Best Answer
Level 104

The Route-Model binding is not working correctly because : +exists: false - you are just getting a new instance of Round_invitation (which has no id).

Make sure that the route URL wildcard {round} matches the controller action parameter $round:

Route::delete('/round/{round}', [WhateverController::class, 'inviteDestroy']);
 public function inviteDestroy(Round_invitation $round, $id)
1 like
MahmoudAdelAli's avatar

@tykus thank you, now it's worked with

 $rounds->invitations()->whereId($nvitation)->delete();

so i'll use this way instead the detach right ?

tykus's avatar

@MahmoudAdelAli no worries, mark the thread closed in that case.

detach is not an option since this is a hasMany relation

1 like
MahmoudAdelAli's avatar

@tykus thank you for helping and your patience , but i have a little question in update case i call the direct Model for invitation to update

  public function inviteUpdate(InvitationUpdateRequest $request, Invitation $invitation)
  {
    $attributes = $request->validated();
    $invitation->update($attributes);
    return back()->with('success', "Invitation Has Been Updated");
  }

this right way or there's way with relationship ?

tykus's avatar

@MahmoudAdelAli where would the relation be used here - there is only the Invitation model in scope?

You could check out scoped model bindings to ensure that the Invitation belongs to the Round_invitation, and then update directly on the Invitation instance as normal.

1 like

Please or to participate in this conversation.