laravel 简单的查询生成器(连接和 where case)
laravel simple query builder (join & where case)
我有2个table(product & type)
produk table
-id
-kode_produk
-nama_produk
-id_jenis_produk
和
jenis table
- id
- jenis_item
我想使用查询生成器从 jenis table
访问数据库 jenis_item
到目前为止我已经尝试了
$selectProduk = DB::table('produk')->where('id', $id)->join('jenis', 'produk.id_jenis_produk', '=', 'jenis.id')->first();
还有类似的东西
$selectProduk = DB::table('produk')
->join('jenis', function($join) {
$join->on('produk.id_jenis_item', '=', 'jenis.id')
->where('produk.id', $id); // line 86 (error)
})->first();
但仍然失败,消息来自 laravel 日志
exception 'ErrorException' with message 'Undefined variable: id' in C:\xampp\htdocs\itklan\app\controllers\ProdukController.php:86
我哪里不见了?
@Thomas Kim
我收到另一个错误
exception 'ErrorException' with message 'Missing argument 3 for Illuminate\Database\Query\JoinClause::where(), called in C:\xampp\htdocs\itklan\app\controllers\ProdukController.php on line 86 and defined' in C:\xampp\htdocs\itklan\vendor\laravel\framework\src\Illuminate\Database\Query\JoinClause.php:87
第 87 行:
$selectProduk = DB::table('produk')
->join('jenis', function($join) use($id) {
$join->on('produk.id_jenis_item', '=', 'jenis.id')
->where('produk.id', $id);
})->first(); //line 87
这就是 PHP 闭包的工作方式。为了使用 $id
,闭包必须使用 use
关键字从父作用域继承变量。例如:
$selectProduk = DB::table('produk')
->join('jenis', function($join) use($id) { // Check this line
$join->on('produk.id_jenis_item', '=', 'jenis.id')
->where('produk.id', '=', $id);
})->first();
Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct.
来源:http://php.net/manual/en/functions.anonymous.php
编辑:
此外,对于 Laravel 的 JoinClause
,您需要具体说明您的运算符。通常,您可以这样做:
->where('produk.id', $id);
并且Laravel为您添加了一个等于运算符。但是,对于连接子句,这将不起作用。您需要指定运算符。
->where('produk.id', '=', $id);
我有2个table(product & type)
produk table
-id
-kode_produk
-nama_produk
-id_jenis_produk
和
jenis table
- id
- jenis_item
我想使用查询生成器从 jenis table
访问数据库 jenis_item
到目前为止我已经尝试了
$selectProduk = DB::table('produk')->where('id', $id)->join('jenis', 'produk.id_jenis_produk', '=', 'jenis.id')->first();
还有类似的东西
$selectProduk = DB::table('produk')
->join('jenis', function($join) {
$join->on('produk.id_jenis_item', '=', 'jenis.id')
->where('produk.id', $id); // line 86 (error)
})->first();
但仍然失败,消息来自 laravel 日志
exception 'ErrorException' with message 'Undefined variable: id' in C:\xampp\htdocs\itklan\app\controllers\ProdukController.php:86
我哪里不见了?
@Thomas Kim
我收到另一个错误
exception 'ErrorException' with message 'Missing argument 3 for Illuminate\Database\Query\JoinClause::where(), called in C:\xampp\htdocs\itklan\app\controllers\ProdukController.php on line 86 and defined' in C:\xampp\htdocs\itklan\vendor\laravel\framework\src\Illuminate\Database\Query\JoinClause.php:87
第 87 行:
$selectProduk = DB::table('produk')
->join('jenis', function($join) use($id) {
$join->on('produk.id_jenis_item', '=', 'jenis.id')
->where('produk.id', $id);
})->first(); //line 87
这就是 PHP 闭包的工作方式。为了使用 $id
,闭包必须使用 use
关键字从父作用域继承变量。例如:
$selectProduk = DB::table('produk')
->join('jenis', function($join) use($id) { // Check this line
$join->on('produk.id_jenis_item', '=', 'jenis.id')
->where('produk.id', '=', $id);
})->first();
Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct.
来源:http://php.net/manual/en/functions.anonymous.php
编辑:
此外,对于 Laravel 的 JoinClause
,您需要具体说明您的运算符。通常,您可以这样做:
->where('produk.id', $id);
并且Laravel为您添加了一个等于运算符。但是,对于连接子句,这将不起作用。您需要指定运算符。
->where('produk.id', '=', $id);