vendredi 29 septembre 2017

Laravel sync throws integrity exception even if extist

I'm trying to link all my TicketMethod objects to all the Event objects:

foreach (Event::all() as $event) {
   $event->ticketMethods()->attach([1, 2]);
}

The 1 and 2 are the only ids in the db for TicketMethod.

In my Event I have the following code:

public function ticketMethods()
{
        return $this->belongsToMany('App\Models\TicketMethod', 'event_ticket_methods');
}

Exception:

[Illuminate\Database\QueryException]                                         
  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update   
  a child row: a foreign key constraint fails (`tickets`.`event_ticket_method  
  s`, CONSTRAINT `event_ticket_methods_event_id_foreign` FOREIGN KEY (`event_  
  id`) REFERENCES `events` (`id`) ON DELETE CASCADE) (SQL: insert into `event  
  _ticket_methods` (`event_id`, `ticket_method_id`) values (@/Y�� ���ʫB{�1�,  
   1), (@/Y�� ���ʫB{�1�, 2)) 

The event_id fields are in binary format, which causes the question marks.

I don't get the error, as the event exists as I just retrieved them...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire