xxxxxxxxxx
//In Laravel, view() is a function that is used to return a view. A view is a file that contains the HTML and PHP code that is rendered in the user's browser.
//**You can use the view() function to return a view from a route or controller.**
//For example:
//Route:
Route::get('/welcome', function () {
return view('welcome');
});
//In this example, the view() function is used to return the welcome view. This view is located in the resources/views directory of your Laravel project. When a user accesses the /welcome route, the contents of the welcome view will be rendered in their browser.
xxxxxxxxxx
<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>
xxxxxxxxxx
class PostViewModel extends ViewModel
{
protected $ignore = ['ignoredMethod'];
// …
public function ignoredMethod() { /* … */ }
}
xxxxxxxxxx
class PostsController
{
public function update(Request $request, Post $post)
{
// …
return new PostViewModel($post);
}
}
xxxxxxxxxx
class PostsController
{
public function update(Request $request, Post $post)
{
// …
return (new PostViewModel($post))->view('post.form');
}
}
xxxxxxxxxx
class PostViewModel extends ViewModel
{
public function formatDate(Carbon $date): string
{
return $date->format('Y-m-d');
}
}