title: How to insert data into multiple table in laravel from single Controller?
body: <p>[I am new in Laravel]</p>
  
  <p>I am using Laravel 5.5</p>
  
  <p>I have <strong>ProductController</strong> and model <strong>Product</strong> also I have two tables <strong>products</strong> and <strong>pcategories</strong>. I want to insert data into <strong>pcategories</strong> table from <strong>ProductController</strong>. How to do this properly? I have used <code>DB::table('pcategories')-&gt;</code>enter code here<code>insert($data);</code> but it's not inserting <code>created_at</code> and <code>updated_at</code> value.</p>
  
  <p>Another question: Can I call multiple models into a controller?</p>
  
  <p>This is my controller </p>
  
  <pre><code>&lt;?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-&gt;categorieslist = Product::table('pcategories')-&gt;get();
          return view('product.add')-&gt;with($data);
      }
  
      /**
       * Show the form for creating a new resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function create() {
          $product-&gt;categorieslist = Product::table('pcategories')-&gt;get();
          return view('product.add')-&gt;with($data);
      }
  
      public function all() {
          $product-&gt;products = Product::table('products')-&gt;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-&gt;validate($request, [
              'producttitle' =&gt; 'required',
              'price' =&gt; 'required',
              'photo' =&gt; 'image|mimes:jpeg,png,jpg,gif,svg',
          ]);
  
          $product = new Product;
  
          $image = $request-&gt;file('photo');
          if ($request-&gt;file('photo')) {
              $product-&gt;photo = time() . '.' . $image-&gt;getClientOriginalExtension();
              $imagePath = public_path('/images/product');
              $img = Image::make($image-&gt;getRealPath());
              $img-&gt;resize(250, 250, function ($constraint) {
                  $constraint-&gt;aspectRatio();
              })-&gt;save($imagePath . '/' . $product-&gt;photo);
          }
  
          $product-&gt;title = $request-&gt;input('producttitle');
          $product-&gt;description = $request-&gt;input('description');
          $product-&gt;category = $request-&gt;input('category');
          $product-&gt;price = $request-&gt;input('price');
          $product-&gt;saleprice = $request-&gt;input('saleprice');
          $product-&gt;weight = $request-&gt;input('weight');
          $product-&gt;dimension = $request-&gt;input('dimension');
          $product-&gt;color = $request-&gt;input('color');
          $product-&gt;save();
          return redirect('/product/')-&gt;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')-&gt;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')-&gt;get();
          return view('product.edit')-&gt;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-&gt;validate($request, [
              'producttitle' =&gt; 'required',
              'price' =&gt; 'required',
              'photo' =&gt; 'image|mimes:jpeg,png,jpg,gif,svg',
          ]);
  
          $product = Product::find($id);
  
          $image = $request-&gt;file('photo');
          if ($request-&gt;file('photo')) {
              $product-&gt;photo = time() . '.' . $image-&gt;getClientOriginalExtension();
              $imagePath = public_path('/images/product');
              $img = Image::make($image-&gt;getRealPath());
              $img-&gt;resize(250, 250, function ($constraint) {
                  $constraint-&gt;aspectRatio();
              })-&gt;save($imagePath . '/' . $product-&gt;photo);
          }
  
          $product-&gt;title = $request-&gt;input('producttitle');
          $product-&gt;description = $request-&gt;input('description');
          $product-&gt;category = $request-&gt;input('category');
          $product-&gt;price = $request-&gt;input('price');
          $product-&gt;saleprice = $request-&gt;input('saleprice');
          $product-&gt;weight = $request-&gt;input('weight');
          $product-&gt;dimension = $request-&gt;input('dimension');
          $product-&gt;color = $request-&gt;input('color');
          $product-&gt;save();
          return redirect('/product/all')-&gt;with('success', 'Successfully Updated');
      }
  
      /**
       * Remove the specified resource from storage.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function destroy($id) {
          Product::find($id)-&gt;delete();
          return redirect('/product/all')-&gt;with('success', 'Successfully Deleted');
      }
  
      public function category() {
          $data['categories'] = DB::table('pcategories')-&gt;get();
          return view('product.category')-&gt;with($data);
      }
  
      public function storecategory(Request $request) {
          $this-&gt;validate($request, [
              'category' =&gt; 'required',
          ]);
  
          $data = array();
          $data['category'] = $request-&gt;input('category');
          DB::table('pcategories')-&gt;insert($data);
          return redirect('/product/category')-&gt;with('success', 'Successfully Added');
      }
  
      public function categorydestroy($id) {
          DB::table('pcategories')-&gt;where('id', $id)-&gt;delete();
          return redirect('product/category/')-&gt;with('success', 'Successfully Deleted');
      }
  
  }
  </code></pre>