如何将php变量直接插入数据库laravel?

时间:2015-07-17 19:49:37

标签: laravel query-builder

我想使用query builder laravel直接将我的值插入数据库, 但问题是如何将这些数据变量保存到数据库

    public function create(epanelRequest $request)
{
    $applicantName  = Input::get('ApplicantName');
    $applicantEmail = Input::get('ApplicantEmail');
    $applicantPhone = Input::get('ApplicantPhone');
    $applicantCity =  Input::get('ApplicantCity');

    $id = DB::table('epanel')->max('id');
    $insertedId = $id + 1;

    //$xuserName = preg_split('/\s+/', $applicantName);
    //$userName = "ssdhdhjdj";

    DB::table('epanel')->insertGetId([ 'username'=> $userName ,'name' => $applicantName, 'email' => $ApplicantEmail, 'phone' => $applicantPhone, 'counter' => 1, 'city' => $applicantCity, ]);

    return redirect()->back();

}

1 个答案:

答案 0 :(得分:0)

如果您将表设置为Laravel中的模型,您应该能够像这样初始化表中的新条目:

$table_variable = new ModelName;

对于每个值,你可以这样输入:

$table_variable->column = $applicantName;

在功能结束时,您可以输入$table_variable->save();

进行保存

至少我是如何去做的。