Laravel将数据插入两个不同的表

时间:2019-03-06 15:50:47

标签: laravel vue.js

为什么我的函数不将输入存储到两个不同的表中?

public function store(Request $request) {

    $this->validate($request,[
        'product_name' => 'required|string|max:191',
        'quantity' => 'required|integer',
        'product_id' => 'required',
        'category' => 'string|max:191',
        'brand' => 'string|max:191',
        'provider_id' => 'required'
    ]);

    return Product::create([
        'product_name' => $request['product_name'],
        'quantity' => $request['quantity']
    ]); 

    return ProductDetails::create([
        'product_id' => $request['product_id'],
        'category' => $request['category'],
        'brand' => $request['brand'],
        'provider_id' => $request['provider_id']
    ]);    
}

该函数仅执行return Product::create,但是,如果我将return ProductDetails::create删除或放在return Product::create之前,该函数确实起作用,但仅执行return ProductDetails::create

2 个答案:

答案 0 :(得分:2)

return终止函数的执行并返回值。

public function store(Request $request)
    {
        //
        $this->validate($request,[
            'product_name' => 'required|string|max:191',
            'quantity' => 'required|integer',
            'product_id' => 'required',
            'category' => 'string|max:191',
            'brand' => 'string|max:191',
            'provider_id' => 'required'
        ]);

        $product = Product::create([
            'product_name' => $request['product_name'],
            'quantity' => $request['quantity']
        ]); 

        $productDetails = ProductDetails::create([
            'product_id' => $request['product_id'],
            'category' => $request['category'],
            'brand' => $request['brand'],
            'provider_id' => $request['provider_id']
        ]);

        return $product;

    }

答案 1 :(得分:0)

我相信您必须对代码进行模块化以实现更好的维护:

首先,不要在控制器中验证您的请求。创建表单请求:

php artisan make:request StoreProduct

在app / Http / Requests / StoreProduct中的文件中创建验证逻辑

class StoreProduct extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */

     public function authorize()
     {
        return true;
     }

    /**
    * Get the validation rules that apply to the request.
    *
    * @return array
    */

    public function rules()
    {
        return [
            'product_name' => 'required|string|max:191',
            'quantity' => 'required|integer',
            'product_id' => 'required',
            'category' => 'string|max:191',
            'brand' => 'string|max:191',
            'provider_id' => 'required'
        ];
}

在您的控制器文件中。

//include the request file
use App\Http\Requests\StoreProject;

// (...) the rest of your code

// The validation is called before the controller method
public function store(StoreProduct $request)
{
    $validated = $request->validated();

    $this->storeProduct($request); 

    $this->storeProductDetails($request);

    // Return a redirect response and not a $product
    // Is a good practice to redirect to your products list
    // for example: redirect('/productsList');
    // Here, I'm redirecting to your root page;
    return redirect('/');

}

public function storeProduct(StoreProduct $request){
    Product::create([
        'product_name' => $request['product_name'],
        'quantity' => $request['quantity']
    ]); 
}

public function storeProductDetails(StoreProduct $request){
    ProductDetails::create([
        'product_id' => $request['product_id'],
        'category' => $request['category'],
        'brand' => $request['brand'],
        'provider_id' => $request['provider_id']
    ]);
}

参考:Laravel Form Request Validation