mercredi 19 juin 2019

Retrieve data from database into select and checkboxes fields to edit in Laravel

I'm trying to do the CRUD operations on a themes resource. I've created the resource controller and properly managed the index, create, store and show functions. It is the first time I'm doing the CRUD with Laravel. In my form I'm using a select and a checkbox group field. Here is the themeEdit blade view

@extends('layouts.layout')

@section('content')
    <form method="PUT">
        @csrf
        <!-- Intitulé du thème -->
        <input type="text" name="intitule" id="intitule" placeholder="Intitulé du thème" value="  " required><br>
        <!-- Catégorie -->
        <select name="categorie" required>
            <option value="">-- Catégorie --</option>
            <option value="web">Développement web</option>
            <option value="appMobile">Programmation application mobile</option>
            <option value="secure">Sécurisation</option>
            <option value="other">Autre</option>
        </select> <br>
        <!-- Filière désirée -->
        <input type="checkbox" name="filiere[]" id="GL" value="  " required>
        <label for="GL">Génie Logiciel</label><br>
        <input type="checkbox" name="filiere[]" id="SI" value="  " required>
        <label for="SI">Sécurité Informatique</label><br>
        <input type="checkbox" name="filiere[]" id="IM" value="  " required>
        <label for="IM">Internet et Multimédia</label><br>
        <input type="checkbox" name="filiere[]" id="SIRI" value="  " required>
        <label for="SIRI">Systèmes d'Information et Réseaux Informatiques</label><br>
        <!-- Descriptif -->
        <textarea name="description" id="description" placeholder="Description de la thématique" required>  </textarea><br>

        <input type="submit" name="submit" id="submit" value="Ajouter">
        <span id="error-message" class="text-danger"></span>
        <span id="success-message" class="text-success"></span>
    </form>

@endsection

I'm passing the data to the view via Controller in the edit function

/**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $theme = Theme::findOrFail($id);
        return view('themeEdit', ['theme' => $theme]);
    }

I tried what you saw in the view code above to see if I can have the checkboxes checked or unchecked accordingly to the data in the database. It is not doing what is expected..

<!-- Filière désirée -->
        <input type="checkbox" name="filiere[]" id="GL" value="  " required>
        <label for="GL">Génie Logiciel</label><br>
        <input type="checkbox" name="filiere[]" id="SI" value="  " required>
        <label for="SI">Sécurité Informatique</label><br>
        <input type="checkbox" name="filiere[]" id="IM" value="  " required>
        <label for="IM">Internet et Multimédia</label><br>
        <input type="checkbox" name="filiere[]" id="SIRI" value="  " required>
        <label for="SIRI">Systèmes d'Information et Réseaux Informatiques</label><br>

If it can be useful, this is how I'm storing the data in the database via store controller function

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $theme = new Theme;
        $theme->intitule = $request->input('intitule');
        $theme->description = $request->input('description');
        $theme->categorie = $request->input('categorie');
        $request->merge([
            'filiere' => implode(',', (array) $request->get('filiere'))
        ]);
        $theme->filiereDesiree = $request->input('filiere');
        $theme->save();
        echo "Enregistré";
    }


What I want to know is how do I get the selected or checked data from the database and retrieve them properly in their respective field in the view.




Aucun commentaire:

Enregistrer un commentaire