Reputation: 4509
I have the following data model
I am trying to get a have a Servicio
given a Auth_Token
of a Tecnico
and id
of a Servicio
, but I get the following error appears
This is my code
Route
Route::post('servicio/download/{id}', array('uses' => 'ServicioController@pdf'));
Servicio model
class Servicio extends Eloquent{
protected $table = 'Servicio';
protected $fillable = array(
'RutaFoto1',
'RutaFoto2',
'RutaFoto3',
'RutaFoto4',
'FechaTermino',
'Latitud',
'Longitud'
);
protected $primaryKey = 'idServicio';
public function materialUsado(){
return $this->hasMany('Material_Usado', 'idMaterial_Usado');
}
public function tecnicos(){
return $this->belongsToMany('Tecnico', 'Servicio_Tecnico', 'Servicio_idServicio', 'Tecnico_idTecnico');
}
}
Tecnico Model
class Tecnico extends Eloquent{
protected $table = 'Tecnico';
protected $fillable = array('Auth_Token');
protected $primaryKey = 'idTecnico';
public function servicios(){
return $this->belongsToMany('Servicio', 'Servicio_Tecnico', 'Tecnico_idTecnico', 'Servicio_idServicio');
}
}
ServicioController
class ServicioController extends BaseController{
public function pdf($id){
$auth = Input::get('Auth_Token');
$tecnico = Tecnico::with('servicios')->where('Auth_Token',$auth)->firstOrFail();
if($tecnico != ''){
$servicios = $tecnico->servicios;
$servicio = $servicio->where('idServicio', $id)->first();
if($servicio != null){
$array = array(
'idServicio' => $servicio->idServicio,
'RutaPDF' => base64_encode($servicio->RutaPDF),
);
$response = Response::json($$array);
return $response;
}else{
$array = array('Error' => '', 'Code' => '');
return Response::json($array);
}
}else{
$array = array('Error' => 'NotAuthorizedError', 'Code' => '403', 'Message' => 'Tecnico inexistente');
$response = Response::json($array);
return $response;
}
}
}
how can I fix it?
Upvotes: 3
Views: 13686
Reputation: 146219
You are using this:
$tecnico = Tecnico::with('servicios')->where('Auth_Token',$auth)->firstOrFail();
In this case, firstOrFail
throws Illuminate\Database\Eloquent\ModelNotFoundException
exception if it doesn't find the requested model and hence it could be because the model with that Auth_Token
field is not available.
Make sure the Auth_Token
is right and that Auth_Token
is available in your database. You may try a dd(Input::get('Auth_Token'))
to check what you have recieved from the POST
submitted by the user.
Upvotes: 4