我开发在线购物系统我有一些表来制作原油基地
我想以专业和有效的方式插入记录,但我不知道我使用的最佳方式是什么here I mention two table product or company
product fields id p_name ,p_price,p_detail
company fields id comp_name comp_detail
我的控制器就像这样
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
class Cdesigination extends Controller
{
public function index()
{
$insertData = array(
"name" => Input::get("name"),
"detail" => Input::get("detail"),
"token_key" => Input::get("_token")
);
return view('company');
}
public function product()
{
$record = array(
"p_name" => Input::get("name"),
"p_price" => Input::get("price"),
"p_detail" => Input::get("detail")
);
return view('product');
}
}
如何在数据库中插入记录Laravel 5.2中的专业方法
答案 0 :(得分:0)
我喜欢insert()
使用Mass Assignment功能(不要忘记为每个模型填充$fillable
数组):
class Cdesigination extends Controller
{
public function company(Request $request)
{
Company::insert($request->all());
return view('company');
}
public function product(Request $request)
{
Product::insert($request->all());
return view('product');
}
}
它简短,易读且易于维护。