模型
class Home extends Model
{
use SoftDelete;
protected $deleteTime = 'delete_time';
protected $table='nam';
public function store($date,$savename){
$this->username=$date['username'];
$this->password=$date['password'];
$this->repassword=$date['repassword'];
$this->type_id=$date['type_id'];
$this->pic=$savename;
$this->nam=$date['nam'];
$this->content=$date['content'];
return $this->save();
}
public function show($keyword){
return $this->join('type','nam.type_id=type.type_id')->where('username','like',"%$keyword%")->paginate([
'list_rows'=>'2',
'query'=>compact('keyword'),
]);
}
public function del($id){
return $this->destroy($id);
}
public function get($id){
return $this->find($id);
}
public function updateuser($id,$arr){
$this->get($id);
return $this->where('id','=',$id)->update($arr);
}
//软删
public function recycle(){
return $this->onlyTrashed()->paginate(2);
}
//恢复
public function dd($id){
$obj=$this->onlyTrashed()->find($id);
return $obj->restore();
}
//真删除
public function remove($id){
return $this->destroy($id,true);
}
}
控制器
class User
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
$typedate=Db::table('type')->select();
return View::fetch('user',compact('typedate'));
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function store(Request $request)
{
$date=$request->param();
// 获取表单上传文件 例如上传了001.jpg
$file = request()->file('pic');
// 上传到本地服务器
$savename = \think\facade\Filesystem::putFile( 'topic', $file);
$obj=new Home();
$res=$obj->store($date,$savename);
if($res){
return redirect('show');
}
}
public function show(){
$keyword=input('keyword');
$obj=new Home();
$res=$obj->show($keyword);
return View::fetch('user/show',compact('res','keyword'));
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
$obj=new Home();
$res=$obj->get($id);
return View::fetch('user/edit',compact('res'));
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request)
{
$id=$request->param('id');
$arr=$request->all();
$obj=new Home();
$res=$obj->updateuser($id,$arr);
if($res){
return redirect('show');
}
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function del($id)
{
$obj=new Home();
$res=$obj->del($id);
if($res){
return redirect('show');
}
}
//软删
public function recycle(){
$obj=new Home();
$res=$obj->recycle();
return View::fetch('user/recycle',compact('res'));
}
//恢复
public function dd($id){
$obj=new Home();
$res=$obj->dd($id);
if($res){
return redirect('show');
}
}
//真删除
public function remove($id){
$obj=new Home();
$res=$obj->remove($id);
if($res){
return redirect('show');
}
}
}
|