????:public function DoAdd(){ // 獲取提交出來(lái)的數(shù)據(jù) $data = Request::param(); // 將產(chǎn)品名獨(dú)立出來(lái) $title = $data['title']; // 利用產(chǎn)品名作為查詢(xún)條件查詢(xún)對(duì)應(yīng)的數(shù)據(jù) &nbs
public function DoAdd()
{
// 獲取提交出來(lái)的數(shù)據(jù)
$data = Request::param();
// 將產(chǎn)品名獨(dú)立出來(lái)
$title = $data['title'];
// 利用產(chǎn)品名作為查詢(xún)條件查詢(xún)對(duì)應(yīng)的數(shù)據(jù)
$info = ProductModel::where('title', $title)->find();
// 判斷是否查詢(xún)到相同的產(chǎn)品名稱(chēng)
if ($info == true) {
// 返回提示信息
return ['res' => 0, 'msg' => '產(chǎn)品標(biāo)題重復(fù)!'];
}
// 加入添加時(shí)間
$data['time'] = time();
// 添加發(fā)布管理員
$data['username'] = Session::get('username');
// 實(shí)例化模型
$product = new ProductModel();
// 進(jìn)行添加并驗(yàn)證
if ($product->save($data)) {
// 返回提示信息
return ['res' => 1, 'msg' => '發(fā)布成功!'];
} else {
// 返回提示信息
return ['res' => 0, 'msg' => '發(fā)布失??!'];
}
}
public function del()
{
// 獲取需要?jiǎng)h除的產(chǎn)品id
$proId = Request::param('id');
$product = new ProductModel();
if ($product->destroy($proId)) {
return ['res'=>1,'msg'=>'刪除成功!'];
}
?? ???:天蓬老師?? ??:2019-03-30 10:21:18
???? ??:在方法中, 盡可能避免直接使用new, $product = new ProductModel();, 可以通過(guò)對(duì)象注入, 或者 Facade 方式進(jìn)行調(diào)用