2017-08-19 97 views
0

我嘗試向數據插入數據。Laravel5.4 - 如何使用POST方法將數據插入數據庫

public function up() 
      { 
       Schema::create('request_song', function (Blueprint $table) { 
        $table->increments('id'); 
        $table->string('song_name', 150); 
        $table->string('singer_name', 50); 
        $table->string('general_input', 200); 

        $table->timestamps(); 
       }); 
      } 

RequestSongModel

class RequestSongModel extends Model 
{ 
    protected $table = 'request_song'; 
    protected $fillable = [ 
     'id', 
     'song_name', 
     'singer_name' 
    ]; 

} 

控制器

public function store(Request $request) 
    { 
     $rule = [ 
      'song_name' => 'required', 
      'singer_name' => 'required', 

     ]; 

     $this->validate($request, $rule); 
     $requestSong = RequestSongModel::created($request->all()); 

     return response()->json(['DATA' => $requestSong], 201); 
    } 

當我插入,它響應空。

{ 「DATA」:空}

我不知道,爲什麼它響應空。 請幫我

回答

1

這個

$requestSong = RequestSongModel::created($request->all());

$requestSong = RequestSongModel::create($request->all());

+0

喔,怎麼謝謝主席先生 – Vichit

+0

澄清:'created'是洋洋灑灑的ORM而引發的事件'創造'實際上創造了模型。 – milo526

相關問題