取消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;
}
}
答案 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');
}
}