我想要模型
如果只是电影
更新:有关环境和我的尝试的更多详细信息。
我创建了电影,人物,角色和查找表movie_person_role(又名三支点):
movies
- id
- name
persons
- id
- name
roles
- id
- name [director, script, actor, etc.]
movie_person_role
- id
- foreignId: movie_id
- foreignId: person_id
- foreignId: role_id
方法:
class Movie extends Model
{
public function persons() {
return $this->belongsToMany('App\Person', 'movie_person_role', 'movie_id', 'person_id')->withPivot('role_id');
}
public function roles() {
return $this->belongsToMany('App\Role', 'movie_person_role', 'movie_id', 'role_id')->withPivot('person_id');
}
}
播种机:
$id = DB::table('movies')->insertGetId([
'name' => 'Redbad',
]);
$m = App\Movies::find($id);
$w->persons()->attach([
App\Person::name('Roel Reiné')->first()->id => ['role_id' => App\Role::name('director')->first()->id],
App\Person::name('Alex van Galen')->first()->id => ['role_id' => App\Role::name('writer')->first()->id],
App\Person::name('Jonathan Banks')->first()->id => ['role_id' => App\Role::name('actor')->first()->id],
])
(可能有更清晰的定义种子的方法)
这正确地将例如人
"Roel Reiné"作为角色
"导演"链接到电影
"Redbad"。
剩余内容:如何获取电影中的人物角色列表?
目前的状态是,我可以得到的人(没有他们的角色):
修补匠:
>>> App\Movie::where('title','Redbad')->with('persons')->get()
=> Illuminate\Database\Eloquent\Collection {#3204
all: [
App\Work {#3224
id: 1,
name: "Redbad",
persons: Illuminate\Database\Eloquent\Collection {#3179
all: [
App\Person {#3250
id: 2,
name: "Roel Reiné",
pivot: Illuminate\Database\Eloquent\Relations\Pivot {#3249
movie_id: 1,
person_id: 2,
role_id: 1,
},
},
...
这就给了《红魔》电影中的相关人员一个机会。在pivot对象中,我看到了角色id。
它允许我执行类似$movie的查询-
但是我更喜欢渴望加载角色,就像人一样,这样我就可以很容易地创建一个电影列表,里面有人和他们的角色。例如。
movie A - person K - role director
movie A - person L - role director
movie A - person L - role actor
movie B - person M - role director
movie B - person N - role actor
我最终使用了急切负载枢轴关系包。
作者有一个示例项目。
定义此关系所需的表:
users
id - integer
name - string
...
roles
id - integer
name - string
user_id - integer
movie_id - integer
movies
id - integer
title - string
实施
让我们在用户模型上定义电影方法
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The roles that belong to the user.
*/
public function movies()
{
return $this->belongsToMany('App\Movie', 'roles', 'user_id', 'movie_id')->withPivot('name');
}
}
定义关系的反面
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Movie extends Model
{
/**
* The users that belong to the role.
*/
public function users()
{
return $this->belongsToMany('App\User', 'roles', 'movie_id', 'user_id')->withPivot('name');
}
}
定义关系后,您可以使用movies dynamic属性访问用户的电影:
$user = App\User::find(1);
foreach ($user->movies as $movie) {
// You may access the intermediate table using the pivot attribute on the models:
echo $movie->pivot->name; // retrieve the role name for user #1 in $movie
}
我正在尝试使用透视表实现多对多关系。我的透视表的名称是“post_-tag”,列名是“post_-id”和“tag_-id”。post和tag模型类如下所示。 标签类别如下所示: post_tag表如下: 我正在使用以下代码尝试帖子的标签: 但它抛出了一个错误: 查询连接中的异常。php第729行:SQLSTATE[42S22]:未找到列:1054个未知列的“posts”。where子句中的pos
我目前正在研究构建SaaS构建管理工具。我想知道的是,如果我使用laravel passport作为api令牌,我如何为用户分配角色,例如: SuperAdmin:可以创建项目|可以创建新用户并为用户分配角色。 管理员:可以查看项目。 因为我希望能够根据用户权限隐藏前端上的元素。 例如,如果我使用传统的laravel应用程序,我可以使用委托和刀片指令@role('admin')来显示基于用户权限类
我对拉威尔望远镜的部分有点困惑。 我有一个用户模型和表。 如何为用户分配用户、客户和/或管理员的角色。 我有一个带有vue和laravel api后端的水疗中心。我用https://laravel.com/docs/5.3/passport#consuming-使用javascript创建api 我如何分配哪个用户模型具有哪些作用域? 还是范围与角色不同? 您将如何实现这一点? 提前谢谢!
我试图从我的链接表sfees中获取数据,它由来自学生表的学生id和来自M_费用表的MFEEID组成。我的模型是: M_fees: 现在,我如何检索特定学生的学费(类型和金额)?我在控制器中使用了以下功能: 但它似乎不起作用。 有人能帮我吗?
我对Laravel截断的数据有一个小问题。我将传递给我的后端值,该值可以是表中定义的“百分比”或“金额”,但在表中也可以为null,但每次尝试将其设置为null时,都会出现如下错误: {"discount_currency":["您输入的值无效。"]} 或者当我从处理请求中删除enum时,它会说 所以我传递数据抛出API从我的Vue,这个婴儿车的请求看起来像: 所以如果我删除...我有第二个错误。
我正在构建一个Laravel web应用程序,其中我需要一个动态图像库,我构建了一个后端管理面板,可以在其中添加图像,我成功地将图像添加并保存到数据库中,但我无法编辑或删除它们。 错误是: UrlGenerationException中的ErrorException。php第17行:缺少[Route:galleries.update][URI:backend/galleries/{gallery}