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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:25:20  来源:igfitidea点击:

Illuminate \ Database \ Eloquent \ ModelNotFoundException - No query results for model In laravel

phplaravellaravel-4

提问by TuGordoBello

I have the following data model

我有以下数据模型

enter image description here

在此处输入图片说明

I am trying to get a have a Serviciogiven a Auth_Tokenof a Tecnicoand idof a Servicio, but I get the following error appears

我试图获得一个Servicio给定Auth_Token的 aTecnicoida Servicio,但出现以下错误

enter image description here

在此处输入图片说明

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, firstOrFailthrows Illuminate\Database\Eloquent\ModelNotFoundExceptionexception if it doesn't find the requested model and hence it could be because the model with that Auth_Tokenfield is not available.

在这种情况下,如果找不到请求的模型,则firstOrFail抛出Illuminate\Database\Eloquent\ModelNotFoundException异常,因此可能是因为具有该Auth_Token字段的模型不可用。

Make sure the Auth_Tokenis right and that Auth_Tokenis available in your database. You may try a dd(Input::get('Auth_Token'))to check what you have recieved from the POSTsubmitted by the user.

确保Auth_Token正确并且Auth_Token在您的数据库中可用。您可以尝试dd(Input::get('Auth_Token'))检查从POST用户提交的内容中收到的内容。