如何使用单一形式在具有Relation的两个表中添加值?

问题描述 投票:0回答:2

我是Laravel的新成员,我想使用单一表单在两个表中添加数据。我的桌子和他们的关系是

enter image description here

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

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

学生表的字段

<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();
    }

商店学生

    public function storeStudent(Request $request)
{
    $user=new User();
    $user->name=$request->name;
    $user->password=Hash::make($request->password);
    $user->email=$request->email;
    $user->role_id=2;
    $user->parent_id=$request->parent_id;
    $user->save();


    return back();
}

现在我想要注册的同一个user_id也应该保存在注册表中

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

php laravel laravel-5 eloquent
2个回答
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();

}

希望能帮助到你


0
投票

请参阅此链接。你可能会对laravel中的关系有所了解。 https://laravel.com/docs/5.8/eloquent-relationships

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

© www.soinside.com 2019 - 2024. All rights reserved.