我的系统创建一个食物菜肴并将其保存在数据库中,我想要的是在我刚刚创建的食物菜肴中添加一种或几种成分,这些成分是通过已经动态的列表复选框选中的。这将看到视图
如您所见,我选择了比萨作为先前创建的菜肴。现在我只需要添加各自数量的配料即可。
说如果用户未选中该复选框,则不会添加成分。
平台迁移
<?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');
}
}
platoController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Plato;
use App\Ingrediente;
use App\PlatoIngrediente;
class PlatoController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$platos = Plato::all();
return view('platos/index', compact('platos'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('platos/create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validatedData = $request->validate([
'nombre' => 'required|max:50',
'valor' => 'required|max:50'
]);
$plato = Plato::create($validatedData);
return redirect('/platos')->with('success','El Plato se guardó correctamente en la base de datos');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$plato = Plato::findOrFail($id);
return view('platos/edit', compact('plato'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$validatedData = $request->validate([
'nombre' => 'required|max:50',
'valor' => 'required|numeric'
]);
Plato::whereId($id)->update($validatedData);
return redirect('/platos')->with('success', 'El Plato se actualizó correctamente en la base de datos');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$plato = Plato::findOrFail($id);
$plato->delete();
return redirect('/platos')->with('success', 'El Palto se eliminó correctamente en la base de datos');
}
}
platinIngrediente迁移
<?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控制器
<?php
namespace App\Http\Controllers;
use App\PlatoIngrediente;
use App\Plato;
use App\Ingrediente;
use Illuminate\Http\Request;
class PlatoIngredienteController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$platos = Plato::all();
return view('platoingrediente/index', compact('platos'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$platos = Plato::all();
$ingredientes = Ingrediente::all();
return view('platoingrediente/create', compact('platos','ingredientes'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validatedData = $request->validate([
'nombre' => 'required|max:50',
'valor' => 'required|max:50',
]);
$validatedData2 = $request->validate([
'id_plato' => 'required|max:50',
'id_ingrediente' => 'required|max:50',
'cantidad' => 'required|max:50'
]);
$plato = Plato::create($validatedData);
$ingrediente = Ingrediente::Create($validateData2);
return redirect('/platos')->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)
{
//
}
}
,然后查看/创建/定位
@extends('layouts.app')
@section('content')
<script>
function showContent(el) {
var element = el.parentNode.parentNode.nextElementSibling.querySelector('.dv');
if (el.checked) {
element.style.display='block';
}
else {
element.style.display='none';
}
}
</script>
<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 en el sistema: </dd>
<select name="" class="form-control">
<option selected>[ SELECCIONA UN PLATO ]</option>
@foreach( $platos as $plato )
<option value="{{ $plato->id }}">{{ $plato->nombre }}</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>{{$ingrediente->id}}</td>
<td>{{$ingrediente->nombre}}</td>
<td>{{$ingrediente->proveedor}}</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" type="number" />
</div>
</div>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
@endsection
如果您需要更多有关模型的信息,请发表评论,如果您需要更多说明,请发表评论!
答案 0 :(得分:0)
在菜肴(柏拉图)和配料(配料)之间建立多对多关系(许多菜肴可以包含很多配料)。
以下假设您同时拥有成分和柏拉图的模型
创建数据透视表:
public function up()
{
Schema::create('plato_ingrediente', function (Blueprint $table) {
$table->increments('id');
$table->integer('plato_id')->unsigned();
$table->integer('ingrediente_id')->unsigned();
});
}
向模型添加关系
柏拉图模型:
public function ingredientes()
{
return $this->belongsToMany('App\Plato');
}
成分模型
public function platos()
{
return $this->belongsToMany('App\Ingrediente');
}
使用带有成分ID作为其值的复选框的输入,将成分添加到plato中:
$plato = Plato::find($id);
$plato->ingredientes()->attach($ingrediente_id);
从plato中检索成分:
$plato = Plato::find($id);
$ingredientes = $plato->ingredientes //returns a collection of ingredientes
$foreach($ingredientes as $ingrediente)
{
//do something e.g.
echo $ingrediente->id;
}