Laravel下拉列表使用一对多关系保存数据

时间:2018-02-06 03:54:25

标签: php laravel drop-down-menu blade

你好如何使用刀片从下拉列表中保存值,而下拉列表数据来自另一个表格我使用一对多的关系,但我可以&#39 ; t将值保存到我的控制器

注意:我无法保存以下内容的值: Student_id,Classes_id,Level_id

控制器

 public function store(Request $request)
{
    //
    $this->validate($request, [


        'accountname'=>'required|max:50',
        'Student_id'=>'required|max:50',
        'Level_id'=>'required|max:50',
        'Classes_id'=>'required|max:50',
        'status'=>'required|max:50',

            ]);


            $accounts = new Account();
            $accounts->accountname = $request->accountname;
            $accounts->Student_id = $request->Student_id;
            $accounts->Level_id = $request->Level_id;
            $accounts->Classes_id = $request->Classes_id;
            $accounts->status = $request->status;
            $accounts->save();

  //Display a successful message upon save
    return redirect()->route('accounts.index')
        ->with('flash_message', 'Success.');
}

查看-刀片



<form action="{{route('accounts.store')}}" method="POST">
    {{csrf_field()}}

<div class="form-group">
<label for="accountname">School Year</label>
<input type="text" name="accountname" id="accountname" value="{{old('accountname')}}" placeholder="Enter SCHOOLYEAR" class="form-control" required>
</div>


<div class="form-group">
<label>STUDENT</label>
<select style="width: 200px" class="form-control" id="Student_id" name="Student_id"  >
<option value="N/A" dissable="true" selected="true">--SELECT--</option>
@foreach($students as $student)
    <option value="{{$student->id}}">{{$student->studentname}}</option>
@endforeach
</select>
</div>



<div class="form-group">
<label>Level</label>
<select style="width: 200px" class="form-control" id="Level_id" name="Level_id">
<option value="N/A" dissable="true" selected="true">--SELECT--</option>
@foreach($levels as $level)
    <option value="{{$level->id}}">{{$level->levelname}}</option>
@endforeach
</select>
</div>


<div class="form-group">
<label>Class</label>
<select style="width: 200px" class="form-control" id="Classes_id" name="Classes_id">
<option value="N/A" dissable="true" selected="true">--SELECT--</option>
@foreach($classes as $class)
    <option value="{{$class->id}}">{{$class->classname}}</option>
@endforeach
</select>
</div>


<div class="form-group">
<label>Status</label>
<select style="width: 200px" class="form-control" id="status" name="status">
<option value="N/A" dissable="true" selected="true">--SELECT--</option>
<option value="ACTIVE">ACTIVE</option>
<option value="WITHDRAW">WITHDRAW</option>
<option value="GRADUATE">GRADUATE</option>
</select>
</div>

<div><button type="submit" class="btn btn-success">SUBMIT</button></div>
</form>
&#13;
&#13;
&#13;

请让我知道我应该编辑哪个部分

2 个答案:

答案 0 :(得分:1)

@ Sagar Gautam

修复它,但是如何在我的索引中显示名称而不是id?

&#13;
&#13;
<h1><i class="fa fa-users"></i> Enroll List</h1>
    
        <table class="table table-dark table-hover">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Student</th>
                    <th>Level</th>
                    <th>Class</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                @foreach ($accounts as $account)
                <tr>
                    <td>{{ $account->accountname }}</td>
                    <td>{{ $account->Student_id }}</td>
                    <td>{{ $account->Level_id}}</td>
                    <td>{{ $account->Classes_id }}</td>
                    <td>{{ $account->status }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
       
        <div><a href="{{route('accounts.create')}}" class="btn btn-success">Add Student <i class="fa fa-plus-square"></i></a></div>
     
    <br>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

dissable="true" selected="true"代码中删除option

<div class="form-group">
<label>STUDENT</label>
 <select style="width: 200px" class="form-control" id="Student_id" name="Student_id"  >
    <option value="N/A">--SELECT--</option>
    @foreach($students as $student)
    <option value="{{$student->id}}">{{$student->studentname}}</option>
    @endforeach
 </select>
</div>



<div class="form-group">
<label>Level</label>
 <select style="width: 200px" class="form-control" id="Level_id" name="Level_id">
  <option value="N/A">--SELECT--</option>
  @foreach($levels as $level)
   <option value="{{$level->id}}">{{$level->levelname}}</option>
  @endforeach
 </select>
</div>


<div class="form-group">
<label>Class</label>
 <select style="width: 200px" class="form-control" id="Classes_id" name="Classes_id">
  <option value="N/A">--SELECT--</option>
  @foreach($classes as $class)
   <option value="{{$class->id}}">{{$class->classname}}</option>
  @endforeach
  </select>
</div>


<div class="form-group">
  <label>Status</label>
  <select style="width: 200px" class="form-control" id="status" name="status">
   <option value="N/A">--SELECT--</option>
   <option value="ACTIVE">ACTIVE</option>
   <option value="WITHDRAW">WITHDRAW</option>
   <option value="GRADUATE">GRADUATE</option>
  </select>
</div>