PHP강좌/[라라벨]게시판만들기강좌

[라라벨+mysql]게시판 만들기 강좌 #29. 함수를 이용해서 새글, 첨부파일, 댓글 수 표시

에스크리토 2024. 8. 13. 13:10
반응형

 

이런식으로 리스트 화면에 관련 아이콘과 숫자를 표시하려고 한다.

 

우선 뷰파일에 함수를 추가해보자.

 

/resources/views/boards/index.blade.php

@extends('boards.layout')
@section('header')
    @include('boards.toptitle', ['toptitle'=>'게시판 목록', 'multi'=>$multi])
@endsection
@section('content')
    @if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
    @endif
    <div style="text-align:right;">
        <a href="/boards/write/{{ $multi }}"><button class="text-xl">등록</button></a>
    </div>
    <table class="table table-striped table-hover">
        <colgroup>
            <col width="10%"/>
            <col width="15%"/>
            <col width="45%"/>
            <col width="15%"/>
            <col width="15%"/>
        </colgroup>
        <thead>
        <tr>
            <th scope="col">번호</th>
            <th scope="col">이름</th>
            <th scope="col">제목</th>
            <th scope="col">조회수</th>
            <th scope="col">등록일</th>
        </tr>
        </thead>
        <tbody>
            <?php
                $pagenumber = $_GET["page"]??1;
                $total = $boards->total();
                $idx = $total-(($boards->currentPage()-1) * 20);
            ?>
            @foreach ($boards as $board)
                <tr>
                    <th scope="row">{{ $idx-- }}</th>
                    <td>{{ $board->userid }}</td>
                    <td><a href="/boards/show/{{$board->bid}}/{{$pagenumber}}">{{ $board->subject }}</a> {!! dispattach($board->bid) !!} {!! dispmemo($board->memo_cnt,$board->memo_date) !!} {!! dispnew($board->regdate) !!} </td>
                    <td>{{ number_format($board->cnt) }}</td>
                    <td>{{ disptime($board->regdate) }}</td>
                </tr>
            @endforeach
            @if(!$total)
                <tr>
                    <th scope="row" colspan="5">게시물이 없습니다.</td>
                </tr>
            @endif
        </tbody>
    </table>
    <div>
        {!! $boards->withQueryString()->links() !!}
    </dvi>
@endsection

 

Helpers 파일에 함수를 추가하자.

 

/app/Helpers.php

<?php

use App\Models\FileTables;

function disptime($regdate){

    $sec = strtotime(date("Y-m-d H:i:s")) - strtotime($regdate);
    if ($sec < 60) {
        $dispdates = $sec."초 전";
    } else if ($sec > 60 && $sec < 3600) {
        $f = floor($sec / 60);
        $dispdates = $f."분 전";
    } else if ($sec > 3600 && $sec < 86400) {
        $f = floor($sec / 3600);
        $dispdates = $f."시간 전";
    } else {
        $dispdates = date("Y-m-d",strtotime($regdate));
    }

    return $dispdates;

}

function dispmemo($memo_cnt, $memo_date){
    if((time()-strtotime($memo_date))<86400){
        return "<span style='color:red;'>[".$memo_cnt."]</span>";
    }else{
        return null;
    }
}

function dispnew($regdate){
    if((time()-strtotime($regdate))<86400){
        return "<span style='color:red;'>New</span>";
    }else{
        return null;
    }
}

function dispattach($bid){
    $attaches = FileTables::where('pid',$bid)->whereIn('code',['boardattach','editorattach'])->where('status',1)->first();
    if($attaches){
        return '
        <path d="M6.002 5.5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0"/>
        <path d="M1.5 2A1.5 1.5 0 0 0 0 3.5v9A1.5 1.5 0 0 0 1.5 14h13a1.5 1.5 0 0 0 1.5-1.5v-9A1.5 1.5 0 0 0 14.5 2zm13 1a.5.5 0 0 1 .5.5v6l-3.775-1.947a.5.5 0 0 0-.577.093l-3.71 3.71-2.66-1.772a.5.5 0 0 0-.63.062L1.002 12v.54L1 12.5v-9a.5.5 0 0 1 .5-.5z"/>
      </svg>';
    }else{
        return null;
    }
}

?>

 

혹시 helpers파일이 없는 사람들은 이전 강좌를 확인해보자

 

 

반응형