【问题标题】:How to store data in foreign key of a user table如何将数据存储在用户表的外键中
【发布时间】:2018-04-12 22:07:35
【问题描述】:

这个问题很简单。我将用户的表单数据存储到表中。我正在使用 laravel 的 Auth 登录系统,当用户填写输入字段并在创建函数中注册在名为 $data 的数组中收到的所有值(正如您知道默认用 laravel 编写的代码)但是当我将它保存在表中时,所有值保存期望 department_id 这是一个外键。还有一个candidate_id,也是candidate_table的外键,也保存了,但是department_id有问题。

刀片

@extends('layouts.app')
@section('content')
<div class="container">
    <div class="row">
        <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('register') }}">
                        {{ csrf_field() }}
                        {{--<input style="text-transform: capitalize;" id="test" type="number" class="form-control" name="id" required autofocus>--}}
                        <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 style="text-transform: capitalize;" id="name" type="text" class="form-control" name="name" 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>
                        <input style="text-transform: capitalize;" id="department" type="number" class="form-control" name="department" required autofocus>
                        <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

用户模型

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    public function candidate()
    {
        return $this->belongsTo(Candidate::class,'candidate_id');
    }

    public function department()
    {
        return $this->belongsTo(Department::class,'department_id');
    }

    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name','email','password','image','candidate_id'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

控制器

<?php

namespace App\Http\Controllers\Auth;

use App\Department;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
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 = '/home';

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

    /**
     * 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, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',

        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
//          'candidate_id' => $data['id'],
            'password' => bcrypt($data['password']),
             'department_id' => $data['department'],
        ]);
    }
}

【问题讨论】:

    标签: laravel


    【解决方案1】:

    如您所见,对于 department_id 输入,您已注释掉表单输入字段。

    {{--<input style="text-transform: capitalize;" id="test" type="number" class="form-control" name="id" required autofocus>--}}
    

    取消注释代码,它将正常工作。 并且因为它,它不会存储department_id

    【讨论】:

    • 这是部门字段,这不是隐藏
    • 实际上我在 auth 的注册控制器中添加了一些更改,但一段时间后我将其更改为默认值.. 这是一个原因吗?
    • 实际上它是另一个文件,现在不是代码的一部分。我的评论中带有名称变量部门的字段这是我在控制器中收到的主要内容,带有 $data 数组但不存储在表属性“department_id”和表中
    【解决方案2】:

    尝试改变...

    protected $fillable = [ 'name','email','password','image','candidate_id' ];

    protected $fillable = [ 'name','email','password','image','candidate_id','department_id' ];

    【讨论】:

    • 哦,伙计,你很漂亮……我完全忘记了……我经常忘记……但是非常感谢……上帝保佑你。
    • 接受答案祝福我 :),这让我很多次......很高兴我能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 2016-08-20
    • 1970-01-01
    • 2019-02-25
    • 2013-02-11
    相关资源
    最近更新 更多