?Briella 2016-11-06 22:25 采纳率: 0%
浏览 202

Laravel DB插入错误500

Unable to insert into database in a Laravel controller. Below is the controller. I keep getting an error highlight in Dreamweaver at the following line:

$users = DB::table('records')->insert([
    'product' => 'john@example.com',
    'name' => 'john@example.com',
    'email' => 'john@example.com'
]);

I keep getting error 500

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\DB;

class create extends Controller
{
    //
    public function createRecord(Request $request){
        json_decode($request->getContent(), true);

        //return $request;
        //$something ='asdf';
        //return $something;

        $users = DB::table('records')->insert([
            'product' => 'john@example.com',
            'name' => 'john@example.com',
            'email' => 'john@example.com'
        ]);
    }
}
  • 写回答

2条回答 默认 最新

  • weixin_33690963 2016-11-06 22:39
    关注

    I don't really see anything wrong with this. Have you ensured that the data type matches in the database to what you are inserting? It's hard to really gather this when you haven't provided the 500 error you are receiving.

    Try the following instead:

    1. Create your model. php artisan make:model Record

    2. Instantiate a new instance of that model. $record = new \App\Record;

    3. Add your data using the Eloquent ORM.

      $record->product = "john@example.com";
      $record->name = "john@example.com";
      $record->email = "john@example.com";
      
    4. Finally, save it: $record->save();

    So, in full this would look like the following:

     $record = new \App\Record;
     $record->product = "john@example.com";
     $record->name = "john@example.com";
     $record->email = "john@example.com";
     $record->save();
    

    See if that works for you.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?