Have you ever made a controller where you had to do a lot of work to prepare variables to be passed to a view? You can move that kind of work to a so called view model. In essence, view models are simple classes that take some data, and transform it into something usable for the view.
You'll find a more detailed explanation and some good examples in this blogpost on Stitcher.io.
Laravel's native view composers are not the same as the view models provided by this package. To learn more about the differences head over to this blogpost on Stitcher.io.
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You can install the package via composer:
composer require spatie/laravel-view-models
A view model is a class where you can put some complex logic for your views. This will make your controllers a bit lighter. You can create a view model by extending the provided Spatie\ViewModels\ViewModel
.
class PostViewModel extends ViewModel
{
public $user;
public $post;
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();
}
}
Then you can use the view modal class in your controller like this:
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);
}
}
In a view you can do this:
<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>
All public methods and properties in a view model are automatically exposed to the view. If you don't want a specific method to be available in your view, you can ignore it.
class PostViewModel extends ViewModel
{
protected $ignore = ['ignoredMethod'];
// …
public function ignoredMethod() { /* … */ }
}
All PHP's built in magic methods are ignored automatically.
It's possible to directly return a view model from a controller.By default, a JSON response with the data is returned.
class PostsController
{
public function update(Request $request, Post $post)
{
// …
return new PostViewModel($post);
}
}
This approach can be useful when working with AJAX submitted forms.
It's also possible to return a view directly:
class PostsController
{
public function update(Request $request, Post $post)
{
// …
return (new PostViewModel($post))->view('post.form');
}
}
Note that when the Content-Type
header of the request is set to JSON,this approach will also return JSON data instead of a rendered view.
View models can expose functions which require extra parameters.
class PostViewModel extends ViewModel
{
public function formatDate(Carbon $date): string
{
return $date->format('Y-m-d');
}
}
You can use these functions in the view like so:
{{ $formatDate($post->created_at) }}
The package included an artisan command to create a new view model.
php artisan make:view-model HomepageViewModel
This view model will have the App\ViewModels
namespace and will be saved in app/ViewModels
.
or into a custom namespace, say, App\Blog
php artisan make:view-model "Blog/PostsViewModel"
This view model will have the App\Blog\ViewModels
namespace and will be saved in app/Blog/ViewModels
.
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
laravel-admin 是一个可以快速帮你构建后台管理的工具,它提供的页面组件和表单元素等功能,能帮助你使用很少的代码就实现功能完善的后台管理功能。 官方开发文档:Laravel admin | 表格数据源 最近遇到一个项目需求,需要将union的数据进行列表化展示在后台; 后台基于Laravel-Admin扩展来实现的,由于model-grid的数据是依赖Eloquent model来查询获
laravel-route-notes laravel框架扩展,原生注解生成路由 优点是直接生成路由文件,不在运行中解析路由,提升效率 使用环境 [PHP] >= 8.0 [Laravel] >= 9.0 如何安装 直接使用composer进行安装: composer require --dev lovefc/laravel-route-notes 命令使用 php artisan notes:
需求 很常见的功能 就是我们登陆后台之后 有一个用户列表 每个用户有一个一个快速登陆的按钮,点击之后直接跳转到用户自己的后台 项目的设计 因为在一套laravel-admin上去作多用户后台是需要一个付费的composer组件(花钱是不可能的!),并且时间关系无法自己套模板去写 所以一步到位,直接新建一个laravel项目,然后装上laravel-admin 关于总后台的登录处理 很明显我在总后台
错误:Base table or view not found: 1146 Table ‘hyzt.brands’ doesn’t exist (SQL: select count(*) as aggregate from brands) 我第一次使用laravel-admin插件。感觉这东西有一点过于牛逼,但是这使用上的无奈也是有的。先说一下这个问题,网上我没有找到正确的答案,先说一下网上都用的
视图是可视化的表。 本章讲解如何创建、更新和删除视图。 SQL CREATE VIEW 语句 在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表。 视图包含行和列,就像一个真实的表。视图中的字段就是来自一个或多个数据库中的真实的表中的字段。 您可以向视图添加 SQL 函数、WHERE 以及 JOIN 语句,也可以呈现数据,就像这些数据来自于某个单一的表一样。 SQL CREATE VIE
描述 View 是最基础的组件,默认为 Flexbox 布局,并且可以任意嵌套。 不论在什么容器中,View 都直接对应一个容器的原生视图,比如在 Web 容器中是使用 div 实现的。 支持任意自定义属性的透传。 安装 $ npm install rax-view --save 属性 属性 类型 默认值 必填 描述 支持 onLongPress Function - ✘ 当组件被长按时触发的事件
视图用于反映“数据模型的外观”。 它们向用户表示模型的数据。 它们提供了向用户呈现模型数据的想法。 它处理用户输入事件,绑定事件和方法,呈现模型或集合以及与用户交互。 下表列出了可用于操作BackboneJS-Views 。 S.No. 方法和描述 1 extend 它扩展了Backbone.View类以创建自定义视图类。 2 initialize 它使用new关键字实例化视图。 3 el 它定义
View::make('path/to/view'); View::make('foo/bar')->with('key', 'value'); View::make('foo/bar')->withKey('value'); View::make('foo/bar', array('key' => 'value')); View::exists('foo/bar'); // 跨视图共享变量 Vi
本文向大家介绍wpf View,包括了wpf View的使用技巧和注意事项,需要的朋友参考一下 示例 视图是M V VM中的“ V” 。这是您的用户界面。您可以使用Visual Studio拖放式设计器,但是大多数开发人员最终都会对原始XAML进行编码,这与编写HTML相似。 这是允许编辑Customer模型的简单视图的XAML 。与其创建一个新视图,不如将其粘贴到WPF项目的MainWindow