controller - How I can get the tuples in tables N to N in laravel? -


i have following data model

enter image description here

a technician can have many services , service can have many technical

tecnico model

class tecnico extends eloquent{ protected $table = 'tecnico'; protected $fillable = array('auth_token');  public function servicios(){     return $this->belongstomany('servicio', 'servicio_tecnico', 'idtecnico', 'idservicio'); } } 

servicio model

class servicio extends eloquent{ protected $table  = 'servicio';  public function detalleservicio(){     return $this->hasone('detalle_servicio', 'iddetalle_servicio'); }  public function tecnicos(){     return $this->belongstomany('tecnico', 'servicio_tecnico', 'idservicio', 'idtecnico'); } } 

i'm trying services of particular technician according "auth_token"

serviciocontroller

class serviciocontroller extends basecontroller{  public function obtenerservicios($auth){      if($auth != ''){         $tecnico = db::table('tecnico')->where('auth_token',$auth)->first();         if($tecnico != null){             $servicio = $tecnico->servicios;             $response = response::json($tecnico);             return $response;          }else{             $array = array('error' => 'notauthorizederror', 'code' => '403');             $response = response::json($array);             return $response;         }     }else{         $array = array('error' => 'invalidargumenterror', 'code' => '403');         $response = response::json($array);         return $response;     }  }  } 

route

route::get('servicio/{auth}', array('uses' => 'serviciocontroller@obtenerservicios')); 

error

enter image description here

how can technical services?

first: it's best use eloquent model query instead of db::table.

eg:

$tecnico = tecnico::with('servicio')->where('auth_token',$auth)->firstorfail(); 

the db query builder doesn't know eloquent relationships. eloquent knows query builder.

and second: don't pass access tokens in uri. not in segment , not in query string. use headers that.

eg:

route::get('servicio', 'serviciocontroller@obtenerservicios');  class serviciocontroller extends basecontroller {      public function obtenerservicios() {         // strip "bearer " part.         $token = substr(request::header('authorization'), 0, 7);         // ...     } } 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -