Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:你的学习进度有点慢了
<?php
use App\Http\Controllers\Home;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
//echo date('Y-m-d H:i:s');
return view('welcome');//视图,视图引擎blade
//return view('test');
});
/*Route::get('/article/p/aaa',function(){
return 'php.cn';
});*/
Route::get('/home/index','Home@index');
Route::get('/admins/article/lists','admins\Article@lists');
Route::get('/dbselect','Home@get');
Route::get('/dbupdate','Home@test_updateadafd');
Route::get('/dbinsert','Home@insert_mytest');
Route::get('/dbdelete','Home@delete_asdf');
Route::get('/dbfinds','Home@finds');
<?php
//1.命名空间:和目录对应
//2.类名称和文件文件名称一致
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class Home extends Controller{
public function index(){
//return 'www.php.cn';
$time = date('Y-m-d H:i:s');
$res = DB::select('select * from article');
$lists = [];
foreach($res as $val){
$lists[] = (array)$val;
}
$data['result'] = $lists;
// echo '<pre>';
// print_r($data);
return view('test',$data);
// return view('test')->with('time',$time)->with('name','laravel应用程序');
}
//数据库查询[原生查询]
public function get(){
//select * from article
$res = DB::select('select * from article where id>3');
echo '<pre>';
print_r($res);
}
//数据库更新操作【原生更新】
public function test_updateadafd(){
$res = DB::update('update article set title="最污技术,我竟然秒懂" where id=3');
var_dump($res);
}
//新增数据[原生新增]
public function insert_mytest(){
$res = DB::insert('insert article(`id`,`title`)values(5,"excel鼠标上下失灵")');
var_dump($res);
}
//删除数据【原生删除】
public function delete_asdf(){
$res = DB::delete('delete from article where id=2');
var_dump($res);
}
//高级数据库查询方法【链式调用】
public function finds(){
$res = DB::table('article')->where('id',3)->first();
echo '<pre>';
print_r($res);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
<title>Document</title>
</head>
<body>
<table class="layui-table">
<thead>
<tr>
<td>ID</td>
<td>标题</td>
</tr>
</thead>
<tbody>
<?php foreach($result as $val){?>
<tr>
<td><?php echo $val['id']?></td>
<td><?php echo $val['title']?></td>
</tr>
<?php }?>
</tbody>
</table>
</body>
</html>
<?php
namespace App\Http\Controllers\admins;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class Article extends Controller
{
public function lists(){
echo '后台文章';
}
}