Laravel 5.5 - save();不会保存到数据库,也不会输出错误

时间:2018-01-11 06:03:25

标签: laravel

我正在尝试使用laravel 5.5和mysql创建一个注册表单,但问题是它不会提交到数据库..

PageController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PageController extends Controller
{
public function main(){
if (!Auth::Check()) {
return view('auth.login');
}
return view('user.main');
 }

public function register_check(Request $request){
$this->validate($request, [

'username' => 'required|min:2|max:12|unique:users',
    'fname' => 'required|min:5|max:20',
    'position' => 'required|not_in:0',
    'password' => 'required|min:6|max:12',
    'ConfirmPass' => 'required|same:password',
    'contactnum' => 'required|min:2|max:12'

]);

$user = new User;
        $user->username = $request['username'];
        $user->fname = $request['fname'];
        $user->position= $request['position'];
        $user->password = bcrypt($request['password']);
        $user->ConfirmPass = $request['ConfirmPass'];
        $user->contactnum = $request['contactnum'];
        $user->save();

    }
        }

路由web.php

Route::get('/', [
'as'=>'index',
'uses'=> 'PageController@main'
]);

Route::get('/register', [
'as'=>'register',
'uses'=> 'PageController@register'
]);

Route::post('/register_check', [
'as'=>'register_check',
'uses'=>'PageController@register_check'
]);

任何帮助将不胜感激。提前谢谢你

2 个答案:

答案 0 :(得分:0)

我猜验证时出错,您将被重定向回表格。您可能没有在表单视图中检查$errors,因此您没有看到错误是什么。

将此代码放在表单视图文件中。查看是否存在任何验证错误。

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

答案 1 :(得分:0)

处理此类情况的最佳方法是检查save()函数的响应,如:

if( $user->save() )
{
    // Success message here
}
else
{
    // Failure message here
} 

现在您将gt知道保存是否成功。