如何将 id 从 url 插入到我的创建方法

How to insert id from url to my create method

我正在尝试创建仅属于一个订单的产品。我要带着那个订单 ID 来创建表格,但不知道如何将其提取到存储功能中。 更新文件: 产品控制器:

use Illuminate\Http\Request;
use App\Order;
use App\Product;
use Illuminate\Support\Facades\Route;

class ProductController extends Controller
{
    public function create(Order $order){
       
        return view('order.product-create', compact($order));
    }

    public function store(Order $order, Request $request){

   $this->validate($request, [
    'name'=>'required',
    'drawing'=>'nullable|file',
    '3d_file'=>'nullable|file',
    'quantity'=>'integer',
    'unit_price'=>'required',
    'discount'=>'nullable'
   ]);
   $order->products()->create($request->all());
   session()->flash('product-created', 'New product was added successfully');    
   return redirect()->route('order.view');
    }
}

路线:

Route::get('/order/{order}/product/create', 'ProductController@create')->name('product.create');
Route::post('/order/{order}/product', 'ProductController@store')->name('product.store');

view.blade.php ---> link 到产品创建表单

<a href="{{route('product.create', $order)}}" class="btn btn-primary">Add new product</a>

product.create.blade.php ---> 片段到方法操作

action="{{route('product.store', ['order' => $order])}}"

所以目前,我无法进入 product-create.blade.php 并出现错误 $order is undefined

按照我的理解,您想将 Order ID 从您的创建表单传递到您的控制器函数,并基于此您想要保存您的表单数据。

您可以通过

将您的订单 ID 传递给您的 POST route

{{ route('route_name', ['order_id' => $order->id]) }}

你在 web.php 的路线是

Route::post('/order/{order_id}/product/create', 'ProductController@store')->name('route_name)

Create Method with Relationship Relationship Documentation

您可以通过

接受在您的控制器函数中传递的参数
public function store($order_id)
{
   //validation

   //save function
   $order = App\Order::findOrFail($order_id);  //Fails if the passed id is not present in DB
   // Assuming you have one to one relationship
   // Product() is the relationship you declare in your model
   $product = $order->Product()->create([
       'your_columns' => 'values',
   ]);

   // redirect after save

}

编辑:更新问题

use Illuminate\Http\Request;
use App\Order;
use App\Product;
use Illuminate\Support\Facades\Route;

class ProductController extends Controller
{
    public function create(Order $order){

       // you are receiving the Order instance by the Order ID (primary key defined) 

        return view('order.product-create', compact($order));
    }

    public function store(Order $order, Request $request){

        $this->validate($request, [
           'name'=>'required',
           'drawing'=>'nullable|file',
           '3d_file'=>'nullable|file',
           'quantity'=>'integer',
           'unit_price'=>'required',
           'discount'=>'nullable'
        ]);

         $order->products()->create($request->all());
         session()->flash('product-created', 'New product was added successfully');    
         return redirect()->route('order.view');
    }
}

和你的Routes

// order = order id which is passed to the controller Order $order variable instance

Route::get('/order/{order}/product/create', 'ProductController@create')->name('product.create');
Route::post('/order/{order}/product/create', 'ProductController@store')->name('product.store');

你的表格POST

// You can pass the ID of the order result you receive from your `create` function

<form action = "{{ route('product.store',['order' => $order->id]) }}" ... >

动态参数在 routes/web.php 或 routes/api.php

中的路由文件中定义为大括号
// routes/web.php
Route::get('/order/{order}/product/create', 'ProductController@create')->name('product.create');
Route::post('/order/{order}/product', 'ProductController@store')->name('product.store');

在 ProductController.php 中使用路由模型绑定让 Laravel 解析容器外的订单。

// ProductController.php
use App\Order;
use App\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function create(Order $order)
    {
        // Only get here if laravel resolves the $order model

        return view('order.product-create', compact('order'));
    }

    public function store(Order $order, Request $request)
    {
        // Only get here if laravel resolves the $order model

        //validation
        $this->validate($request, [
            'name'       => 'required',
            'drawing'    => 'nullable|file',
            '3d_file'    => 'nullable|file',
            'quantity'   => 'integer',
            'unit_price' => 'required',
            'discount'   => 'nullable'
        ]);

        // Create related Product model and associate it with the Order model.
        // https://laravel.com/docs/7.x/eloquent-relationships#the-create-method
        //
        // Need to allow for mass assignment in the Product.php model
        // https://laravel.com/docs/7.x/eloquent#mass-assignment
        $order->products()->create($request->all());

        // Flash to session and redirect after creating
        session()->flash('product-created', 'New product was added successfully');
        return redirect()->route('order.view');
    }
}

view.blade.php

<a href="{{route('product.create', ['order' => $order]))}}" class="btn btn-primary">Add new product</a>

product.create.blade.php

action="{{route('product.store', ['order' => $order])}}"