简体   繁体   中英

Laravel5.4 - How to use POST method insert data to database

I try to insert data to data.

  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'
    ];

}

Controller

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

When I insert, it response null.

{ "DATA": null }

I don't know, why it response null. please help me

How about this

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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