You shouldn't put a lot of complicated things inside your views.
Inside your model, you can do the following:
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['action'];
/**
*
*
* @var string
*/
public function getActionAttribute()
{
switch ($this->attributes['status'])
{
case 1:
return '<a href="/post/edit/' . $this->attributes['id'] . '" class="btn btn-warning"><i class="icon fa-pencil-square-o" aria-hidden="true"></i> Edit</a>';
case 2:
return '<a href="#"></a>';
case 3:
return '<a href="#"></a>';
case 4:
return '<a href="#"></a>';
}
}
And then in your view:
@foreach($posts as $post)
<tr>
<td>{{ $post->name }}</td>
<td>{{ str_limit($post->content, 100) }}</td>
<td>{{ $post->status }}</td>
<td>{{ $post->action }}</td>
</tr>
@endforeach
Well, that would work BUT the same principle applies to your Model, you shouldn't put any view related actions.
You have two options:
- Create a class that accepts the
status parameter and returns the string containing HTML codes or a more defined way but a little complex:
- Use a Transformer.
It is your code. Your choice. There are no law that states what you should and what you should not do.