亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

\ Eloquent \ Model :: Create方法返回null,狀態碼為OK

\ Eloquent \ Model :: Create方法返回null,狀態碼為OK

PHP
UYOU 2021-05-10 16:09:20
創建方法無法正常工作。始終返回OK狀態,數據為空,并且在db中沒有插入。沒有錯誤顯示不幸,所以我不知道該怎么辦。protected function addBooking(Request $request){    $data = $request->all();    if ($this->validator($data)->fails()) {        return $this->sendError('Validation Error.', $this->validator($data)->errors());    }    Booking::create($data);    return $data;}這是遷移 public function up(){    Schema::create('bookings', function (Blueprint $table) {        $table->bigIncrements('id');        $table->bigInteger('booker_id')->nullable(false)->unsigned();        $table->bigInteger('classroom_id')->nullable(false)->unsigned();        $table->string('name');        $table->string('color')->default("#ff0000");        $table->string('file')->default(NULL);        $table->string('start')->nullable(false);        $table->string('end')->nullable(false);        $table->timestamps();        $table->foreign('booker_id')->references('id')->on('users');        $table->foreign('classroom_id')->references('id')->on('classrooms');    });}該模型  class Booking extends Model    {    protected $fillable = [        'booker_id', 'classroom_id', 'name', 'color', 'file', 'start', 'end'    ];    protected $hidden = [    ];    protected $casts = [    ];}我如何發送請求{  "booker_id": 10,  "classroom_id": 4,  "name": "Microsoft",  "start": "2019-04-25 14:45",  "end": "2019-04-25 16:45",  "color": "#ff0000",  "file": "test"}
查看完整描述

2 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

Laravel有一個路由約定,如果要創建新項目,則方法名稱應為store()。我使用兩種方式創建新元素:首先,這是較短的方法,并且我在StoreBooking中添加了驗證


public function store( StoreBooking $request ) {

  $data = $request->all();

  $booking = Booking::query()->create( $data );

}

基于laravel文檔的記錄器:


public function store( StoreBooking $request ) {

 $booking = new Booking();

 $booking->bookier_id = $request->bookier_id;

 /**

  add other items

 **/

 $booking->save();

}


查看完整回答
反對 回復 2021-05-14
?
慕田峪9158850

TA貢獻1794條經驗 獲得超8個贊

//您應該使用表單請求來驗證數據。并將所有業務邏輯移至模型


protected function addBooking(Request $request)

{


    $data = $request->all();



    if ($this->validator($data)->fails()) {

        return $this->sendError('Validation Error.', $this->validator($data)->errors());

    }


    return Booking::create($data)->fresh();


}


查看完整回答
反對 回復 2021-05-14
  • 2 回答
  • 0 關注
  • 158 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號