目录
1.新建数据库
2.修改文件.env
3.新建文件UserController.php
4.修改文件api.php
5.浏览器测试
6.新建模型文件User.php
7.修改控制器文件UserController.php
8.修改路由文件api.php
9.浏览器测试2
后续
1.新建数据库
2.修改文件.env
3.新建文件UserController.php
D:\Microsoft\phpstudy_pro\WWW3\blog\app\Http\Controllers
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class UserController extends Controller {
public function query()
{
$student=DB::select("select * from px_user_score_log where id < 10");
//var_dump($student);
//dd()是laravel提供的方法,可以将一个数组以节点树的形式展示出来。
//dd($student);
$json = json_encode(array(
"result"=>200,
"message"=>"登录成功!",
"data"=>$student
),JSON_UNESCAPED_UNICODE);
echo($json);
}
public function add()
{
$bool=DB::insert("insert into px_user_score_log(user_id,create_time,action,score,coin)
values(?,?,?,?,?)",[1000,1111111,'test1',66,2]);
var_dump($bool);
}
public function delete($id)
{
$num=DB::delete('delete from px_user_score_log where id= ?',[$id]);
echo $num;
}
public function update()
{
$bool=DB::update('update px_user_score_log set score= ? where id= ? ',[78,55]);
echo $bool;
}
}
4.修改文件api.php
D:\Microsoft\phpstudy_pro\WWW3\blog\routes
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::group(['prefix'=>"users"],function() {
//Route::prefix('users')->group(function () {
Route::get('query', [UserController::class,'query']);
Route::get('add', [UserController::class,'add']);
Route::get('delete/{id}', [UserController::class,'delete']);
Route::get('update', [UserController::class,'update']);
});
5.浏览器测试
http://localhost/api/users/query
Laravel 自带的 Eloquent ORM 为您的数据库提供了一个优雅的、简单的 ActiveRecord 实现。每一个数据库的表有一个对应的 "Model" 用来与这张表交互。
Eloquent ORM - Laravel - 为 WEB 艺术家创造的 PHP 框架。
6.新建模型文件User.php
在app目录下建立一个User模型,即User.php
D:\Microsoft\phpstudy_pro\WWW3\blog\app
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model{
//指定表名
protected $table = 'px_user_score_log';
//指定主键
protected $primaryKey = 'id';
//laravel会默认维护created_at,updated_at两个字段
public $timestamps = false;
//create方法新增
protected $fillable=['user_id','create_time','action','score','coin'];
public function getDateFormat()
{
return time();
}
}
7.修改控制器文件UserController.php
?D:\Microsoft\phpstudy_pro\WWW3\blog\app\Http\Controllers
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\User;
class UserController extends Controller {
//Eloquent ORM 结构生成器
public function query2(){
// 1、查询所有数据
$users=User::all();
//dd($users);
// 2、查询一条,依据主键查询。findOrFail()查找不存在的记录时会抛出异常
//$users=User::find(6); //id=6
//var_dump($users['attributes']);
// 3、查询构造器的使用,省略了指定表名
//$users=User::get();
//var_dump($users);
return $users;
}
public function add2()
{
/*$users = new User();
$users->user_id=2000;
$users->create_time=22222;
$users->action='test2';
$users->score=34;
$users->coin=3;
$bool=$users->save(); //保存
echo $bool;*/
User::create(['user_id'=>2001,'create_time'=>22221,'action'=>'test2_1','score'=>12,'coin'=>3]);
}
}
8.修改路由文件api.php
?D:\Microsoft\phpstudy_pro\WWW3\blog\routes
use App\Http\Controllers\UserController;
Route::group(['prefix'=>"users"],function() {
Route::get('query2', [UserController::class,'query2']);
Route::get('add2', [UserController::class,'add2']);
});
9.浏览器测试2
http://localhost/api/users/query2
后续
如果你觉得该方法或代码有一点点用处,可以给作者点个赞;╮( ̄▽ ̄)╭ 如果你感觉方法或代码不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进。o_O??? 谢谢各位童鞋们啦( ′ ▽ ` )ノ ( ′ ▽ ` )っ!!!
|