duande3134 2014-11-29 12:37
浏览 17

必须在注册时输入值作为插入,并使用laravel 4.2中的login插入值进行匹配

Hi guys able to simple validate the login details and signup details but unable to insert and fetch database of phpmyadmin using laravel 4.2. Can you help with this?

Following is my code for models

class Website extends Eloquent implements UserInterface, RemindableInterface 
{ 
use UserTrait,RemindableTrait;
protected $table = 'websites'; 
protected $hidden = array('password', 'remember_token');
protected $protected = array();
protected $fillable = array('name',email','username','password','confirm_password','phone');
}

here is code for view file

here is view file

signup.blade.php

                                                {{ Form::open(array('url' => 'register')) }}    
                                                            @if($errors->has('name'))
                                                                    {{ $errors->first('name') }}
                                                                @endif                      
                                                            {{ Form::label('name', 'Name:') }}
                                                            {{ Form::text('name')  }}



                                                             @if($errors->has('username'))
                                                                    {{ $errors->first('username') }}
                                                                @endif
                                                            {{ Form::label('username', 'Username:') }}
                                                            {{ Form::text('username') }}

                                                            @if($errors->has('password'))
                                                                    {{ $errors->first('password') }}
                                                                @endif
                                                            {{ Form::label('password', 'Password:') }}
                                                                {{ Form::password('password') }}

                                                            @if($errors->has('password'))
                                                                    {{ $errors->first('confirm_password') }}
                                                                                @endif
                                                            {{ Form::label('password', 'Confirm Password:') }}
                                                            {{ Form::password('confirm_password') }}

                                                            @if($errors->has('email'))
                                                                                {{ $errors->first('email') }}
                                                                                                @endif
                                                            {{ Form::label('email', 'Email:') }}
                                                                {{ Form::text('email')  }}


                                                            @if($errors->has('phone'))
                                                                                    {{ $errors->first('phone') }}
                                                                                    @endif
                                                            {{ Form::label('phone', 'Phone:') }}
                                                            {{ Form::text('phone') }}


                                                            </div>

                                                        {{ Form::submit('Create', array('class' => 'btn btn-success')) }}


                {{ Form::close() }}

here is login.blade.php

{{ Form::open(array('url' => 'login')) }}
  <table>
     <tr>

        <td width="150px" class="table-responsive">
            {{ Form::label('username', 'Username:') }}
            {{ Form::text('username') }}
             @if($errors->has('username'))
                          <label> {{  $errors->first('username') }} </label>
             @endif
       </td>
        </tr>   
        <tr>
          <td width="150px">
            {{ Form::label('password', 'Password:') }}
            {{ Form::text('password') }}
            @if($errors->has('password'))
                          <label> {{  $errors->first('password') }} </label>
             @endif
        </td>
        </tr>
         <tr>
          <td width="150px">
        <div class="field">
            <input type="checkbox" name="remember"  id="remember"> 
            <label for="remember"> 
                Remember me
                </td>
        </tr>
        </div>
        <tr>
        <td>
            {{ Form::submit('Login', array('class' => 'btn btn-primary')) }}
       </td>
</tr>
</table>    
@if( $errors->count() > 0 )
    <p>The following errors have occurred:</p>

    <ul id="form-errors">
        {{ $errors->first('username', '<li>:message</li>') }}
        {{ $errors->first('password', '<li>:message</li>') }}
        {{ $errors->first('password_confirmation', '<li>:message</li>') }}
    </ul>   
@endif{{ Form::close() }}

here is my code for controller for function

WebsiteController.php

<?php

    class WebsiteController extends BaseController {

            public function index()
                        {
                            return View::make('websites.index',compact('websites'));
                        }


            public function login()
                        {

                             return View::make('websites.login');

                        }
            public function signup()
                        {

                                return View::make('websites.signup');

                        }


            public function show()
                        {
                                                 $input=Input::all();
                                                 $messages = array('username.required' => 'Please enter your username','password.required' => 'You have to set a password');
                                                 $rules = array(  'username'  => 'required|alpha-num','password'  => 'required');
                                                 $validator = Validator::make($input,$rules,$messages);

                                    if ($validator->fails()) {
                                                                      print_r($input);
                                                                      echo 1;
                                                                      return Redirect::to('login')->withErrors($validator);

                                                             }


                                                        else
                                                             {
                                                                  return Redirect::to('websites')
                                                                  ->with('message', 'Your account has been created, please login');                                                                   print_r($input);      
                                                             }



                     }
            public function store()
            {

                        $input=Input::all();

                         $messages = array(
                                                            'name.required' => 'Please enter your name',
                                                            'email.required' => 'your email address required',
                                                            'username.required' => 'Please enter your username',
                                                            'password.required' => 'You have to set a password',
                                                            'confirm_password.required' => 'Write again your password',
                                                            'confirm_password.matchpass' => 'The two passwords does not match');

                                  $rules = array(
                                                            'name'=>'required|alpha',                     
                                                            'username'  => 'required|alpha-num',
                                                            'password'  => 'required',
                                                            'confirm_password'=>'required',
                                                            'email'     => 'required|email',
                                                            'phone'=>'required|numeric'
        );


                         $validator = Validator::make($input,$rules,$messages);

                                    if ($validator->fails()) {
                                                                  print_r($input);

                                                                echo 1;
                                                                 return Redirect::to('register')->withErrors($validator)->withInput();

                                    }
                                                        else
                                                             {
                                                                        $user = new Website();
                                                                        $user->username =$input['username'];
                                                                        $user->email = $input['email'];
                                                                        $user->password = $input['password'];
                                                                        $user->name = $input['name'];
                                                                        $user->phone =$input['phone'];

                                                                        return Response::json(array('success' => true), 200);
                                                                        return Redirect::to('login')
                   ->with('global', 'Your account has been created! We have sent an email to activate your account');
//                                                                      }

                                                                                                                                    }


        }

        }
?>
  • 写回答

1条回答 默认 最新

  • dongshenghe1833 2014-12-01 08:04
    关注

    You just forgot to save the data in the store method.

    Add:

    $user->save();
    

    To:

    $user = new Website();
    $user->username =$input['username'];
    $user->email = $input['email'];
    $user->password = $input['password'];
    $user->name = $input['name'];
    $user->phone =$input['phone'];
    

    Final result:

    $user = new Website();
    $user->username =$input['username'];
    $user->email = $input['email'];
    $user->password = $input['password'];
    $user->name = $input['name'];
    $user->phone =$input['phone'];
    $user->save();
    

    To get all the result in your index method you need to add this code before the return:

    $websites = Website::all(); 
    

    Final result:

    public function index()
    {
        $websites = Website::all();
        return View::make('websites.index',compact('websites'));
    }
    

    Plus you have an error in your model, at this line you're missing an apostrophe, this is correct:

    protected $fillable = array('name','email','username','password','confirm_password','phone');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测