eloquent 主键值为空的模型
eloquent model with empty primary key value
我正在使用带有 Eloquent 作为 ORM 的 Slim 3。我有一个产品 table,它只包含 3 个字段,productCode、name 和 price。我在获取产品属性时遇到问题。
我已经为产品创建了一个模型class,如下:
class Product extends Model
{
protected $primaryKey = 'productCode';
public $timestamps = false;
protected $fillable = [
'name',
'price'
];
}
Esta la estoy utilizando desde mi controlador para enviarlos como Json, de la siguiente forma:
class ProductsController extends Controller
{
public function getAll($request, $response)
{
$products = Product::all();
return $response->withJson($products, 200);
}
}
问题是它returns以下内容:
[{"productCode":0,"name":"producto 1","price":15000},{"productCode":0,"name":"producto 2","price":20000},{"productCode":0,"name":"producto 3","price":5999}]
所有 productCode 变量都被 vacuum 改变了,所以 json returns 0.
当使用 print_r() 检查变量时,它确实包含正确的产品代码,但我无法获得这些值,它总是 returns 空。
Store\Models\Product Object
(
[primaryKey:protected] => productCode
[timestamps] =>
[fillable:protected] => Array
(
[0] => name
[1] => price
)
[connection:protected] => default
[table:protected] =>
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[productCode] => cod0001
[name] => producto 1
[price] => 15000
)
[original:protected] => Array
(
[productCode] => cod0001
[name] => producto 1
[price] => 15000
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
如您所见,如果变量包含正确的 productCode,问题是当我尝试获取它们时它们仍然为空。
您必须告诉 Eloquent 主键不是自动递增的:
class Product extends Model
{
public $incrementing = false;
}
我正在使用带有 Eloquent 作为 ORM 的 Slim 3。我有一个产品 table,它只包含 3 个字段,productCode、name 和 price。我在获取产品属性时遇到问题。
我已经为产品创建了一个模型class,如下:
class Product extends Model
{
protected $primaryKey = 'productCode';
public $timestamps = false;
protected $fillable = [
'name',
'price'
];
}
Esta la estoy utilizando desde mi controlador para enviarlos como Json, de la siguiente forma:
class ProductsController extends Controller
{
public function getAll($request, $response)
{
$products = Product::all();
return $response->withJson($products, 200);
}
}
问题是它returns以下内容:
[{"productCode":0,"name":"producto 1","price":15000},{"productCode":0,"name":"producto 2","price":20000},{"productCode":0,"name":"producto 3","price":5999}]
所有 productCode 变量都被 vacuum 改变了,所以 json returns 0.
当使用 print_r() 检查变量时,它确实包含正确的产品代码,但我无法获得这些值,它总是 returns 空。
Store\Models\Product Object
(
[primaryKey:protected] => productCode
[timestamps] =>
[fillable:protected] => Array
(
[0] => name
[1] => price
)
[connection:protected] => default
[table:protected] =>
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[productCode] => cod0001
[name] => producto 1
[price] => 15000
)
[original:protected] => Array
(
[productCode] => cod0001
[name] => producto 1
[price] => 15000
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
如您所见,如果变量包含正确的 productCode,问题是当我尝试获取它们时它们仍然为空。
您必须告诉 Eloquent 主键不是自动递增的:
class Product extends Model
{
public $incrementing = false;
}