Laravel5.4 - 如何使用POST方法将数据插入数据库

时间:2017-08-19 13:34:54

标签: php laravel-5.4

我尝试将数据插入数据。

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

当我插入时,它响应为null。

  

{       “DATA”:null}

我不知道,为什么它响应为null。 请帮帮我

1 个答案:

答案 0 :(得分:1)

这个怎么样

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

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