打印可读的消息以获取较小的int值

时间:2018-08-06 10:07:21

标签: php laravel laravel-5

我有一列PAID tinyint(1)。现在,我想在视图页表中将此值0/1显示为UNPAID / PAID。我应该怎么做?

如果PAID列数据为PAID,则将其着色为绿色,如果UNPAID则将其着色为BLUE。我该怎么办?

控制器代码块以获取数据

function fetchData() {
    $ordered_books = OrderedBook::orderBy('id', 'DESC')->get()->toArray();
    return compact('ordered_books');
}

查看页面表代码块

<table id="showBooksIn" class="table table-bordered gridview">
    <thead>
        <tr>
            <th>BOOK ID</th>
            <th>BILLED DATE</th>
            <th>BILLED NUMBER</th>
            <th>QUANTITY</th>
            <th>PRICE</th>
            <th>PAID</th>
            <th>REMARKS</th>
        </tr>
    </thead>
    <tbody>
        @foreach($ordered_books as $data)
        <tr>
            <td> {{$data['BookID']}} </td>
            <td> {{$data['BilledDate']}} </td>
            <td> {{$data['BilledNum']}} </td>
            <td> {{$data['Qunatity']}} </td>
            <td> {{$data['Price']}} </td>
            <td> {{$data->bill_paid}} </td>
            <td> {{$data['Remarks']}} </td>
        </tr>
        @endforeach
    </tbody>
</table>

根据建议,我编辑了模型课程

class OrderedBook extends Model
{
    //
    protected $appends = ['bill_paid'];

    public function getBillPaidAttribute(){
        if($this->BillPaid == 0){
           return 'UNPAID';
        }else {
          return 'PAID'; 
        }
      }
}

现在由于:: ErrorException (E_NOTICE) Undefined property: App\OrderedBook::$BillPaid

而出现错误

2 个答案:

答案 0 :(得分:0)

使用刀片模板中的条件条件并应用css进行着色。

@foreach($ordered_books as $data)
 <tr>
     <td> {{$data['BookID']}} </td>
     <td> {{$data['BilledDate']}} </td>
     <td> {{$data['BilledNum']}} </td>
     <td> {{$data['Qunatity']}} </td>
     <td> {{$data['Price']}} </td>

     @if($data['BillPaid'] === 0)
        <td style='color:blue;'> UNPAID </td>
     @else
        <td style='color:green;'> PAID </td>
     @endif

     <td> {{$data['Remarks']}} </td>
 </tr>
@endforeach

答案 1 :(得分:0)

使口才更好:OrderBook中的变量

protected $appends = ['bill_status']

public function getBillStatusAttribute(){
  if($this->BillPaid == 0){
     return 'unpaid';
  }else {
    return 'paid'; 
  }
}

可见

 $orderBook->bill_status