【问题标题】:Laravel DB insert error 500Laravel DB 插入错误 500
【发布时间】:2017-03-20 05:03:18
【问题描述】:

无法在 Laravel 控制器中插入数据库。下面是控制器。 我在 Dreamweaver 中不断收到以下行中的错误突出显示:

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

我不断收到错误 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'
        ]);
    }
}

【问题讨论】:

  • 你能给我们看完整错误信息的截图吗?

标签: php ajax laravel post insert


【解决方案1】:

我真的不认为这有什么问题。您是否确保数据库中的数据类型与您插入的数据类型匹配?当您没有提供收到的 500 错误时,很难真正收集到这些信息。

请尝试以下方法:

  1. 创建您的模型。 php artisan make:model Record

  2. 实例化该模型的新实例。 $record = new \App\Record;

  3. 使用 Eloquent ORM 添加您的数据。

    $record->product = "john@example.com";
    $record->name = "john@example.com";
    $record->email = "john@example.com";
    
  4. 最后保存:$record->save();

所以,这将如下所示:

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

看看这是否适合你。

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2018-02-28
      • 2018-03-21
      • 1970-01-01
      • 2015-05-31
      • 2016-04-08
      • 2019-03-16
      相关资源
      最近更新 更多