laravel view code example

Example 1: laravel load view in variable

$html = view('users.edit', compact('user'))->render();

Example 2: laravel return view in web.php

Route::get("/page", function(){
   return View::make("dir.page");
});

Example 3: pass parameter to view laravel

Route::get('/', function () {
    return view('greeting', ['name' => 'James']);
});

Example 4: laravel view-model

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

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

Example 5: 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>

Example 6: laravel view-model

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

Tags:

Php Example