如何使用单个表格在具有关联的两个表中添加值?

时间:2019-04-09 13:03:05

标签: php laravel laravel-5 eloquent

我是Laravel的新手,我想使用一种形式将数据添加到两个表中。我的表及其关系是

enter image description here

我想为用户添加所有字段,然后选择class_id,然后通过下拉列表选择section_id。

然后注册,我希望在数据注册后再注册。注册表应具有该新用户的user_id,并已注册并选择了该类别的用户ID和部分ID。

学生表

的字段
ax[0], ax[1], ax[2]

注册表

中的字段
<form method="POST" action="{{url('/storeStudent')}}">
@csrf

<div class="form-group row">
    <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

    <div class="col-md-6">
        <input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name') }}" required autofocus>

        @if ($errors->has('name'))
            <span class="invalid-feedback" role="alert">
            <strong>{{ $errors->first('name') }}</strong>
        </span>
        @endif
    </div>
</div>

<div class="form-group row">
    <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

    <div class="col-md-6">
        <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required>

        @if ($errors->has('email'))
            <span class="invalid-feedback" role="alert">
            <strong>{{ $errors->first('email') }}</strong>
        </span>
        @endif
    </div>
</div>

<div class="form-group row">
    <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

    <div class="col-md-6">
        <input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>

        @if ($errors->has('password'))
                <span class="invalid-feedback" role="alert">
                <strong>{{ $errors->first('password') }}</strong>
        </span>
        @endif
    </div>
    </div>


     <input id="role_id" type="hidden" name="role_id" value="2">

注册控制器

    <select class="" name="class_id">
         <option value="null">Class</option>
         @foreach($cid as $cc)
             @if($counterr==0)
                 <option selected="selected" value="{{$cc->id}}">{{$cc->title}}</option>
                 {{$counterr++}}
             @else

                 <option value="{{$cc->id}}">{{$cc->title}}</option>
             @endif
        @endforeach
    </select>

     <select section="" name="section_id">
         <option value="null">Section</option>
         @foreach($sec as $sc)
             @if($counterrr==0)
                 <option selected="selected" value="{{$sc->id}}">{{$sc->title}}</option>
                 {{$counterrr++}}
             @else

                 <option value="{{$sc->id}}">{{$sc->title}}</option>
             @endif
         @endforeach
     </select>
   </div>
  </div>
 </form>

存储学生

protected function create(array $data)
    {
        return Registration::create([
            'reg_id' => $data['reg_id'],
            'user_id' => $data['user_id'],
            'class_id' => $data['class_id'],
            'section_id' => $data['section_id'],


        ]);
    }

    public function store(Request $request)
    {

        registration::create($request->all());

        return back();
    }

现在,我希望将要注册的同一user_id也应保存在注册表

我在控制器中使用了简单的存储方法来保存数据。

2 个答案:

答案 0 :(得分:1)

很容易..您应该创建用户并使用其ID ...类似这样的东西

学生控制器:

public function storeStudent(Request $request)
{
     $created_user = User::create([
         'name' => $request->name,
         'email' => $request->email,
         'password' => bcrypt($request->password),
         'parent_id' => $request->parent_id,
         'role_id' => $request->role_id,
     ]);

     //And after that you need to store user_id in Registration table , so:

     Registration::create([
         'user_id' => $created_user->id,
         'class_id' => $request->class_id,
         'section_id' => $request->section_id,
     ]);

     return redirect()->back();

}

希望有帮助

答案 1 :(得分:0)

请参阅此链接。您可能会了解有关Laravel中关系的一些想法。 https://laravel.com/docs/5.8/eloquent-relationships

如果这没有帮助,那么我需要查看您的控制器和型号。请粘贴您的控制器和型号代码。