Laravel将数据插入两个不同的表中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel将数据插入两个不同的表中相关的知识,希望对你有一定的参考价值。
为什么我的函数不将输入存储到两个不同的表中?
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
。
答案
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;
}
另一答案
我相信您必须模块化您的代码以获得更好的维护:
首先,不要在您的控制器中验证您的请求。创建表单请求:
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
以上是关于Laravel将数据插入两个不同的表中的主要内容,如果未能解决你的问题,请参考以下文章
如何通过单击适配器类中代码的项目中的删除按钮来删除列表视图中的项目后重新加载片段?