简体   繁体   中英

how to create relationship in laravel by model id and model types

I have a table that is associated with one, using their id and id table type

table 1

id, type_id, type, count, crteated_at, updated_at

other tables

id name, ....... crteated_at updated_at

model type ids

model2 = type_id = 2; model3 = type_id = 3;

In model 1

public function model2()
{
  return $this->belongsTo(model2::class, 'type_id', 'id');
}

public function model3()
{
  return $this->belongsTo(model3::class, 'type_id', 'id');
}
public function getModel(Request $request){

      if($request->get('model_type')==1){
       $mod = 'Model1'
       }
       if($request->get('model_type')==2){
         $mod = 'Model'
       }  

      $obj = new {Mod}()
      $result = $obj->get();

      return $result       
 }

I would do so

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM