简体   繁体   中英

Add value to a table by retrieve value from another table laravel

I am currently making web (online store) for my assignment. I have product table which include idStore and idProduct, and I am making order table which also include idStore, idProduct, and idOrder. How can I add values for Order.idStore and Order.idProduct by retrieving Product.idStore and Product.idProduct that customer chose?

create_orders

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

Please help me, I have to submit this before 19th of May. Thanks!

Not sure exactly what you're trying to do, but I guess you are just looking to get a product based on ID and StoreID which you would just do

Product::where('idProduct', $id)->where('idStore', $store)->first()

Or if you want to automatically return a 404 just call ->firstOrFail()

Just for reference, you might want to look how changing how your IDs work, usually rather than using this:

$table->increments('idOrder')
$table->integer('idProduct')

You would look at the more suitable naming convention such as:

$table->id()
$table->integer('product_id')

Even though if you are looking at allowing more than 1 product through an order, I'd suggest creating a table which can hold the basket session, you can use session()->getId() to identify if to the customer/visitor and and/either store session_id on both orders and let's say baskets table or store order_id on the baskets table in order to make the relationship.

Also just to let you know inside your create.blade.php , your hidden input:

<input type="hidden" class="form-control" name="$order->idStore">

Needs to be wrapped in the name like so:

<input type="hidden" class="form-control" name="{{ $order->idStore }}">

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