Model tampilan Laravel
composer require spatie/laravel-view-models
Handsome Hippopotamus
composer require spatie/laravel-view-models
<input type="text" value="{{ $post->title }}" />
<input type="text" value="{{ $post->body }}" />
<select>
@foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
<a href="{{ $indexUrl }}">Back</a>
class PostViewModel extends ViewModel
{
protected $ignore = ['ignoredMethod'];
// …
public function ignoredMethod() { /* … */ }
}
class PostsController
{
public function update(Request $request, Post $post)
{
// …
return (new PostViewModel($post))->view('post.form');
}
}
{{ $formatDate($post->created_at) }}
php artisan make:view-model "Blog/PostsViewModel"
class PostsController
{
public function update(Request $request, Post $post)
{
// …
return new PostViewModel($post);
}
}
class PostViewModel extends ViewModel
{
public function formatDate(Carbon $date): string
{
return $date->format('Y-m-d');
}
}
php artisan make:view-model HomepageViewModel