iT邦幫忙

2021 iThome 鐵人賽

DAY 24
0
永豐金融APIs

釋放你的潛能用技能交易吧!系列 第 24

[Day24] 第二十四章 - 建立point評分表 (複習前面的方法還有query builder)

  • 分享至 

  • twitterImage
  •  

前言

昨天學習了query builder
今天我們的目的就是要建立評分表

所謂評分表理當應該是除了自己以外,對其他人的技能評分
當然自己不能評分到自己的技能 !!

就跟特級廚師測驗一樣~/images/emoticon/emoticon01.gif

目標

  1. 建立point的migration,model,route,controller,view
  2. 練習昨天的query builder來進行篩選他人分數

實作開始

1. 建立migration以及model

php artisan make:model Point -m

我們先建立point的資料model模型 並且建立migration

2.migration 建立資料表型態修正

在database/migrations/create_points_table.php裡面修改

    public function up()
    {
        Schema::create('points', function (Blueprint $table) {
            $table->id();
            $table->integer('skill_id');
            $table->integer('user_id');
            $table->integer('judge_user_id');
            $table->integer('point');
            $table->timestamps();
        });
    }

這邊的目的主要是我們評分表要記錄

  • skill_id : 總要紀錄評的技能是誰吧XD
  • user_id : 為了方便查找順便紀錄這項技能擁有者是誰
  • judge_user_id : 給分數的人是誰也很重要!! 要記錄下來
  • point: 主要評分的分數

接者就下指令

php artisan migrate

3. 修改route順便建立controllrt

先下指令因為我們route要使用resource方法對應controller
沒有controllrt會報錯誤

php artisan make:controller PointController --resource

我們一樣先把CRUD建好

在web.php裡面加入

Route::resource('points', 'PointController');

4. 修改controller

我們先到Model Point增加前端要傳的fillable參數

class Point extends Model
{
    use HasFactory;
    protected $fillable = [
        'point', 'user_id', 'judge_user_id', 'skill_id'
    ];
}

接者我們來到point的controller裡面

我們來修改一下index function

    public function index()
    {
        //
        if (Auth::check()) {
            $user = Auth::user();
            $skills = Skill::query()->where('user_id', '!=', $user->id)->get();
            // dd($filterSkills);
            return view('point.index', ['skills' => $skills]);
        }
        return view('user.login');
    }

評分顧名思義就是要評別人,我們這邊要先判斷使用者是誰就要先進行登入
第二步驟再來判斷使用者是誰,接者過濾掉除了該使用者後撈取技能來評分
我這邊登入的使用者是roni
所以只會顯示 admin 的技能 化學 單一項目

接者我們把view新增一個point的資料夾
我們要來做point的前端介面

/images/emoticon/emoticon08.gif

各位小可愛應該也有發現惹這邊我們可以copy skill的來使用
我們先copy skill的使用
接者修改參數吧!!

5. Point view

這個檔案
views/point/index.blade.php

<head>
  <!-- Scripts -->
  <script src="{{ asset('js/app.js') }}" defer></script>

  <!-- Styles -->
  <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<div>
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>ID</th>
        <th>標題</th>
        <th>使用者ID</th>
        <th>技能評分</th>
      </tr>
    </thead>
    <tbody>
      @foreach($skills as $skill )
      <tr>
        <td>{{$skill->id}}</td>
        <td>{{$skill->title}}</td>
        <td>{{$skill->user_id}}</td>
        <td><button type="button" class="btn btn-primary">進入評分</button></td>
      </tr>
      @endforeach
    </tbody>
  </table>
</div>

我們先把controllrt filter的skill篩進去
接者多增加一個button吧
https://ithelp.ithome.com.tw/upload/images/20211010/20121052YiRg9hPJee.png

總結

今天先完成到這邊明天把評分 跟列表串完吧!!


上一篇
[Day23] 第二十三章 - 學會laravel的query方法來filter資料(Query Builder)
下一篇
[Day25] 第二十五章-新增空白的point表單 (跨資料查詢還有對應細節)
系列文
釋放你的潛能用技能交易吧!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言