substr 方法的简单 Laravel public 函数 (if/else)
Simple Laravel public function (if/else) for substr method
我的页面中有一个包含 6 列的 table,我需要一些帮助来处理最后一个名为 Notes(Description) 的列。在这里,我使用工具提示来显示上面的描述,因为文本通常很长,而对于列文本,我使用 substr($item->notes, 0, 15) 来仅捕获前 15 个字母。
现在,我要做的是在我的模型中创建一个函数,为我提供下一个行为:如果该项目有描述,则显示 substr($item->notes, 0 , 15), 否则只显示 'N/A'。
这是我的条目:
<td class="text-right" data-toggle="tooltip" data-placement="top" data-html="true" title="{{ $item->notes ? $item->notes : 'N/A' }}">{{ substr($item->notes, 0, 15) }}</td>
您必须按照标题中的操作进行操作:
<td class="text-right" data-toggle="tooltip" data-placement="top" data-html="true" title="{{ $item->notes ? $item->notes : 'N/A' }}">{{ substr($item->notes, 0, 15) ? substr($item->notes, 0, 15) : 'N/A' }}</td>
这来自文档:https://laravel.com/docs/5.5/eloquent-mutators#accessors-and-mutators
To define an accessor, create a getFooAttribute method on your model
where Foo is the "studly" cased name of the column you wish to access.
In this example, we'll define an accessor for the first_name
attribute. The accessor will automatically be called by Eloquent when
attempting to retrieve the value of the first_name attribute:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Get the user's first name.
*
* @param string $value
* @return string
*/
public function getFirstNameAttribute($value)
{
return ucfirst($value);
}
}
在你的情况下会导致类似于
的结果
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
protected $fillable = ['notes']; // just for this demo
public function getNotesAttribute($value)
{
if (!empty($value)) {
return substr($value, 0, 15);
} else{
return 'N/A';
}
}
}
如果您只想在视图中执行 $item->notes 而不必每次都编写条件,则必须在模型中使用访问器。
在这里,我假设 "notes" 是您的剥夺 属性 的名称。修改它以满足您的需要。
public function getNotesAttribute($value) {
if (!empty($value)) {
return substr($value, 0, 15);
} else{
return 'N/A';
}
}
使用laravel的辅助函数,在这种情况下,str_limit()就可以为您服务,例如:
<td class="text-right" data-toggle="tooltip" data-placement="top" data-html="true" title="{{ $item->notes ? $item->notes : 'N/A' }}">
{{ str_limit($item->notes, 15) }}
</td>
另外laravel还有很多有用的功能,你可以在文档中看到它们:
https://laravel.com/docs/5.1/helpers#method-str-limit
https://laravel.com/docs/5.1/helpers
现在,如果您需要创建自己的函数,请在此处说明如何操作:
我的页面中有一个包含 6 列的 table,我需要一些帮助来处理最后一个名为 Notes(Description) 的列。在这里,我使用工具提示来显示上面的描述,因为文本通常很长,而对于列文本,我使用 substr($item->notes, 0, 15) 来仅捕获前 15 个字母。
现在,我要做的是在我的模型中创建一个函数,为我提供下一个行为:如果该项目有描述,则显示 substr($item->notes, 0 , 15), 否则只显示 'N/A'。
这是我的条目:
<td class="text-right" data-toggle="tooltip" data-placement="top" data-html="true" title="{{ $item->notes ? $item->notes : 'N/A' }}">{{ substr($item->notes, 0, 15) }}</td>
您必须按照标题中的操作进行操作:
<td class="text-right" data-toggle="tooltip" data-placement="top" data-html="true" title="{{ $item->notes ? $item->notes : 'N/A' }}">{{ substr($item->notes, 0, 15) ? substr($item->notes, 0, 15) : 'N/A' }}</td>
这来自文档:https://laravel.com/docs/5.5/eloquent-mutators#accessors-and-mutators
To define an accessor, create a getFooAttribute method on your model where Foo is the "studly" cased name of the column you wish to access. In this example, we'll define an accessor for the first_name attribute. The accessor will automatically be called by Eloquent when attempting to retrieve the value of the first_name attribute:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Get the user's first name.
*
* @param string $value
* @return string
*/
public function getFirstNameAttribute($value)
{
return ucfirst($value);
}
}
在你的情况下会导致类似于
的结果<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
protected $fillable = ['notes']; // just for this demo
public function getNotesAttribute($value)
{
if (!empty($value)) {
return substr($value, 0, 15);
} else{
return 'N/A';
}
}
}
如果您只想在视图中执行 $item->notes 而不必每次都编写条件,则必须在模型中使用访问器。
在这里,我假设 "notes" 是您的剥夺 属性 的名称。修改它以满足您的需要。
public function getNotesAttribute($value) {
if (!empty($value)) {
return substr($value, 0, 15);
} else{
return 'N/A';
}
}
使用laravel的辅助函数,在这种情况下,str_limit()就可以为您服务,例如:
<td class="text-right" data-toggle="tooltip" data-placement="top" data-html="true" title="{{ $item->notes ? $item->notes : 'N/A' }}">
{{ str_limit($item->notes, 15) }}
</td>
另外laravel还有很多有用的功能,你可以在文档中看到它们:
https://laravel.com/docs/5.1/helpers#method-str-limit
https://laravel.com/docs/5.1/helpers
现在,如果您需要创建自己的函数,请在此处说明如何操作: