【问题标题】:print readable message for tiny int value打印微小 int 值的可读消息
【发布时间】:2018-08-06 10:07:21
【问题描述】:

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

此外,如果 PAID 列数据为 PAID,则将其着色为绿色,如果 UNPAID,则将其着色为蓝色。我该怎么做?

用于获取数据的控制器代码块

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

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    在刀片模板中使用 if 条件并应用 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
    

    【讨论】:

      【解决方案2】:

      制作 Eloquent:OrderBook 中的变异器

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

      在视图中

       $orderBook->bill_status 
      

      【讨论】:

      • 如何在视图页面中访问它。请帮忙。
      • 在你的视图中写$orderBook-&gt;bill_paid
      • 如何作为表数据访问,&lt;td&gt; {{$data['BillPaid']}} &lt;/td&gt;
      • 在您的可填写属性之后写下 protected $appends =['bill_paid']
      • 我已经按照您的建议编辑了我的代码,但仍然出现错误。请查看问题我已将代码编辑为ErrorException (E_NOTICE) Undefined property: App\OrderedBook::$BillPaid
      猜你喜欢
      • 2022-10-14
      • 2012-02-06
      • 2018-01-13
      • 1970-01-01
      • 2012-03-03
      • 1970-01-01
      • 2022-12-10
      • 2018-11-23
      • 2016-11-18
      相关资源
      最近更新 更多