Reputation: 394
I'm quite new to laravel, created all that is needed but i can't edit or add new entries into the database. However i can successfully delete. what could be the problem not making the application write to the database?
This is my store function
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');
}
Route
<?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');
Upvotes: 0
Views: 82
Reputation: 21691
Change your store
method with below given and let me know which message will print?
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();
//Success redirect
if($user){
Session::flash('message', 'Successfully added the record!');
Session::flash('alert-type', 'success');
}
//Failed redirect
else {
Session::flash('message', 'Something went wrong!');
Session::flash('alert-type', 'success');
}
return Redirect::to('user');
}
For get the ride of basic CRUD with laravel example.
Upvotes: 1
Reputation: 2736
User Controller
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');
}
Your routes
<?php
Route::get('/', function () {
return view('auth.login');
});
Route::auth();
Route::get('/home', 'HomeController@index');
Route::post('/user/store', 'UserController@store');
Your form
<form id="form-add-user" role="form" method="POST" action="{{ url('/user/store') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
...
</form>
Upvotes: 1