您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
mass-assignment批量赋值异常及期间的注意事项(基于laravel-breeze的inertia的数据列表的实现)
发布时间:2024-10-30 21:03:03编辑:雪饮阅读()
-
基于laravel的Inertia数据列表实现起来也和传统的纯view是有区别的。
我们这里以上次创建的project列表的展示为例。
首先我们封装一个组件用于展示单个project。
Y:\root\example-app\resources\js\Pages\Project\Partials\Project.vue
<script setup>
defineProps(['project']);
</script>
<template>
<div class="p-6 flex space-x-2">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-gray-600 -scale-x-100" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" />
</svg>
<div class="flex-1">
<div class="flex justify-between items-center">
<div>
<span class="text-gray-800">{{ project.name }}</span>
</div>
</div>
</div>
</div>
</template>
然后我们用project列表模板来调用这个project组件
Y:\root\example-app\resources\js\Pages\Project\Index.vue
<script setup>
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout.vue';
import Project from './Partials/Project.vue';
import { Head } from '@inertiajs/vue3';
defineProps(['projects']);
</script>
<template>
<Head title="Projects" />
<AuthenticatedLayout>
<div class="mt-6 bg-white shadow-sm rounded-lg divide-y">
<Project
v-for="project in projects"
:key="project.id"
:project="project"
/>
</div>
</AuthenticatedLayout>
</template>
然后控制器中查询当前用户的project列表
Y:\root\example-app\app\Http\Controllers\ProjectController.php中的index方法实现如
public function index(Request $request):Response
{
return Inertia::render('Project/Index', [
'projects' => Project::where("uid",$request->user()->id)->get(),
]);
}
这里需要注意下这个response需要使用这个来命名空间来引入
use Inertia\Response;
然后给加上路由
Y:\root\example-app\routes\web.php中的auth组中新增条目如
Route::get('/project/index', [ProjectController::class, 'index'])->name('project.index');
最后登录到dashboard中然后访问路径如
http://localhost/project/index
就可以看到我们之前创建的project了。
本篇参考https://bootcamp.laravel.com/inertia/installation
本期词汇
Inertia 惰性 惯性
laravel 一个免费的开源 PHP Web 框架
Chirp 轻松愉快地说
关键字词:laravel,inertia
相关文章
- blade视图模板的扩展与复用(laravel的inertia)
- 调试请求数据并创建project(为基于sail的laravel项目
- project-create相关的路由及controller定义(laravel-b
- 前端form组件的介绍(laravel11中的breeze的form实现)
- vite刷新慢的解决办法(laravel的sail部署中mysql容器
- 创建project相关的视图与数据库设计(laravel入门套件B
- MVC介绍及创建数据模型(laravel在命令行中创建模型)
- 连接数据库,以及users表相关的数据结构(了解laravel的Br
- 开启注册登陆功能(解决laravel安装Breeze后没有注册、
- laravel环境变量的原理、优势及项目配置