如何创建 Laravel Eloquent 实体的空实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38425289/
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
How to create empty instance of Laravel Eloquent entity
提问by TomR
I have the Laravel PHP code:
我有 Laravel PHP 代码:
class Contract extends Model
{
public $table = "CONTRACTS";
public $timestamps = false;
protected $primaryKey = 'ID';
protected $fillable = array("ID", "CONTRACT_DATE", "PRICE_TYPE", "AMOUNT");
}
I would like Laravel controller to have special method that returns empty instance of Contracts entity as json object. The idea is that Laravels's code make all the necessary default assignments to this entity (e.g. ID from the generator, default price type according to user preferences, default currency according to region and so on) and returns entity that can be usable from Javascript interface. Javascript interface later will decided whether to discard this entity of whether to continue with this entity and to finally save (insert) it with the appropriate insert method or Laravelt REST API.
我希望 Laravel 控制器具有特殊的方法,可以将 Contracts 实体的空实例作为 json 对象返回。这个想法是 Laravel 的代码对这个实体进行所有必要的默认分配(例如来自生成器的 ID、根据用户偏好的默认价格类型、根据地区的默认货币等)并返回可以从 Javascript 界面使用的实体。Javascript 接口稍后会决定是否丢弃该实体,是否继续使用该实体并最终使用适当的插入方法或 Laravelt REST API 保存(插入)它。
The problem is - how to create empty instance of Laravel model?E.g.
问题是 -如何创建 Laravel 模型的空实例?例如
class Contracts extends Controller
public function create() {
$contract = new Contract;
return json_encode($contract);
}
}
code simply returns
代码只是返回
[]
But I would like to have something like this:
但我想要这样的东西:
{"ID":1111,
"CONTRACT_DATE":null,
"PRICE_TYPE":"2",
"AMOUNT":0.0}
Is that possible with Laravel. One solution could be use of raw SQL that returns empty values and then one can hope that Laravel can translate this SQL into object...
Laravel 可以实现吗?一种解决方案可能是使用返回空值的原始 SQL,然后希望 Laravel 可以将此 SQL 转换为对象......
回答by theMohammedA
If you want to set custom values to your instance,
如果您想为您的实例设置自定义值,
Go to your model and add this
转到您的模型并添加此
//these are the name of the column of contract table that you wanna fill in
//if ID is an auto increment column remove it!
protected $fillable = [
'ID','CONTRACT_DATE','PRICE_TYPE','AMOUNT'];
the change this code:
更改此代码:
class Contracts extends Controller
public function create() {
$contract = new Contract;
return json_encode($contract);
}
}
to this:
对此:
class Contracts extends Controller
public function create() {
$contract = new Contract;
$contract->ID = 1111;
$contract->CONTRACT_DATE = null;
$contract->PRICE_TYPE = '2';
$contract->AMOUNT = 0.0;
//$contract->save();//this line will insert this instance into db
return response()->json($contract);
}
}
If your values are fixed, you can do this in your contract model class
如果您的值是固定的,您可以在您的合同模型类中执行此操作
//set default attributes
protected $attributes = array(
'ID' => 1111,
'CONTRACT_DATE' => null,
'PRICE_TYPE' => '2',
'AMOUNT' => 0.0
);
then you can use your code to return an instance filled with the default values above
然后您可以使用您的代码返回一个填充了上述默认值的实例
class Contracts extends Controller
public function create() {
$contract = new Contract;
/*return json_encode($contract);*/
//why use laravel response? because it sets other header parameters for you :)
return response()->json($contract);
}
}
回答by Adam Moore
I needed to do the same for a weird little hack. I used the below to create an empty model instance using the $fillable
attributes.
我需要为一个奇怪的小黑客做同样的事情。我使用下面的$fillable
属性来创建一个空的模型实例。
$quote_request = new QuoteRequest();
$data = array_combine(
$quote_request->getFillable(),
array_fill(0, count($quote_request->getFillable()), null)
);
$quote_request->fill($data);
dd($quote_request->toArray());