“Laravel View-Model” Réponses codées

Laravel View-Model

composer require spatie/laravel-view-models
Handsome Hippopotamus

Laravel View-Model

<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>
Handsome Hippopotamus

Laravel View-Model

class PostViewModel extends ViewModel
{
    protected $ignore = ['ignoredMethod'];

    // …
    
    public function ignoredMethod() { /* … */ }
}
Handsome Hippopotamus

Laravel View-Model

class PostsController
{
    public function update(Request $request, Post $post)
    {
        // …
        
        return (new PostViewModel($post))->view('post.form');
    }
}
Handsome Hippopotamus

Laravel View-Model

{{ $formatDate($post->created_at) }}
Handsome Hippopotamus

Laravel View-Model

php artisan make:view-model "Blog/PostsViewModel"
Handsome Hippopotamus

Laravel View-Model

class PostsController
{
    public function update(Request $request, Post $post)
    {
        // …
        
        return new PostViewModel($post);
    }
}
Handsome Hippopotamus

Laravel View-Model

class PostViewModel extends ViewModel
{
    public function formatDate(Carbon $date): string
    {
        return $date->format('Y-m-d');
    }
}
Handsome Hippopotamus

Laravel View-Model

php artisan make:view-model HomepageViewModel
Handsome Hippopotamus

Réponses similaires à “Laravel View-Model”

Questions similaires à “Laravel View-Model”

Plus de réponses similaires à “Laravel View-Model” dans PHP

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code