laravel网站开发实例------6

前几篇博客实现了用户管理,从本篇博客起,开始写比赛报名系统

比赛报名系统

系统设计

功能:
1、管理员可以随便发布比赛(删除比赛),用户可以点击报名,管理员还可以设置给每个人单独发比赛账号
2、游客不能进行报名,只有注册了才能报名,避免恶意报名

管理员登录退出

1、注册路由

//管理员登录
$api->post('adminlogin','AdminsController@login')
    ->name('api.adminlogin.login');
//管理员退出
$api->post('adminlogout','AdminsController@logout')
    ->name('api.adminlogout.logout');

路由注册完了之后,需要写迁移文件,用于存储管理员账号

php artisan make:migration create_admins_table –create=admins

public function up()
{
    Schema::create('admins', function (Blueprint $table) {
        $table->increments('id');
        $table->string('username');
        $table->string('password');
        $table->timestamps();
    });
}

执行命令,进行迁移

php artisan migrate

然后再写一个模型文件

php artisan make:model App\Models\Admin

namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
class Admin extends Model
{
    //
    use Notifiable;

    protected $table = "admins";

    protected $fillable = [
       'username','password'
    ];

    protected $hidden = [
        'password',
    ];
}

最后再重新生成一个控制器

php artisan make:Controller Api\AdminsController

2、写控制逻辑

public function login(Request $request){
    $this->validate($request,[
       'username' => 'required',
        'password' => 'required',
    ]);
    $password = $request->password;
    $username = $request->username;
    if($request->session()->has($username)){
        return $this->response->array([
            'info' => '您已经登录',
            'status_code'=>400,
        ])->setStatusCode(200);
    }else{
        $admin = Admin::where('username',$username)->first();
        if(md5($password)==$admin->password){
            $adminkey = $username;
            $key = 'adminlogin'.str_random(15);
            $request->session()->put($adminkey,$key);
            $request->session()->put($key,$adminkey);
            return $this->response->array([
                'key' => $key,
                'info' => '登录成功',
                'status_code' => 200,
            ])->setStatusCode(200);
        }
    }
}
public function logout(Request $request){
    $this->validate($request,[
       'verification_key' => 'required',
    ]);
    $key = $request->verification_key;
    $admin = $request->session()->get($key);
    $request->session()->forget($key);
    $request->session()->forget($admin);
    return $this->response->array([
        'info' => '登出成功',
        'status_code' => 200,
    ])->setStatusCode(200);
}

3、测试
登陆成功

{
    "key": "adminloginyF5TuBjYpG1vyAl",
    "info": "登录成功",
    "status_code": 200
}

登陆失败

{
    "info": "您已经登录",
    "status_code": 400
}
///////
{
    "info": "密码错误",
    "status_code": 200
}

登出成功

{
    "info": "登出成功",
    "status_code": 200
}
管理员管理比赛

管理比赛包括新增、删除、修改和查询比赛信息
比赛信息:比赛全称、负责人名字、负责人电话号码、比赛时间、比赛地点、组织单位、比赛规则详情、报名人数、最新公告、比赛结果表id、报名表id、比赛状态(是否还能报名)
路由

//管理员管理比赛
$api->post('creategame','GamesController@create')
    ->name('api.creategame.create');
$api->post('deletegame','GamesController@delete')
    ->name('api.deletegame.delete');
$api->get('showgames','GamesController@show')
    ->name('api.showgames,show');
$api->post('udpategame','GamesController@update')
    ->name('api.updategame.update');

创建数据库迁移和持久化模型

php artisan make:migration create_games_table –create=games

public function up()
    {
        Schema::create('games', function (Blueprint $table) {
            $table->increments('id');
            $table->string('gamename');
            $table->string('fuzeren');
            $table->string('fuzerenphone');
            $table->dateTime('gametime');
            $table->text('address');
            $table->string('origanizetion');
            $table->text('guize');
            $table->integer('number');
            $table->text('news');
            $table->integer('resultid');
            $table->integer('baomingid');
            $table->integer('gamestatus');
            $table->timestamps();
        });
    }

php artisan migrate

php artisan make:model App\Models\Game

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;

class Game extends Model
{
    use Notifiable;

    protected $table = "games";

    protected $fillable = [
        'gamename','fuzeren','fuzerenphone','gametime','address','origanizetion','guize','number','news','resultid','baomingid','gamestatus',    ];

    protected $hidden = [
        'fuzeren','fuzerenphone',
    ];
}

创建竞赛管理控制器

php artisan make:Controller Api\GamesController

在创建新的游戏的时候需要关联一个表,报名表,所以需要用一个数据库来记录报名表最新是多少。

public function create(Request $request){
  $this->validate($request,[
     'key' => 'required',
  ]);
  $key = $request->key;
  if($request->session()->has($key)){
      $game = Game::create([
          'gamename' => $request->gamename,
          'fuzeren' => $request->fuzeren,
          'fuzerenphone' => $request->fuzerenphone,
          'gametime' => $request->gametime,
          'address' => $request->address,
          'origanizetion' => $request->origanizetion,
          'guize' => $request->guize,
          'number' => 0,
          'news' => $request->news,
          'gamestatus' => 0,
      ]);
      $baoming = Baoming::where('id',1)->first();
      $game->baomingid = $baoming->number;
      $game->save();
      $baoming->number = $baoming->number+1;
      $baoming->save();
      return $this->response->array([
          'info' => '发布成功',
          'status_code' => 200,
      ])->setStatusCode(201);
  }else{
      return $this->response->array([
          'info' => '请登录后重试',
          'status_code' => 400,
      ])->setStatusCode(200);
  }
}

目前先实现创建比赛的功能,后续的管理功能再加

{
    "info": "发布成功",
    "status_code": 200
}

从数据库中也可以看见相关的数据。

下一讲 用户报名

猜你喜欢

转载自blog.csdn.net/xielinrui123/article/details/81256434