如何在我的选择选项中动态显示我的学生的名字和姓氏

时间:2019-07-25 23:34:31

标签: php laravel

我是Laravel的初学者。我正在使用ajax。我想在我的选择选项中按名字(eleves)姓(prénom)显示学生(nom)的数据,但问题是它仅显示名字({ {1}})没有姓氏(标称)。但是我需要关联名字和姓氏。请帮助我。

控制器

prénom

我的视图,在那里我正在使用ajax函数

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Note;
use App\Matiere;
use App\Eleve;
use App\Classe;
use DB;
class DropdownlistController extends Controller
{

    public function index()

    {
        $notes = Note::all();
        $matieres = Matiere::all();
        $eleves = Eleve::all();
        $classes = Classe::all();
        $classes = DB::table("classes")->pluck("classe","id");

        return view('admin.dropdownlists',compact('classes','notes','matieres','eleves'));
    }


    public function store(Request $request)
    {
        Note::create($request->all());

        return redirect()->back();
    }

    public function getStateList(Request $request)
    {
        $eleves = DB::table("eleves")
            ->where("classe_id",$request->classe_id)
            ->pluck("nom" ,"prenom","id");
        return response()->json($eleves);
    }

    public function getCityList(Request $request)
    {
        $matieres = DB::table("matieres")
            ->where("classe_id",$request->classe_id)
            ->pluck("nom_matiere","id");
        return response()->json($matieres);
    }
}

路线

@extends('layouts.admin')

@section('content')
    <section class="wrapper">
        <div class="table-title">
            <div class="row">
                <div class="col-sm-6">
                    <h2> ESpace de Gestion <b></b> des notes </h2>
                </div>
                <div class="col-sm-6">

                    <a href="#addEmployeeModal" class=" btn btn-success" data-toggle="modal"><i class="material-icons">&#xE147;</i>
                        <span>Ajouter un nouvelle note </span></a>

                </div>
            </div>
        </div>
        <div class="row mt">
            @if(session()->has('success'))
                <div class="alert alert-success">
                    {{session()->get('success')}}
                </div>
            @endif
            <div class="col-lg-12">
                <div class="content-panel">

                    <section id="no-more-tables">
                        <table class="table table-bordered table-striped table-condensed cf">
                            <thead class="cf">
                            <tr>
                                <th>id-note</th>
                                <th>Nom</th>

                                <th>classe</th>
                                <th>matiére</th>
                                <th>Note</th>
                                <th>les actions</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($notes as $note)
                                <tr>

                                    <td class="numeric" data-title="id-parent">{{$note->id}}</td>
                                    <td class="numeric"
                                        data-title="id-parent">{{$note->eleve->nom}}  {{$note->eleve->prenom}}</td>
                                    <td class="numeric" data-title="id-parent">{{$note->classe->classe}}</td>

                                    <td class="numeric" data-title="id-parent">{{$note->matiere->nom_matiere}}</td>

                                    <td class="numeric" data-title="Login">{{$note->note}}</td>

                                    <td>
                                        <button href="#editEmployeeModal" class="btn btn-theme"
                                                data-target="#editEmployeeModal " data-catid={{$note->id}}  class="edit"
                                                data-toggle="modal"><i class="material-icons" data-toggle="tooltip"
                                                                       title="Edit">&#xE254;</i></button>
                                        <button href="#deleteEmployeeModal" class="btn btn-theme"
                                                data-target="#deleteEmployeeModal" data-catid="{{$note->id}}"
                                                class="delete" data-toggle="modal"><i class="material-icons"
                                                                                      data-toggle="tooltip"
                                                                                      title="Delete">&#xE872;</i>
                                        </button>
                                    </td>

                                </tr>
                            </tbody>
                            @endforeach
                        </table>
                        <div class="text-center">

                        </div>
                        <div class="clearfix">

                            <div class="hint-text">Affichage de <b>5</b> sur <b>25</b> entrées</div>

                            <div id="addEmployeeModal" href="create" class="modal fade">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <form action="{{route('dropdownlists.store')}}" method="post">
                                            {{csrf_field()}}
                                            <div class="modal-header">
                                                <h4 class="modal-title">Ajouter un éléve</h4>
                                                <button type="button" class="close" data-dismiss="modal"
                                                        aria-hidden="true">&times;
                                                </button>
                                            </div>
                                            <div class="modal-body">
                                                <div class="panel-body">
                                                    <div class="form-group">
                                                        <label for="title">Select classe:</label>
                                                        <select id="classe" name="classe" class="form-control"
                                                                style="width:350px">
                                                            <option value="" selected disabled>Select</option>
                                                            @foreach($classes as $key => $classe)
                                                                <option value="{{$key}}"> {{$classe}}</option>
                                                            @endforeach
                                                        </select>
                                                    </div>
                                                    <div class="form-group">
                                                        <label for="title">Select classe:</label>
                                                        <select id="eleve" name="eleve" class="form-control"
                                                                style="width:350px">


                                                        </select>
                                                    </div>

                                                    <div class="form-group">
                                                        <label for="title">Select matiere:</label>
                                                        <select id="matiere" name="matiere" class="form-control"
                                                                style="width:350px">
                                                            <option value="" selected disabled>Select</option>
                                                        </select>
                                                    </div>


                                                    <div class="form-group">
                                                        <label>la note </label>
                                                        <input type="text" id="note" name="note" class="form-control"
                                                               required>
                                                    </div>
                                                </div>


                                                <div class="modal-footer">
                                                    <input type="button" class="btn btn-default" data-dismiss="modal"
                                                           value="Annuler">
                                                    <input type="submit" class="btn btn-success" value="Ajouter">
                                                </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                        @endsection

                        @section('js')
                            <script type="text/javascript">
                                $('#classe').change(function () {
                                    var classeID = $(this).val();
                                    if (classeID) {
                                        $.ajax({
                                            type: "GET",
                                            url: "{{url('get-state-list')}}?classe_id=" + classeID,
                                            success: function (res) {
                                                if (res) {
                                                    $("#eleve").empty();
                                                    $("#eleve").append('<option>Select</option>');
                                                    $.each(res, function (key, value) {
                                                        $("#eleve").append('<option
                                                        value = "'+eleve.age+'" > '+value+' < /option>');
                                                    });

                                                } else {
                                                    $("#eleve").empty();
                                                }
                                            }
                                        });
                                    } else {
                                        $("#classe").empty();
                                        $("#matiere").empty();
                                    }
                                });
                                $('#classe').on('change', function () {
                                    var classeID = $(this).val();
                                    if (classeID) {
                                        $.ajax({
                                            type: "GET",
                                            url: "{{url('get-city-list')}}?classe_id=" + classeID,
                                            success: function (res) {
                                                if (res) {
                                                    $("#matiere").empty();
                                                    $.each(res, function (key, value) {
                                                        $("#matiere").append('<option
                                                        value = "'+key+'" > '+value+' < /option>');
                                                    });

                                                } else {
                                                    $("#matiere").empty();
                                                }
                                            }
                                        });
                                    } else {
                                        $("#matiere").empty();
                                    }

                                });
                            </script>
                        @endsection

1 个答案:

答案 0 :(得分:1)

Laravel具有非常强大且非常有用的功能,mutators可以使您轻松完成这一工作。通过添加'fullName'变异器,您可以随时在模型中进行此操作。然后,在控制器中,您可以像调用一个字段一样调用该函数。

因此,在您的 Eleve 模型上:

public function getFullNameAttribute(){
    return $this->prenom. " " . $this->nom;
}

然后在任何控制器中,例如,当您要将它们发送到下拉列表时,都可以获取您的集合,然后将其转移到所需的数组中。

获取收藏集:

$elevesCollection = \App\Eleve::orderBy('nom')->select('prenom', 'nom', 'id')->get();

然后,从集合中提取全名和ID:

 $eleves = $elevesCollection ->pluck('name', 'id')->toArray();

您可以将其发送到表单下拉列表。请仔细检查我的代码/拼写,但这应该可以满足您的要求。