“Model tampilan Laravel” Kode Jawaban

Model tampilan Laravel

composer require spatie/laravel-view-models
Handsome Hippopotamus

Model tampilan Laravel

<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

Model tampilan Laravel

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

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

Model tampilan Laravel

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

Model tampilan Laravel

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

Model tampilan Laravel

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

Model tampilan Laravel

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

Model tampilan Laravel

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

Model tampilan Laravel

php artisan make:view-model HomepageViewModel
Handsome Hippopotamus

Jawaban yang mirip dengan “Model tampilan Laravel”

Pertanyaan yang mirip dengan “Model tampilan Laravel”

Lebih banyak jawaban terkait untuk “Model tampilan Laravel” di PHP

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya