laravel Illuminate \ Database \ Eloquent \ ModelNotFoundException - 在laravel中没有模型的查询结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23299976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Illuminate \ Database \ Eloquent \ ModelNotFoundException - No query results for model In laravel
提问by TuGordoBello
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
我试图获得一个Servicio
给定Auth_Token
的 aTecnico
和id
a Servicio
,但出现以下错误
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?
我该如何解决?
回答by The Alpha
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.
在这种情况下,如果找不到请求的模型,则firstOrFail
抛出Illuminate\Database\Eloquent\ModelNotFoundException
异常,因此可能是因为具有该Auth_Token
字段的模型不可用。
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.
确保Auth_Token
正确并且Auth_Token
在您的数据库中可用。您可以尝试dd(Input::get('Auth_Token'))
检查从POST
用户提交的内容中收到的内容。