Получить записи на <select>в Laravel 5 - PullRequest
1 голос
/ 17 мая 2019

Мне нужно в моем приложении принести записи, которые являются внешними ключами в выборке. В выбранном мне нужно, чтобы он показал название тарелки с едой (полукокса), вот и все.

У меня есть таблицы: Plato Y platoIngrediente (где находятся внешние ключи), например:

Платоновская миграция:

<?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');
    }
}

Платформа миграции:

  <?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');

        }
    }

ПлатоИнгредиент Модель:

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

и вид:

<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>

Нужно больше кода?

Ответы [ 2 ]

0 голосов
/ 18 мая 2019

В контроллере:

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

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

В блейд-файле:

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

     @forelse($platoingrediente as $key => $val)
         <option value="{{ $key }}">{{ $val }}</option>
     @empty
     @endforelse
  </select>
0 голосов
/ 17 мая 2019

Вам нужно перебрать $ platos

@foreach($platos as $plato)
  <option value=">{{ $plato->nombre }}">{{ $plato->nombre }}</option>
@endofeach
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...