如何将数据从单个控制器插入到laravel中的多个表中?

iq0todco  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(308)

[我是新来的]
我用的是laravel 5.5
我有productcontroller和model product,还有两个表products和pc categories。我想从productcontroller将数据插入pcategories表。如何正确地做到这一点?我用过 DB::table('pcategories')-> 在此处输入代码 insert($data); 但它没有插入 created_at 以及 updated_at 价值观。
另一个问题:我可以调用多个模型到一个控制器?
这是我的控制器

<?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');
    }

}
fcipmucu

fcipmucu1#

首先,与模型建立适当的关系(一对多等);其次,可以使用db::transact将数据插入db中的多个表中。它有自己的优点,而不是仅仅在表中插入fk来填充数据。更多信息,你可以在谷歌搜索。

zqdjd7g9

zqdjd7g92#

使用 Eloquent Model 而不是 Query Builder . 领域 created_at , updated_at 是…的一部分 Eloquent . 你要么用 Eloquent 或插入 manually 那些田地。
如果你想用 Eloquent ,然后制作两个模型 Product 以及 PCategory . 然后插入你的模型。

PCategory::create($data);

你需要 mass assigned 模型类上的字段。如果不想批量分配字段,请执行以下操作-

$pcategory = new PCategory();
//$pcategory->column1 = $data['column1'] or $data->column1;
$pcategory->save();

要了解更多信息,请遵循此官方文档。
至于你的第二部分,是的,你可以。

相关问题