[英]Add value to a table by retrieve value from another table laravel
我目前正在为我的任务制作 web(在线商店)。 我有包含 idStore 和 idProduct 的产品表,我正在制作还包含 idStore、idProduct 和 idOrder 的订单表。 如何通过检索客户选择的 Product.idStore 和 Product.idProduct 来为 Order.idStore 和 Order.idProduct 添加值?
创建订单
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->increments('idOrder');
$table->integer('idProduct');
$table->integer('idStore');
$table->date('rentdate');
$table->date('returndate');
$table->integer('price');
$table->string('ship_to');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
}
OrderController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Order;
use App\Product;
class OrderController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$orders = Order::paginate(4);
return view('v4.index', compact('orders'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create($slug)
{
$order = Product::where('idProduct', $slug)->first()->product()->orderBy('created_at', 'DESC')->paginate(12);
return view('v4.create', compact('order'));
}
/**
* 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 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)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
create.blade.php
<!-- create.blade.php -->
<!DOCTYPE html> <html>
<head>
<meta charset="utf-8">
<title>Order</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<div class="container"> <h2>Add Order</h2><br/>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div><br />
@endif
@if (\Session::has('success'))
<div class="alert alert-success">
<p>{{ \Session::get('success') }}</p>
</div><br />
@endif
<form method="post" action="{{url('v4')}}">
{{csrf_field()}}
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<label for="idStore">ID Store: </label>
<input type="hidden" class="form-control" name="$order->idStore">
<p> {{$order->idStore}} </p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<label for="idProduct">ID Product: </label>
<input type="hidden" class="form-control" name="idProduct">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<label for="ship_to">Ship To:</label>
<input type="text" class="form-control" name="ship_to">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<label for="rentdate">Rent Date:</label>
<input type="date" class="form-control" name="rentdate">
</div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<label for="returndate">Return Date:</label>
<input type="date" class="form-control" name="returndate">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<label for="price">Price: </label>
<input type="hidden" class="form-control" name="price">
<p> {{$product->price}} </p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4"></div> <div class="form-group col-md-4">
<button type="submit" class="btn btn-success" style="margin- left:38px">Add Order</button>
</div>
</div>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
请帮帮我,我必须在 5 月 19 日之前提交。 谢谢!
不确定您到底要做什么,但我想您只是想获得基于ID
和StoreID
的产品,您会这样做
Product::where('idProduct', $id)->where('idStore', $store)->first()
或者,如果您想自动返回 404,只需调用->firstOrFail()
仅供参考,您可能想查看如何更改您的 ID 的工作方式,通常而不是使用这个:
$table->increments('idOrder')
$table->integer('idProduct')
您将查看更合适的命名约定,例如:
$table->id()
$table->integer('product_id')
即使您正在考虑通过订单允许超过 1 个产品,我建议创建一个可以容纳篮子 session 的表,您可以使用session()->getId()
来确定客户/访客和和/或者在两个orders
上存储session_id
,让我们说baskets
表或在baskets
表上存储order_id
以建立关系。
也只是为了让您知道在您的create.blade.php
中,您的隐藏输入:
<input type="hidden" class="form-control" name="$order->idStore">
需要像这样包装在名称中:
<input type="hidden" class="form-control" name="{{ $order->idStore }}">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.