Correction status:Uncorrected
Teacher's comments:
<?php
// 命名空间:和目录对应
// 类名称和文件名称一致
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class Home extends Controller {
public function index(){
$time = date('Y-m-d H:i:s');
$data = [];
$data['time'] = $time;
$data['name'] = 'laravel中文';
//第一种方法,数组当做参数
//return view('test',$data);
//第二种方法,with
return view('test')->with('time',$time)->with('name','with链式调用');
}
//数据库查询(原生查询)
//::静态方法,->需要new
//DB::select查询的结果第一层是数组,第二层是对象,get()将结果转为两维数组,getall()未转。
public function get(){
//select * from article
echo '<pre>';
$res = DB::select('select * from article');
//print_r($res);
$lists = [];
foreach($res as $val){
$lists[] = (array)$val;
}
$data['result']=$lists;
//print_r($data);
return view('dbselect',$data);
}
public function getall(){
//select * from article
echo '<pre>';
$res = DB::select('select * from article');
//print_r($res);
$data['result']=$res;
print_r($data);
return view('dbselect1',$data);
}
//数据库更新(原生更新)
public function test_updateadafd(){
//select * from article
echo '<pre>';
$res = DB::update('update article set title="999" where id =2');
var_dump($res);
}
//新增(原生)
public function insert_mytest(){
$res = DB::insert('insert article(`id`,`title`) values(5,"5款简洁的layui后台管理模板推荐")');
var_dump($res);
}
//删除(原生)
public function delete_asdf(){
$res = DB::delete('delete from article where id=5');
var_dump($res);
}
//高级数据库查询方法
public function finds(){
$res = DB::table('article')->where('id',3)->first();
echo '<pre>';
print_r($res);
}
}
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
//echo date('Y-m-d H:i:s');
//return view('welcome');
return view('test');
});
Route::get('/article/p/aaa', 'Home@index');
Route::get('/admin/article/lists', 'Admin\Article@lists');
Route::get('/dbselect', 'Home@get');
Route::get('/dbselect1', 'Home@getall');
Route::get('/dbupdate', 'Home@test_updateadafd');
Route::get('/dbinsert', 'Home@insert_mytest');
Route::get('/dbdelete', 'Home@delete_asdf');
Route::get('/dbfinds', 'Home@finds');
<!DOCTYPE html>
<html>
<head>
<title>my test</title>
</head>
<body>
<h3><?php echo $name;?></h3>
php中文网
<div><?php echo $time;?></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>my test</title>
<link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
</head>
<body>
<table class="layui-table">
<thead>
<tr>
<th>ID</td>
<th>标题</td>
</tr>
</thead>
<tbody>
<?php foreach($result as $val){?>
<tr>
<th><?php echo $val['id']?></td>
<th><?php echo $val['title']?></td>
</tr>
<?php }?>
</tbody>
</table>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>my test</title>
<link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
</head>
<body>
<table class="layui-table">
<thead>
<tr>
<th>ID</td>
<th>标题</td>
</tr>
</thead>
<tbody>
<?php foreach($result as $val){?>
<tr>
<th><?php echo $val->id?></td>
<th><?php echo $val->title?></td>
</tr>
<?php }?>
</tbody>
</table>
</body>
</html>