Answers for "laravel view-model"

PHP
0

laravel view-model

composer require spatie/laravel-view-models
Posted by: Guest on March-07-2021
0

laravel view-model

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

    // …
    
    public function ignoredMethod() { /* … */ }
}
Posted by: Guest on March-07-2021
0

laravel view-model

php artisan make:view-model HomepageViewModel
Posted by: Guest on March-07-2021
0

laravel view-model

class PostViewModel extends ViewModel
{
    public $indexUrl = null;

    public function __construct(User $user, Post $post = null)
    {
        $this->user = $user;
        $this->post = $post;
        
        $this->indexUrl = action([PostsController::class, 'index']); 
    }
    
    public function post(): Post
    {
        return $this->post ?? new Post();
    }
    
    public function categories(): Collection
    {
        return Category::canBeUsedBy($this->user)->get();
    }
}
Posted by: Guest on March-07-2021
0

laravel view-model

class PostViewModel extends ViewModel
{
    public function formatDate(Carbon $date): string
    {
        return $date->format('Y-m-d');
    }
}
Posted by: Guest on March-07-2021
0

laravel view-model

class PostsController
{
    public function update(Request $request, Post $post)
    {
        // …
        
        return new PostViewModel($post);
    }
}
Posted by: Guest on March-07-2021
0

laravel view-model

php artisan make:view-model "Blog/PostsViewModel"
Posted by: Guest on March-07-2021
0

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>
Posted by: Guest on March-07-2021
0

laravel view-model

class PostsController
{
    public function create()
    {
        $viewModel = new PostViewModel(
            current_user()
        );
        
        return view('blog.form', $viewModel);
    }
    
    public function edit(Post $post)
    {
        $viewModel = new PostViewModel(
            current_user(), 
            $post
        );
    
        return view('blog.form', $viewModel);
    }
}
Posted by: Guest on March-07-2021
0

laravel view-model

{{ $formatDate($post->created_at) }}
Posted by: Guest on March-07-2021

Browse Popular Code Answers by Language