php artisan make:view viewName
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);
}
}
class PostsController
{
public function update(Request $request, Post $post)
{
// …
return (new PostViewModel($post))->view('post.form');
}
}
class PostViewModel extends ViewModel
{
public function formatDate(Carbon $date): string
{
return $date->format('Y-m-d');
}
}
{{ $formatDate($post->created_at) }}
php artisan make:view-model HomepageViewModel
php artisan make:view-model "Blog/PostsViewModel"
Laravel_View
Laravel_View_2
@php
$user_id = Auth::user()->id;
@endphp