Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SQLSTATE[HY000]:一般错误:1096未使用表_Php_Mysql_Laravel 5 - Fatal编程技术网

Php SQLSTATE[HY000]:一般错误:1096未使用表

Php SQLSTATE[HY000]:一般错误:1096未使用表,php,mysql,laravel-5,Php,Mysql,Laravel 5,错误是这样的: SQLSTATE[HY000]: General error: 1096 No tables used (SQL: select `Marcas`.`nombre` as `Marca`, `Modelo`.`nombre` as `Modelo`, `Vehiculos`.`year` as `Anio`, `Vehiculos`.`id` from `Vehiculos` inner join `Reservaciones` on `Vehiculos`.`id` = `Re

错误是这样的:

SQLSTATE[HY000]: General error: 1096 No tables used (SQL: select `Marcas`.`nombre` as `Marca`, `Modelo`.`nombre` as `Modelo`, `Vehiculos`.`year` as `Anio`, `Vehiculos`.`id` from `Vehiculos` inner join `Reservaciones` on `Vehiculos`.`id` = `Reservaciones`.`id_vehiculo` inner join `Marcas` on `Vehiculos`.`id_marca` = `Marcas`.`id` inner join `Modelo` on `Vehiculos`.`id_modelo` = `Modelo`.`id` where `Vehiculos`.`id_marca` = 1 and `Marcas`.`id_categoria` = 2 and `Vehiculos`.`id` not in (select *))
这是我在laravel提出的问题:

public function checkDisponibilityByDate(Request $req){
    $fecha = $req->fecha;

    $vehiculos= Vehiculo::join('Reservaciones', 'Vehiculos.id', '=', 'Reservaciones.id_vehiculo')
    ->join('Marcas', 'Vehiculos.id_marca', '=' , 'Marcas.id')
    ->join('Modelo', 'Vehiculos.id_modelo', '=' , 'Modelo.id')
    ->select('Marcas.nombre as Marca', 'Modelo.nombre as Modelo', 'Vehiculos.year as Anio', 'Vehiculos.id')
    ->where('Vehiculos.id_marca', '=' ,$req->id_marca)
->where('Marcas.id_categoria', '=' ,$req->id_categoria)
->whereNotIn('Vehiculos.id', function($fecha) {
        Reservacion::select('Reservaciones.id_vehiculo')
        ->from('Reservaciones')
          ->where('Reservaciones.fecha', '=', $fecha);
})
->get();
return $vehiculos->toJson();
}

我希望您能在这方面帮助我,我正在使用Laravel 5

我认为如果您想向查询传递参数,您需要使用use$fecha传递它

也可以通过单独构造数组来简化whereNotIn

public function checkDisponibilityByDate(Request $req)
{
    $fecha = $req->fecha;
    $check = Reservacion::where('fecha', $fecha)->lists('id_vehiculo');

    $vehiculos= Vehiculo::join('Reservaciones', 'Vehiculos.id', '=', 'Reservaciones.id_vehiculo')
                ->join('Marcas', 'Vehiculos.id_marca', '=' , 'Marcas.id')
                ->join('Modelo', 'Vehiculos.id_modelo', '=' , 'Modelo.id')
                ->select('Marcas.nombre as Marca', 'Modelo.nombre as Modelo', 'Vehiculos.year as Anio', 'Vehiculos.id')
                ->where('Vehiculos.id_marca', '=' ,$req->id_marca)
                ->where('Marcas.id_categoria', '=' ,$req->id_categoria)
                ->whereNotIn('Vehiculos.id', $check)
            })
            ->get();
    return $vehiculos->toJson();
}    
希望这有帮助


如果您可以共享您的模型和表结构,那么可能可以建议其他/纯雄辩的方法。

请参见查询末尾的“选择”中的“…和Vehiculos.id”。看到问题了吗?如果让我猜的话,那是因为您没有从whereNotIn回调返回任何内容。但我不擅长雄辩,所以可能它有点神奇。尝试更改它以返回Reservation::selectIt在@BarmarTry中不起作用,而在'Vehiculos.id',函数$query use$fecha中{$query->select'id_vehiculo'->from'Reservaciones'->where'Reservaciones.fecha','=',$fecha;。您使用的是哪一个Laravel版本的L5.0或更高版本?您将雄辩的查询生成器与查询生成器混为一谈,这似乎不是最佳实践。如果没有表结构和模型关系代码,就不可能提供替代方法。
public function checkDisponibilityByDate(Request $req)
{
    $fecha = $req->fecha;
    $check = Reservacion::where('fecha', $fecha)->lists('id_vehiculo');

    $vehiculos= Vehiculo::join('Reservaciones', 'Vehiculos.id', '=', 'Reservaciones.id_vehiculo')
                ->join('Marcas', 'Vehiculos.id_marca', '=' , 'Marcas.id')
                ->join('Modelo', 'Vehiculos.id_modelo', '=' , 'Modelo.id')
                ->select('Marcas.nombre as Marca', 'Modelo.nombre as Modelo', 'Vehiculos.year as Anio', 'Vehiculos.id')
                ->where('Vehiculos.id_marca', '=' ,$req->id_marca)
                ->where('Marcas.id_categoria', '=' ,$req->id_categoria)
                ->whereNotIn('Vehiculos.id', $check)
            })
            ->get();
    return $vehiculos->toJson();
}