?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.

    评论

报告相同问题?