Laravel将数据输入到多个表并进行验证

时间:2017-05-02 00:15:59

标签: php mysql laravel eloquent

所以问题是,我正在尝试使用Laravel自己的脚手架版本进行自定义注册。我的输入上方有2个单选按钮,用户可以选择他的帐户类型。 (User :: Company)首先,我为用户和公司制作了数据透视表,并使用了雄辩的关系。

用户模型:

protected $fillable = [
    'first_name', 'last_name', 'email', 'phone',
    'password', 'verified', 'email_token',
];

protected $hidden = [
    'password', 'remember_token',
];

 public function company(){
    $this->belongsToMany('Company', 'user_company');
}

公司型号:

protected $table = 'companies';

protected $fillable = [
    'company', 'reg_nr', 'address',
    'person', 'area_id', 'category_id',
];

public function users(){
    $this->belongsToMany('User', 'user_company');
}

注册视图

@extends('layouts.app')

@section('content')
    <br/><br/><br/><br/>
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Liitu</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="{{ route('register') }}">
                        {{ csrf_field() }}
                        <div class="col-md-offset-5">
                            <label class="radio-inline"><input type="radio" name="optradio" value="1" class="user_type"> Eraisik</label>
                            <label class="radio-inline"><input type="radio" name="optradio" value="2" class="user_type"> Juriidiline isik</label>
                        </div>
                        <br/>
                        <script>
                            $(document).ready(function() {
                                $('.user_type').change(function(){
                                    if( $(this).val() == 1 )
                                    {
                                        $("#company").hide();
                                        $("#reg_input").hide();
                                        $("#address_input").hide();
                                        $("#person_input").hide();
                                        $("#area_input").hide();
                                        $("#category_input").hide();
                                    }
                                    else
                                    {
                                        $("#company").show();
                                        $("#reg_input").show();
                                        $("#address_input").show();
                                        $("#person_input").show();
                                        $("#area_input").show();
                                        $("#category_input").show();
                                    }
                                });
                            });
                        </script>
                        <div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
                            <label for="first_name" class="col-md-4 control-label">Eesnimi:</label>

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

                                @if ($errors->has('first_name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('first_name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('last_name') ? ' has-error' : '' }}">
                            <label for="last_name" class="col-md-4 control-label">Perekonnanimi:</label>

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

                                @if ($errors->has('last_name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('last_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</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('contact') ? ' has-error' : '' }}">
                            <label for="contact" class="col-md-4 control-label">Tel. nr:</label>

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

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

                        <div class="form-group{{ $errors->has('company') ? ' has-error' : '' }}" id="company" style="display: none">
                            <label for="company" class="col-md-4 control-label">Ettevõte:</label>

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

                                @if ($errors->has('company'))
                                    <span class="help-block">
                                    <strong>{{ $errors->first('company') }}</strong>
                                </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('category_id') ? ' has-error' : '' }}" id="category_input" style="display: none">
                            <label for="category" class="control-label">Tegevusvaldkond</label>
                            <select name="category_id" id="category" class="form-control{{isset($listing) && $listing->live() ? ' disabled = "disabled' : ''}}">
                                @foreach($categories as $category)
                                    <optgroup label="{{ $category->name }}">
                                        @foreach($category->children as $child)
                                            @if(isset($listing) && $listing->category_id == $child->id || old('category_id') == $child->id)
                                                <option name="category" id="category" value="{{ $child->id }}" selected="selected">{{ $child->name }}</option>
                                            @else
                                                <option name="category" id="category" value="{{ $child->id }}">{{ $child->name }}</option>
                                            @endif
                                        @endforeach
                                    </optgroup>
                                @endforeach
                            </select>
                            @if($errors->has('category_id'))
                                <span class="help-block">
                                {{$errors->first('category_id')}}
                            </span>
                            @endif
                        </div>
                        <div class="form-group{{ $errors->has('reg_nr') ? ' has-error' : '' }}" id="reg_input" style="display: none">
                            <label for="reg_nr" class="col-md-4 control-label">Reg. nr:</label>

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

                                @if ($errors->has('reg_nr'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('reg_nr') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('area_id') ? ' has-error' : '' }}" id="area_input" style="display: none">
                            <label for="area" class="control-label">Maakond</label>
                            <select name="area_id" id="area" class="form-control">
                            @foreach($areas  as $country)
                                @foreach($country->children  as $state)
                                    @foreach($state->children  as $city)
                                        @if(
                                            isset($listing) && $listing->area->id == $city->id ||
                                            !isset($listing) && $area->id == $city->id  && !old('area_id')||
                                            old('area_id') == $city->id)
                                            <option name="area" id="area" value="{{ $city->id }}" selected="selected">{{$city->name}}</option>
                                        @else
                                            <option name="area" id="area" value="{{ $city->id }}">{{$city->name}}</option>
                                        @endif
                                    @endforeach
                                @endforeach
                            @endforeach
                            </select>
                            @if($errors->has('area_id'))
                                <span class="help-block">
                                {{$errors->first('area_id')}}
                            </span>
                            @endif
                        </div>
                        <div class="form-group{{ $errors->has('address') ? ' has-error' : '' }}" id="address_input" style="display: none">
                            <label for="address" class="col-md-4 control-label">Aadress:</label>

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

                                @if ($errors->has('address'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('address') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('person') ? ' has-error' : '' }}" id="person_input" style="display: none">
                            <label for="name" class="col-md-4 control-label">Kontakt isik:</label>

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

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


                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label for="password" class="col-md-4 control-label">Parool</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">Kinnita parool</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">
                                    Liitu
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

