簡體   English   中英

通過從另一個表 laravel 檢索值向表添加值

[英]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 日之前提交。 謝謝!

不確定您到底要做什么,但我想您只是想獲得基於IDStoreID的產品,您會這樣做

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM