默认情况下, Laravel 使用完全限定类名存储关联模型类型。在上面的一对多示例中, 因为 Comment 可能从属于一个 Post 或一个 Video,默认的 commentable_type 就将分别是 App\Post 或App\Video。不过,你可能希望数据库与应用的内部结构解耦。在这种情况下,可以定义一个 「morph 映射」 来通知 Eloquent 使用自定义...
You are able to use any of the Laravel query builder's methods on the relationship, so be sure to explore the query builder documentation to learn about all of the methods that are available to you.Chaining orWhere Clauses After Relationships...
When accessing Eloquent relationships as properties, the relationship data is "lazy loaded". This means the relationship data is not actually loaded until you first access the property. However, Eloquent can "eager load" relationships at the time you query the parent model. Eager loading alleviates...
WARNING You're browsing the documentation for an old version of Laravel. Consider upgrading your project to Laravel 12.x. Eloquent: RelationshipsIntroductionDatabase tables are often related to one another. For example, a blog post may have many comments, or an order could be related to the ...
默认情况下,Laravel 会使用「包含命名空间的类名」作为多态表的类型区分,例如,Post 和Comment 可以被 Like,likable_type 的值会是 App\Post 或App\Comment。然而,你也可以选择自定义自己的「多态对照表」:use Illuminate\Database\Eloquent\Relations\Relation; Relation::morphMap([ App\Post::class, App\Comment...
默认,Laravel 会使用完全限定类名作为关联模型保存在多态模型上的类型字段值。比如,在上面的例子中,Comment 属于Post 或者Video,那么 commentable_type的默认值对应地就是 App\Post 和App\Video。但是,您可能希望将数据库与程序内部结构解耦。那样的话,你可以定义一个「多态映射表」来指示 Eloquent 使用每个模型自定义...
Many to many relationship is a little bit complicated than one to one and one to many relationships. An example of such a relationship is a user with may have multiple roles, where the role are also connected with multiple users. many to many relationship in laravel 6, laravel 7, laravel...
This tutorial explains Laravel Database handling, Migrations, Seeding, Raw SQL Queries, Eloquent Models, Eloquent Relationships, Artisan and Tinker: In the previous tutorial ofLaravel Tutorial Series, we learned about the architecture, installation, and components of the Laravel Framework. We have seen...
默认,Laravel 会使用完全限定类名作为关联模型保存在多态模型上的类型字段值。比如,在上面的例子中,Comment 属于Post 或者Video,那么 commentable_type 的默认值对应地就是 App\Post 和App\Video。但是,您可能希望将数据库与程序内部结构解耦。那样的话,你可以定义一个「多态映射表」来指示 Eloquent 使用每个模型自...
默认情况下,Laravel 将根据模型的类名确定与给定模型关联的关系; 你也可以通过将关系名称作为 whereBelongsTo 方法的第二个参数来手动指定关系名称:$posts = Post::whereBelongsTo($user, 'author')->get(); 一对多检索有时一个模型可能有许多相关模型,如果想检索关系的「最新」或「最旧」相关模型。例如,一...