RegisterController

    <?php

namespace App\Http\Controllers\Auth;

use App\Company;
use DB;
use Mail;
use App\User;
use App\Area;
Use App\Category;
use Validator;
use Illuminate\Http\Request;
use App\Mail\EmailVerification;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    public function showRegistrationForm(){

        $areas = Area::all();
        $categories = Category::all();
        return view('auth.register', compact('areas', 'categories'));
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {

        return Validator::make($data, [
//            'first_name' => 'required|max:255',
//            'last_name' => 'required|max:255',
//            'company' => 'required|max:255|unique:companies',
//            'reg_nr' => 'required|max:255',
//            'address' => 'required|max:255',
//            'email' => 'required|email|max:255|unique:users',
//            'person' => 'required|max:255',
//            'contact' => 'required',
//            'area' => 'required|max:255',
//            'category' => 'required|max:255',
//            'password' => 'required|min:6|confirmed',
        ]);

    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(Request $request,array $data)
    {
        $user = User::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'contact' => $data['contact'],
            'password' => bcrypt($data['password']),
            'email_token' => str_random(10),
        ]);

        $company = Company::create([
            'company' => $data['company'],
            'reg_nr' => $data['reg_nr'],
            'address' => $data['address'],
            'contact' => $data['contact'],
            'area_id' => $data['area'],
            'category_id' => $data['category'],
        ]);

        return $user;
    }
}

...现在我删除的原因

验证只是为了测试,我可以将数据提交到数据库,但现在它甚至没有向数据库发出POST请求。我也尝试使用条件,但看起来我对Laravel和PHP的知识仍然相当有限,所以我希望有人可以帮助我在这里。试图从谷歌搜索类似的问题,但没有发现任何可以帮助的事情。

无论如何,基本上我对注册的最终想法是,当客人去注册路线时。他需要在这些单选按钮(User :: Company)之间进行选择,并且可以说当客户选择了通常的用户类型时,他有能力将数据提交给用户表,并且只能看到

First_name输入 Last_name输入 电邮输入 联系输入 密码输入

当guest选择公司类型时,他可以看到所有输入,但是当用户创建公司时,它们存储在两个不同的数据库表中。

抱歉英语不好,这里很累啊.. 问候

0 个答案:

没有答案