简体   繁体   English

Laravel 5.2 IF语句语法,基于值更改名称

[英]Laravel 5.2 IF Statement Syntax, Change Name based on Value

So, I'm going to moving on from PHP native to Laravel framework. 因此,我将从PHP本机转向Laravel框架。 See the code below, it's must be error, but this would tell you what I want. 请参阅下面的代码,它一定是错误的,但这会告诉您我想要什么。 I'm confused to the IF statement in Laravel based my database. 我对基于数据库的Laravel中的IF语句感到困惑。

<td>
        {{ 
        @if($book->status==1)
            echo 'Available';
        @elseif($book->status==2)
            echo 'Lost';
        @else
            echo 'Not Available';
        @endif
        }}
</td>

Is that possible to print out the name based on database, example: if the value is 1, then I'll print out 'Lost' on my website? 是否可以根据数据库打印出名称,例如:如果该值为1,那么我将在我的网站上打印“丢失”? Thank you. 谢谢。

<td>
    @if ($book->status == 1)
        Available
    @elseif ($book->status == 2)
        Lost
    @else
        Not Available
    @endif
</td>

A better place for this would be in the book model itself: 一个更好的地方应该在书本模型本身中:

public function getAvailabilityAttribute()
{
    if ($this->status == 1) return 'Available';

    if ($this->status == 2) return 'Lost';

    return 'Not Available';
}

Then you could use it directly in your view: 然后,您可以在视图中直接使用它:

<td>{{ $book->availability }}</td>

Use like this: 像这样使用:

<td>       
    @if($book->status==1)
        {{ 'Available' }}
    @elseif($book->status==2)
        {{ 'Lost' }}
    @else
        {{ 'Not Available' }}
    @endif
</td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM