错误Laravel将两个相同的记录插入到Mysql中

时间:2017-10-13 05:14:05

标签: laravel laravel-5.3 mysql-error-1064

取消Laravel时遇到问题。我在Mysql中插入一个页面但在Mysql中显示两个相同的记录。

路由器:

Route::post('pages','Admin\Pages@adds');

控制器:     

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class Pages extends Controller
{
    //
    public function adds(Request $request)
    {
        $pages = \App\Page::adds($request);
        return redirect('admin/pages');
    }
}

型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Page extends Model
{
    protected $table = 'page';

    public static function adds($request)
    {
        $pages = new Page;
        $pages->title = $request->input('title');
        $pages->content = $request->input('body');
        $pages->status = $request->input('status');
        $pages->timeupdate = time();
        $pages->save();
        return 1;
    }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

试试这个:

型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Page extends Model
{
    protected $table = 'page';

}

控制器:

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Page;

class Pages extends Controller
{
    //
    public function adds(Request $request)
    {
        $pages = new Page;
        $pages->title = $request->input('title');
        $pages->content = $request->input('body');
        $pages->status = $request->input('status');
        $pages->timeupdate = time();
        $pages->save();
        //return 1;
        return redirect('admin/pages');
    }
}

答案 1 :(得分:0)

你应该试试这个:

<强>模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Page extends Model
{
    protected $table = 'page';

}

<强>控制器

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Page;

class Pages extends Controller
{
    public function adds(Request $request)
    { 

      $data = $request->all();
      $pages = \App\Page::create($data);
      return redirect('admin/pages');
    }
}