简体   繁体   中英

Get records to a <select> in laravel 5

I have to in my application need to bring the records that are foreign keys in a select. In the select I need it to show the name of a plate of food(char), that's all.

I have the tables of : plato y platoIngrediente (where the foreign keys are), like this:

plato migration:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePlatosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('platos', function (Blueprint $table) {
            $table->increments('id');
            $table->char('nombre',50);
            $table->double('valor', 8, 2);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('platos');
    }
}

platoIngrediente migration:

  <?php

    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;

    class CreatePlatoIngredienteTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
        Schema::create('platoIngrediente', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('plato_id');
        $table->foreign('plato_id')->references('id')->on('plato');
        $table->unsignedInteger('plato_id');
        $table->foreign('ingrediente_id')->references('id')->on('ingrediente');
        $table->double('cantidad', 8, 2);
        $table->timestamps();
            });
        }

        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
             Schema::dropIfExists('platoIngrediente');

        }
    }

platoIngrediente Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PlatoIngrediente extends Model
{
    public $table = "platoIngrediente";

    protected $fillable = ['cantidad'];


    public function platos(){
        return $this->belongsTo('App\Plato','id_plato');
    }

    public function ingredientes(){
        return $this->belongsTo('App\Ingrediente','id_ingrediente');
    }

}

platoIngredienteController :

<?php

namespace App\Http\Controllers;

use App\PlatoIngrediente;
use Illuminate\Http\Request;

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('platoingrediente/create');
    }

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

    /**
     * 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)
    {
        //
    }

    public function formulario(){
        $platos = Platos::with('platos','ingredientes')->get();
        return view('/platoingrediente/index', compact('$platos'));

    }

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

and the view:

<div class="up sombra card">
  <div class="card-header">
    Creacion del plato
  </div>
  <div class="card-body">
    <div class="up">
  @if(session()->get('success'))
    <div class="alert alert-success">
      {{ session()->get('success') }}  
    </div><br />
  @endif


  <dd>Primero selecciona un plato: </dd>
  <select class="custom-select">

  <option selected>[ SELECCIONA UN PLATO ]</option>
  <option value="$plato->id">{{ $plato->nombre }}</option>
  <option value="2">Two</option>
  <option value="3">Three</option>

</select>
</div>
</div>
</div>

Need more code?

You need to loop through the $platos

@foreach($platos as $plato)
  <option value=">{{ $plato->nombre }}">{{ $plato->nombre }}</option>
@endofeach

In controller:

    public function index(){
        $platoingrediente = PlatoIngrediente::pluck('nombre', 'id');

        return view('platoingrediente/index', compact('platoingrediente'));
    }

In blade file:

  <select class="custom-select">
     <option selected>[ SELECCIONA UN PLATO ]</option>

     @forelse($platoingrediente as $key => $val)
         <option value="{{ $key }}">{{ $val }}</option>
     @empty
     @endforelse
  </select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM