如何在Laravel中从数据库中选择/获取值

时间:2018-03-11 14:25:19

标签: php database laravel

我正在创建一个网站,我有一个注册表单。因此,当有人要使用已存在于数据库中的用户名和电子邮件时,我想向他们显示一条消息“用户名和电子邮件已经被拍摄”,就像这样。如果用户名和电子邮件是唯一的,用户可以注册。但是,当我尝试注册某人时,它总是显示“已经使用了用户名和电子邮件”。即使我使用唯一数据,也无法注册。

我该如何解决这个问题?

AdminPanel.blade.php

@if(session()->has('OnlyImg'))
<h4 class="alert alert-success"> {{ session()->get('OnlyImg') }} </h4>
@endif  

    <form class="form-horizontal" method="POST" action="{{ route('adinsert') }}" enctype="multipart/form-data">

    {{ csrf_field() }}

  <div class="form-group">
    <label>Username : *</label>
    <input type="text" class="form-control" name="username" value="{{ old('username') }}" placeholder="Enter Your Username" required>
  </div>

    <div class="form-group">
    <label>Email : *</label>
    <input type="email" class="form-control" name="email" value="{{ old('email') }}" placeholder="Enter Your Username" required>
  </div>

  <div class="form-group">
    <label>Password : *</label>
    <input type="password" class="form-control" name="password" value="{{ old('password') }}" placeholder="Enter Your Password" required>
  </div>

  <div class="form-group">
    <label>Upload Profile Picture :</label>
    <input type="file" class="form-control-file" name="file_img" aria-describedby="fileHelp">
    <small id="fileHelp" class="form-text text-muted">If U Want , U Can Skip Upload A Profile Picture</small>
  </div>

  @section('btnName',"Insert") 
  <input type="submit" class="btn btn-primary" onclick="myFunction()" name="submit" value="@yield('btnName')">
                    </form>

AdminPanelController.php

public function adinsert(Request $request)
    {

        $username = $request->input('username');
        $email = $request->input('email');
        $password = bcrypt($request->input('password'));
        //$passen = bcrypt($password);

        $user = new User();
        $user->username = $username;
        $user->email = $email;
        $user->password = $password;

        $this->validate($request, [
            'email' => 'required'
        ]);

        $res = DB::table('users')->where(['username' => $username, 'email' => $email])->get();

        if ($res > 0 ) {
            $request->session()->flash('Msg', 'Email already taken , Use another Email !!');
            return redirect('AdminPanel');
        }
        else
        {

        if(Input::hasFile('file_img')){

            $file = Input::file('file_img');

            $rules = array(
                'file_img' => 'required|max:10000|mimes:jpeg,png,jpg'
            );

            $validator = Validator::make(Input::all(), $rules);

            if  ($validator->fails()) {

                $request->session()->flash('OnlyImg', 'You Can Only Upload Images !!');
                return redirect('AdminPanel');

            }

            else if ($validator->passes()) {

                $fileimg = $file->getClientOriginalName();
                $destinationPath = 'img/Admins/';
                $filemove = $file->move($destinationPath, $fileimg);

                $user->fileimg = $fileimg;
                $user->filemove = $filemove;

                $user->save();

                $request->session()->flash('Msg', 'Successfully Inserted !!');

                return redirect('AdminPanel');

            }
        }

        else
        {

            $user->save();

            $request->session()->flash('Msg', 'Successfully Inserted !!');

            return redirect('AdminPanel');
        }

        }   

    } 

这是Route。

Route::post('adinsert',[
'uses'=> 'AdminPanelController@adinsert',
'as' => 'adinsert'
]);

0 个答案:

没有答案