Laravel命名约定中的多态关系
我打算在Laravel中创建多态关系,但我的表太旧了,它的命名约定不符合Laravel。我可以这样做吗?如何做?当然,您可以直接设置表名和FK列名。Laravel命名约定中的多态关系,laravel,polymorphism,polymorphic-associations,Laravel,Polymorphism,Polymorphic Associations,我打算在Laravel中创建多态关系,但我的表太旧了,它的命名约定不符合Laravel。我可以这样做吗?如何做?当然,您可以直接设置表名和FK列名。 查看不动产,如有必要,在Laravel或 如果你有 posts id - integer title - string body - text comments id - integer post_id - integer body - text likes id - integer
查看不动产,如有必要,在Laravel或 如果你有
posts
id - integer
title - string
body - text
comments
id - integer
post_id - integer
body - text
likes
id - integer
likeable_id - integer
likeable_type - string
那么您的代码将被删除
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Like extends Model
{
/**
* Get all of the owning likeable models.
*/
public function likeable()
{
return $this->morphTo('likeable', 'likeable_type', 'likeable_id');
}
}
class Post extends Model
{
/**
* Get all of the post's likes.
*/
public function likes()
{
return $this->morphMany('App\Like', 'likeable', 'likeable_type', 'likeable_id');
}
}
class Comment extends Model
{
/**
* Get all of the comment's likes.
*/
public function likes()
{
return $this->morphMany('App\Like', 'likeable', 'likeable_type', 'likeable_id');
}
}
最好添加有关当前数据库架构的更多信息。您可以添加代码吗?