简体   繁体   English

如何创建Laravel雄辩的实体的空实例

[英]How to create empty instance of Laravel Eloquent entity

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. 我希望Laravel控制器具有特殊的方法,该方法返回Contracts实体的空实例作为json对象。 The idea is that Laravels's code make all the necessary default assignments to this entity (eg 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. 这个想法是Laravels的代码对该实体进行所有必要的默认分配(例如,生成器的ID,根据用户偏好的默认价格类型,根据地区的默认货币等),并返回可从Javascript界面​​使用的实体。 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. 稍后,Javascript接口将决定是否放弃该实体以继续该实体,并最终使用适当的insert方法或Laravelt REST API保存(插入)该实体。

The problem is - how to create empty instance of Laravel model? 问题是- 如何创建Laravel模型的空实例? Eg 例如

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. 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... 一种解决方案是使用返回空值的原始SQL,然后可以希望Laravel可以将此SQL转换为对象。

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);
    } 
}

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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM