laravel中控制器调用模型 使用示范(2)
程序员文章站
2022-03-24 20:12:39
...
目录
App\Http\Controllers\Admin\ArticleController.php
控制器
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Support\Facades\Request; // 引入request
use App\Http\Model\Admin\Article; // 使用模型
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
class ArticleController extends Controller
{
public function index()
{
// 查询
$article = new article();
$data = $article->read()->toArray();
return view('admin/article/index',['data'=>$data]);
}
public function edit_list()
{
// 显示原内容
// 从链接,获取id
$input = Request::all();
$art_id = $input['id'];
// 查询
$article = new article();
$data = $article->only_cha('id',$art_id);
return view('admin/article/edit',['data'=>$data]);
}
// 操作数据库,可以使用模型,可以使用DB
public function edit_art(Request $request)
{
// echo '<pre>';
$input = Request::all();
$bb['art_title'] = $input['art_title'];
$bb['art_content'] = $input['art_content'];
$art_id = $input['art_id'];
// 修改
$article = new article();
$res = $article->upd('id',$art_id,$bb);
if($res)
{
return back()->with('success','操作成功');
}else{
return back()->with('success','操作失败');
}
}
public function delete_art()
{
$input = Request::all();
$art_id = $input['id'];
// 删除
$article = new article();
$data = $article->del('id',$art_id);
if($data)
{
return back()->with('success','操作成功');
}else{
return back()->with('success','操作失败');
}
}
public function login(Request $request)
{
$input = Request::all();
$bb['username'] = $input['username'];
$bb['password'] = $input['password'];
// 添加
// $res = DB::table('user')->insert($bb); // 已插入数据库
$res = $user->add($bb);
if($res)
{
return view('admin/index/index'); // 后台首页
}else{
echo '222';
}
}
}