如何从单个控制器将数据插入到 Laravel 中的多个表中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48200368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert data into multiple table in laravel from single Controller?
提问by Prince John
[I am new in Laravel]
[我是 Laravel 的新手]
I am using Laravel 5.5
我正在使用 Laravel 5.5
I have ProductControllerand model Productalso I have two tables productsand pcategories. I want to insert data into pcategoriestable from ProductController. How to do this properly? I have used DB::table('pcategories')->
enter code hereinsert($data);
but it's not inserting created_at
and updated_at
value.
我有ProductController和模型Product还有两个表products和pcategories。我想从ProductController将数据插入到pcategories表中。如何正确地做到这一点?我在这里使用了输入代码,但它不是插入和值。DB::table('pcategories')->
insert($data);
created_at
updated_at
Another question: Can I call multiple models into a controller?
另一个问题:我可以将多个模型调用到一个控制器中吗?
This is my controller
这是我的控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Product;
use Image;
class ProductController extends Controller {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$product->categorieslist = Product::table('pcategories')->get();
return view('product.add')->with($data);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
$product->categorieslist = Product::table('pcategories')->get();
return view('product.add')->with($data);
}
public function all() {
$product->products = Product::table('products')->get();
return view('product.all', $data);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
$this->validate($request, [
'producttitle' => 'required',
'price' => 'required',
'photo' => 'image|mimes:jpeg,png,jpg,gif,svg',
]);
$product = new Product;
$image = $request->file('photo');
if ($request->file('photo')) {
$product->photo = time() . '.' . $image->getClientOriginalExtension();
$imagePath = public_path('/images/product');
$img = Image::make($image->getRealPath());
$img->resize(250, 250, function ($constraint) {
$constraint->aspectRatio();
})->save($imagePath . '/' . $product->photo);
}
$product->title = $request->input('producttitle');
$product->description = $request->input('description');
$product->category = $request->input('category');
$product->price = $request->input('price');
$product->saleprice = $request->input('saleprice');
$product->weight = $request->input('weight');
$product->dimension = $request->input('dimension');
$product->color = $request->input('color');
$product->save();
return redirect('/product/')->with('success', 'Successfully Added');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id) {
$product = Product::find($id);
return view('product.show')->with('product', $product);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id) {
$data['product'] = Product::find($id);
$data['categorieslist'] = Product::table('pcategories')->get();
return view('product.edit')->with($data);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
$this->validate($request, [
'producttitle' => 'required',
'price' => 'required',
'photo' => 'image|mimes:jpeg,png,jpg,gif,svg',
]);
$product = Product::find($id);
$image = $request->file('photo');
if ($request->file('photo')) {
$product->photo = time() . '.' . $image->getClientOriginalExtension();
$imagePath = public_path('/images/product');
$img = Image::make($image->getRealPath());
$img->resize(250, 250, function ($constraint) {
$constraint->aspectRatio();
})->save($imagePath . '/' . $product->photo);
}
$product->title = $request->input('producttitle');
$product->description = $request->input('description');
$product->category = $request->input('category');
$product->price = $request->input('price');
$product->saleprice = $request->input('saleprice');
$product->weight = $request->input('weight');
$product->dimension = $request->input('dimension');
$product->color = $request->input('color');
$product->save();
return redirect('/product/all')->with('success', 'Successfully Updated');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
Product::find($id)->delete();
return redirect('/product/all')->with('success', 'Successfully Deleted');
}
public function category() {
$data['categories'] = DB::table('pcategories')->get();
return view('product.category')->with($data);
}
public function storecategory(Request $request) {
$this->validate($request, [
'category' => 'required',
]);
$data = array();
$data['category'] = $request->input('category');
DB::table('pcategories')->insert($data);
return redirect('/product/category')->with('success', 'Successfully Added');
}
public function categorydestroy($id) {
DB::table('pcategories')->where('id', $id)->delete();
return redirect('product/category/')->with('success', 'Successfully Deleted');
}
}
回答by Sohel0415
Use Eloquent Model
instead of Query Builder
. Fields created_at
,updated_at
are "part" of Eloquent
. You either use Eloquent
or insert manually
those fields.
使用Eloquent Model
代替Query Builder
。字段created_at
,updated_at
是 的“一部分” Eloquent
。您可以使用Eloquent
或插入manually
这些字段。
If you want to use Eloquent
, then make two model Product
and PCategory
. And then insert with your model.
如果要使用Eloquent
,则制作两个模型Product
和PCategory
。然后插入您的模型。
PCategory::create($data);
And you need to mass assigned
your field on model class.If you don't want to mass assigned your fields, than do like this-
并且您需要mass assigned
在模型类上使用您的字段。如果您不想大量分配您的字段,请这样做-
$pcategory = new PCategory();
//$pcategory->column1 = $data['column1'] or $data->column1;
$pcategory->save();
To learn more, follow thisofficial doc.
要了解更多信息,请遵循此官方文档。
And for your second part, Yes, You can.
对于你的第二部分,是的,你可以。
回答by Larigyn
First, create a proper relationship with your models ( one to many, etc. ) Second, you can use the DB::transact to insert data into multiple tables in DB. It has it's own advantages rather than just inserting Fk in tables to fill in the data. For further info you can search in google.
首先,与您的模型建立适当的关系(一对多等)。其次,您可以使用 DB::transact 将数据插入到 DB 中的多个表中。它有它自己的优点,而不是仅仅在表格中插入Fk来填充数据。有关更多信息,您可以在谷歌搜索。