mardi 21 mai 2019

Save

I have a dynamic select, which is full of a dynamic array from my database, I need to save the selection of that select and save that selection in another table, for this I have a foreign key like this: I need to save the selection of that select and save that selection in another table, for this I have a foreign key like this:

public function up()
    {
    Schema::create('platoIngrediente', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('plato_id');
    $table->foreign('plato_id')->references('id')->on('platos');
    $table->unsignedInteger('ingrediente_id');
    $table->foreign('ingrediente_id')->references('id')->on('ingredientes');
    $table->double('cantidad', 8, 2);
    $table->timestamps();
        });
    }

the other table where the record is stored (an id and a name) that I'm showing in the select is:

public function up()
    {
        Schema::create('platos', function (Blueprint $table) {
            $table->increments('id');
            $table->char('nombre',50);
            $table->double('valor', 8, 2);
            $table->timestamps();
        });
    }

This is my view.

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

  <dd>Primero selecciona un plato en el sistema: </dd>
  <select name="plato_id" class="form-control">
  <option selected>[ SELECCIONA UN PLATO ]</option>
   @foreach( $platos as $plato )
  <option value=""></option>
 @endforeach
</select>


    <div class="text-center up">
      <label class="name">  Agregar Ingredientes a el plato </label>
  </div>

<table class="table table-striped">
  <thead>
    <tr>
      <td>ID</td>
      <td>Nombre del Ingrediente</td>
      <td>Proveedor</td>
      <td>Agregar</td>
      <td>Cantidad</td>
    </tr>
  </thead>
  <tbody>
      @foreach($ingredientes as $ingrediente)
      <tr>
          <td></td>
          <td></td>
          <td></td>
          <td>

              <label>
            <input type="checkbox" name="check" id="check" value="" onchange="javascript:showContent(this)" />
          </label>

          </td>

          <td>
            <div class="form-group row">
             <div class="col-sm-4">
               <div class="dv" style="display:none;">
            <input class="dvs" name="cantidad" type="number" />
            </div>
          </div>
          </div>
          </td>
      </tr>
      @endforeach

  </tbody>
</table>


</div>
<div class="up text-center">
  <button type="submit" class="btn btn-primary">Crear Plato</button>
</div>
</form>

where I have as name the parameters that are in my database, but for some reason do not work

This is my .store method.

class PlatoIngredienteController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
      $platos = Plato::all();
      $ingredientes = Ingrediente::all();
      $platoingredientes = PlatoIngrediente::all();
      return view('platoingrediente/index', compact('platos','ingredientes','platoingredientes'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $platos = Plato::all();
        $ingredientes = Ingrediente::all();
        $platoingredientes = PlatoIngrediente::all();
        return view('platoingrediente/create', compact('platos','ingredientes','platoingredientes'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

      $validatedData = $request->validate([
          'cantidad' => 'required|max:50',
          'id_plato' => 'required|max:50'
      ]);

      $platoingrediente = PlatoIngrediente::create($validatedData);
      return redirect('/platoingrediente')->with('success','El Plato se guardó correctamente en la base de datos');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\PlatoIngrediente  $platoIngrediente
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\PlatoIngrediente  $platoIngrediente
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\PlatoIngrediente  $platoIngrediente
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\PlatoIngrediente  $platoIngrediente
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

The question.. How do I save the select and if possible the checkbox input to my database?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire