lundi 8 avril 2019

How to add values in two tables having Relation using single form?

I m new at Laravel, I want to add data in two tables using single form. My table and their relation are

enter image description here

I want to add all the fields for user then select the class_id and then section_id through dropdown.

And then register and I want that after data is registered . The Registration table should have user_id of that new user registered and selected class and section id.

My blade page for student register is

@extends('layouts.app')

@section('content')

<div class="">
    <div class="box">
        <div class="box-header">
            <h3 class="box-title">Add Student</h3>
        </div>
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">


                <div class="card-body">

                     <form method="POST" action="">
                        @csrf

                        <div class="form-group row">
                            <label for="name" class="col-md-4 col-form-label text-md-right"></label>

                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control" name="name" value="" required autofocus>

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

                        <div class="form-group row">
                            <label for="email" class="col-md-4 col-form-label text-md-right"></label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control" name="email" value="" required>

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

                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right"></label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control" name="password" required>

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

                        <div class="form-group row">
                            <label for="password-confirm" class="col-md-4 col-form-label text-md-right"></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 row">
                             <label for="parent" class="col-md-4 col-form-label text-md-right"></label>
                             @php
                                 use Illuminate\Support\Facades\DB;

                                 $uid=DB::table('users')->where('role_id','4')->get();
                                 $counter=0;
                             @endphp

                             <select user="" name="parent_id">
                                 <option value="null">User</option>
                                 @foreach($uid as $user)
                                     @if($counter==0)
                                         <option selected="selected" value=""></option>
                                         
                                     @else

                                         <option value=""></option>
                                     @endif
                                 @endforeach
                             </select>
                             <div class="col-md-6">



                             </div>
                         </div>




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


                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">

                                    
                                </button>

                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
    </div>
</div>

@endsection

but I m confused how to add fields of registration table in it as it should register in same user_id.

Do I have to make any functions in Models for it also.

Thanks in advance :)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire