dongzi0850 2018-01-11 05:34
浏览 90
已采纳

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

[I am new in Laravel]

I am using Laravel 5.5

I have ProductController and model Product also I have two tables products and pcategories. I want to insert data into pcategories table 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.

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

}
  • 写回答

2条回答 默认 最新

  • douxin1163 2018-01-11 05:40
    关注

    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.

    If you want to use Eloquent, then make two model Product and PCategory. And then insert with your model.

    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-

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

    To learn more, follow this official doc.

    And for your second part, Yes, You can.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?