dpi96151 2016-07-17 19:46
浏览 161
已采纳

如何创建Laravel Eloquent实体的空实例

I have the Laravel PHP code:

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.

The problem is - how to create empty instance of Laravel model? E.g.

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...

  • 写回答

2条回答 默认 最新

  • duanruoyu6675 2016-07-17 20:34
    关注

    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);
        } 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解