Laravel多态关系通过动态类型

i used polimorphic relation in laravel but my problem is, how to pass relation_type dynamically (Example:When i want to create tickets for Organization pass Organization Model namespace or for Contact pass Contact Model namespace).

This is my model

class Ticket

public function relation(){
  return $this->morphTo();
}

class Organization

public function tickets(){
   return $this->morphMany('Ticket','relation');
}

class Contact

public function tickets(){
   return $this->morphMany('Ticket','relation');
}

i don't now it is possible but i want a function that accept two param

public function ticketRelation($model, $Id){
     $modelNamespace = $model::findById($id); 
     $ticket = new Ticket();
     $ticket->save();
     $modelNamespace->tickets()->save($ticket);    
}

In this casse a $model param should be accept Organization or Contact to associate new ticket And if i realize this i need only one route that accept a model and an Id to associate new ticket.How to pass an class like a param in this method ?

All you need to do to link a Ticket to either Organization or Contact is:

$organization = new Organization;
$organization->save();

$contact = new Contact();
$contact->save();

$organizationTicket = new Ticket;
$organizationTicket->relation()->associate($organization);
$organizationTicket->save();

$contactTicket = new Ticket;
$contactTicket ->relation()->associate($contact);
$contactTicket ->save();

Yes, it is possible to achieve what you want with your function.

Since PHP 5.3 it is possible to dynamically call class static methods on a variable containing class name, so if $model contains a valid namespaced class name you can just do:

public function ticketRelation($class, $id){
     $model= $class::findOrFail($id); 
     $ticket = new Ticket();
     $ticket->relation()->associate($model);
     $ticket->save();
}