dpd7195 2017-10-24 03:43
浏览 90
已采纳

Laravel 5.5 - 无法从sql server 2008R2设置/获取数据(始终返回500)

I have problem with my laravel and sqlserver. Recently no problem with php pod because Ive use this sqlserver on my yii2 project and it work well.

try {
    DB::connection()->getPdo();
    if(DB::connection()->getDatabaseName()){
        echo "Yes! Successfully connected to the DB: " . DB::connection()->getDatabaseName();
    }
} catch (\Exception $e) {
    die("Could not connect to the database.  Please check your configuration.");
}

I also have tried db connection and it returned "Yes! Successfully connected to the DB: MyDB".

This is my controller

public function postsignup(Request $request){
        $this->validate($request, [
            'username' =>'required',
            'email' =>'required',
            'password' =>'required',
        ]);
        try{
            $users = new User;
            $users->id = '15';
            $users->username = $request->input('username');
            $users->password_hash = bcrypt($request->input('password'));
            $users->email = $request->input('email');
            $users->save();
            return redirect('/')->with('response','register success');
        }
        catch(\PDOException $e){
            echo $e->getMessage();
        }
    }

my model

 public $timestamps = false;
    protected $fillable = [
        'username', 'email', 'password_hash',
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];

this is my view

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row">

            @if(session('response'))
                <div class="col-md-8 alert alert-success">
                    {{session('success')}}
                </div>
            @endif


            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <div class="panel-heading">Register</div>

                    <div class="panel-body">
                        <form class="form-horizontal" method="post" action="{{route('account-create-post')}}">
                            {{ csrf_field() }}

                            <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                                <label for="name" class="col-md-4 control-label">Name</label>

                                <div class="col-md-6">
                                    <input id="name" type="text" class="form-control" name="username"
                                           value="{{ old('name') }}" required autofocus>

                                    @if ($errors->has('name'))
                                        <span class="help-block">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                                <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                                <div class="col-md-6">
                                    <input id="email" type="email" class="form-control" name="email"
                                           value="{{ old('email') }}" required>

                                    @if ($errors->has('email'))
                                        <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                                <label for="password" class="col-md-4 control-label">Password</label>

                                <div class="col-md-6">
                                    <input id="password" type="password" class="form-control" name="password" required>

                                    @if ($errors->has('password'))
                                        <span class="help-block">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>

                                <div class="col-md-6">
                                    <input id="password-confirm" type="password" class="form-control"
                                           name="password_confirmation" required>
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-6 col-md-offset-4">
                                    <button type="submit" class="btn btn-primary">
                                        Register
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

myroutes

Route::post('/signup', array(
    'as' => 'account-create-post',
    'uses' => 'UserController@postsignup'
));

Route::get('/signup', array(
    'as' => 'account-create',
    'uses' => 'UserController@getsignup'
));

This 500 error always appear when I try to access the database.

This code are used to my signup. First, user fill the signup input field then hit register button to save their data to database but I know something wrong with my connection with database because this 500 error not appear if I comment $users->save() . I also have tried to retrive data from database with $users = User::all(); and this 500 error appear. I hope some one ever faced this error and help me to solve this error so I can continue my work. Fyi I already do debug everywhere so I guess my codes are ok.

  • 写回答

1条回答 默认 最新

  • dongmaonao0505 2017-10-25 02:37
    关注

    After 3 days finally I know why this 500 comes out. I recently tried to check db connection with error handling and it returned success massage, but when I tried to migrate schema from database, my cmd stop to respond, I realize something wrong with my db conf in config/database.php

    'sqlsrv' => [
        'driver' => 'sqlsrv',
        'host' => 'my_IP',
        'port' => 'my_Port',
        'database' => 'DbName',
        'username' => 'Db_Username',
        'password' => 'Db_Password',
        'charset' => 'utf8',
        'prefix' => 'pos.',
    ],
    

    the problem is very simple -_-, prefix contain "." . I remove it and everything work perfectly. huh awkward moment, thanks @madalinivascu for your fast respond +1 for you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