无法将信息保存到数据库

时间:2016-12-05 10:37:25

标签: laravel

我对laravel很新,创建了所需的全部但我无法编辑或添加新条目到数据库中。但是我可以成功删除。可能是什么问题没有让应用程序写入数据库?

这是我的商店功能

public function store(Request $request)
{
  //validate data
  $validation=$this->validate($request, [
    'username' => 'required|max:50',
    'role_id' => 'required|max:50',
    'email' => 'required|email|max:50',
  ]);

  $user = new User;
  $user->username = $request->username;
  $user->role_id = $request->role_id;
  $user->email = $request->email;
  $user->save();

  // redirect
  Session::flash('message', 'Successfully added the record!');
  Session::flash('alert-type', 'success');

  return Redirect::to('user');
}

路线

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
    return view('auth.login');
});

Route::auth();

Route::get('/home', 'HomeController@index');

Route::resource('user', 'UserController');

Route::resource('role', 'RoleController');

1 个答案:

答案 0 :(得分:1)

用户控制器

public function store(Request $request)
{
  //validate data
  $validation=$this->validate($request, [
    'username' => 'required|max:50',
    'role_id' => 'required|max:50',
    'email' => 'required|email|max:50',
  ]);

  $user = new User;
  $user->username = $request->username;
  $user->role_id = $request->role_id;
  $user->email = $request->email;
  $user->save();

  // redirect
  Session::flash('message', 'Successfully added the record!');
  Session::flash('alert-type', 'success');

  return Redirect::to('user');
}

您的路线

<?php
Route::get('/', function () {
    return view('auth.login');
});

Route::auth();

Route::get('/home', 'HomeController@index');

Route::post('/user/store', 'UserController@store');

您的表格

<form id="form-add-user" role="form" method="POST" action="{{ url('/user/store') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}"> 
...
</form>