Explain Eloquent morphMany parameters
up vote
0
down vote
favorite
Hi guys Im new to laravel, can someone explain to me the parameters of morphMany
$this->morphMany(Photo::class, 'imageable');
php laravel eloquent
add a comment |
up vote
0
down vote
favorite
Hi guys Im new to laravel, can someone explain to me the parameters of morphMany
$this->morphMany(Photo::class, 'imageable');
php laravel eloquent
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Hi guys Im new to laravel, can someone explain to me the parameters of morphMany
$this->morphMany(Photo::class, 'imageable');
php laravel eloquent
Hi guys Im new to laravel, can someone explain to me the parameters of morphMany
$this->morphMany(Photo::class, 'imageable');
php laravel eloquent
php laravel eloquent
edited Nov 10 at 13:18
HCK
2,9351829
2,9351829
asked Nov 10 at 3:15
silent_terius
31
31
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
The MorphMany relationship has the following function signature:
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
//
Where:
$related(required): refers to the related model. e.g:User::class$name(required: the name of the polymorphic relation, likecommentable$type(optional): customize therelation_typefield to look up when doing a query.$id(optional): customize therelation_idfield to look up when doing a query.$localKey(optional): customize the local key (by defaultid) to search when doing a query.
So -using the example shown in the Laravel docuemntation- if you want to use a different table structure for the comments table from this:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
to this:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
foo - integer // the index to look
bar - string // the type to match
You'd need to define your relationships like this:
Post.php
public function comments()
return $this->morphMany(Comment::class, 'commentable', 'foo', 'bar');
Video.php
public function comments()
return $this->morphMany(Comment::class, 'commentable', 'foo', 'bar');
Comment.php
public function commentable()
return $this->morphTo('commentable');
Check this other answer.
add a comment |
up vote
0
down vote
Polymorphic Relations
Table Structure
Polymorphic relations allow a model to belong to more than one other model on a single association. For example, imagine users of your application can "comment" on both posts and videos. Using polymorphic relationships, you can use a single comments table for both of these scenarios. First, let's examine the table structure required to build this relationship:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
Two important columns to note are the commentable_id and commentable_type columns on the comments table. The commentable_id column will contain the ID value of the post or video, while the commentable_type column will contain the class name of the owning model. The commentable_type column is how the ORM determines which "type" of owning model to return when accessing the commentable relation.
Model Structure
Next, let's examine the model definitions needed to build this relationship:
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Comment extends Model
/**
* Get all of the owning commentable models.
*/
public function commentable()
return $this->morphTo();
class Post extends Model
/**
* Get all of the post's comments.
*/
public function comments()
return $this->morphMany('AppComment', 'commentable');
class Video extends Model
/**
* Get all of the video's comments.
*/
public function comments()
return $this->morphMany('AppComment', 'commentable');
Retrieving Polymorphic Relations
Once your database table and models are defined, you may access the relationships via your models. For example, to access all of the comments for a post, we can use the comments dynamic property:
$post = AppPost::find(1);
foreach ($post->comments as $comment)
//
You may also retrieve the owner of a polymorphic relation from the polymorphic model by accessing the name of the method that performs the call to morphTo. In our case, that is the commentable method on the Comment model. So, we will access that method as a dynamic property:
$comment = AppComment::find(1);
$commentable = $comment->commentable;
The commentable relation on the Comment model will return either a Post or Video instance, depending on which type of model owns the comment.
See this link: polymorphic-relations:
You can entry like that:
+---------+----------------+-------------------+
| user_id | commentable_id | commentable_type |
+---------+----------------+-------------------+
| 1 | 1 | AppPost |
| 1 | 2 | AppPost |
| 1 | 3 | AppPost |
| 1 | 1 | AppVideo |
| 1 | 2 | AppVideo |
| 1 | 3 | AppVideo |
+---------+----------------+-------------------+
thank you for replying @Keval Mangukiya but what I need is the parameters inside $this->morphMany() function the documentation does not elaborate the parameters of that function
– silent_terius
Nov 10 at 4:24
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
The MorphMany relationship has the following function signature:
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
//
Where:
$related(required): refers to the related model. e.g:User::class$name(required: the name of the polymorphic relation, likecommentable$type(optional): customize therelation_typefield to look up when doing a query.$id(optional): customize therelation_idfield to look up when doing a query.$localKey(optional): customize the local key (by defaultid) to search when doing a query.
So -using the example shown in the Laravel docuemntation- if you want to use a different table structure for the comments table from this:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
to this:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
foo - integer // the index to look
bar - string // the type to match
You'd need to define your relationships like this:
Post.php
public function comments()
return $this->morphMany(Comment::class, 'commentable', 'foo', 'bar');
Video.php
public function comments()
return $this->morphMany(Comment::class, 'commentable', 'foo', 'bar');
Comment.php
public function commentable()
return $this->morphTo('commentable');
Check this other answer.
add a comment |
up vote
0
down vote
accepted
The MorphMany relationship has the following function signature:
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
//
Where:
$related(required): refers to the related model. e.g:User::class$name(required: the name of the polymorphic relation, likecommentable$type(optional): customize therelation_typefield to look up when doing a query.$id(optional): customize therelation_idfield to look up when doing a query.$localKey(optional): customize the local key (by defaultid) to search when doing a query.
So -using the example shown in the Laravel docuemntation- if you want to use a different table structure for the comments table from this:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
to this:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
foo - integer // the index to look
bar - string // the type to match
You'd need to define your relationships like this:
Post.php
public function comments()
return $this->morphMany(Comment::class, 'commentable', 'foo', 'bar');
Video.php
public function comments()
return $this->morphMany(Comment::class, 'commentable', 'foo', 'bar');
Comment.php
public function commentable()
return $this->morphTo('commentable');
Check this other answer.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The MorphMany relationship has the following function signature:
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
//
Where:
$related(required): refers to the related model. e.g:User::class$name(required: the name of the polymorphic relation, likecommentable$type(optional): customize therelation_typefield to look up when doing a query.$id(optional): customize therelation_idfield to look up when doing a query.$localKey(optional): customize the local key (by defaultid) to search when doing a query.
So -using the example shown in the Laravel docuemntation- if you want to use a different table structure for the comments table from this:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
to this:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
foo - integer // the index to look
bar - string // the type to match
You'd need to define your relationships like this:
Post.php
public function comments()
return $this->morphMany(Comment::class, 'commentable', 'foo', 'bar');
Video.php
public function comments()
return $this->morphMany(Comment::class, 'commentable', 'foo', 'bar');
Comment.php
public function commentable()
return $this->morphTo('commentable');
Check this other answer.
The MorphMany relationship has the following function signature:
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
//
Where:
$related(required): refers to the related model. e.g:User::class$name(required: the name of the polymorphic relation, likecommentable$type(optional): customize therelation_typefield to look up when doing a query.$id(optional): customize therelation_idfield to look up when doing a query.$localKey(optional): customize the local key (by defaultid) to search when doing a query.
So -using the example shown in the Laravel docuemntation- if you want to use a different table structure for the comments table from this:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
to this:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
foo - integer // the index to look
bar - string // the type to match
You'd need to define your relationships like this:
Post.php
public function comments()
return $this->morphMany(Comment::class, 'commentable', 'foo', 'bar');
Video.php
public function comments()
return $this->morphMany(Comment::class, 'commentable', 'foo', 'bar');
Comment.php
public function commentable()
return $this->morphTo('commentable');
Check this other answer.
answered Nov 10 at 4:39
HCK
2,9351829
2,9351829
add a comment |
add a comment |
up vote
0
down vote
Polymorphic Relations
Table Structure
Polymorphic relations allow a model to belong to more than one other model on a single association. For example, imagine users of your application can "comment" on both posts and videos. Using polymorphic relationships, you can use a single comments table for both of these scenarios. First, let's examine the table structure required to build this relationship:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
Two important columns to note are the commentable_id and commentable_type columns on the comments table. The commentable_id column will contain the ID value of the post or video, while the commentable_type column will contain the class name of the owning model. The commentable_type column is how the ORM determines which "type" of owning model to return when accessing the commentable relation.
Model Structure
Next, let's examine the model definitions needed to build this relationship:
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Comment extends Model
/**
* Get all of the owning commentable models.
*/
public function commentable()
return $this->morphTo();
class Post extends Model
/**
* Get all of the post's comments.
*/
public function comments()
return $this->morphMany('AppComment', 'commentable');
class Video extends Model
/**
* Get all of the video's comments.
*/
public function comments()
return $this->morphMany('AppComment', 'commentable');
Retrieving Polymorphic Relations
Once your database table and models are defined, you may access the relationships via your models. For example, to access all of the comments for a post, we can use the comments dynamic property:
$post = AppPost::find(1);
foreach ($post->comments as $comment)
//
You may also retrieve the owner of a polymorphic relation from the polymorphic model by accessing the name of the method that performs the call to morphTo. In our case, that is the commentable method on the Comment model. So, we will access that method as a dynamic property:
$comment = AppComment::find(1);
$commentable = $comment->commentable;
The commentable relation on the Comment model will return either a Post or Video instance, depending on which type of model owns the comment.
See this link: polymorphic-relations:
You can entry like that:
+---------+----------------+-------------------+
| user_id | commentable_id | commentable_type |
+---------+----------------+-------------------+
| 1 | 1 | AppPost |
| 1 | 2 | AppPost |
| 1 | 3 | AppPost |
| 1 | 1 | AppVideo |
| 1 | 2 | AppVideo |
| 1 | 3 | AppVideo |
+---------+----------------+-------------------+
thank you for replying @Keval Mangukiya but what I need is the parameters inside $this->morphMany() function the documentation does not elaborate the parameters of that function
– silent_terius
Nov 10 at 4:24
add a comment |
up vote
0
down vote
Polymorphic Relations
Table Structure
Polymorphic relations allow a model to belong to more than one other model on a single association. For example, imagine users of your application can "comment" on both posts and videos. Using polymorphic relationships, you can use a single comments table for both of these scenarios. First, let's examine the table structure required to build this relationship:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
Two important columns to note are the commentable_id and commentable_type columns on the comments table. The commentable_id column will contain the ID value of the post or video, while the commentable_type column will contain the class name of the owning model. The commentable_type column is how the ORM determines which "type" of owning model to return when accessing the commentable relation.
Model Structure
Next, let's examine the model definitions needed to build this relationship:
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Comment extends Model
/**
* Get all of the owning commentable models.
*/
public function commentable()
return $this->morphTo();
class Post extends Model
/**
* Get all of the post's comments.
*/
public function comments()
return $this->morphMany('AppComment', 'commentable');
class Video extends Model
/**
* Get all of the video's comments.
*/
public function comments()
return $this->morphMany('AppComment', 'commentable');
Retrieving Polymorphic Relations
Once your database table and models are defined, you may access the relationships via your models. For example, to access all of the comments for a post, we can use the comments dynamic property:
$post = AppPost::find(1);
foreach ($post->comments as $comment)
//
You may also retrieve the owner of a polymorphic relation from the polymorphic model by accessing the name of the method that performs the call to morphTo. In our case, that is the commentable method on the Comment model. So, we will access that method as a dynamic property:
$comment = AppComment::find(1);
$commentable = $comment->commentable;
The commentable relation on the Comment model will return either a Post or Video instance, depending on which type of model owns the comment.
See this link: polymorphic-relations:
You can entry like that:
+---------+----------------+-------------------+
| user_id | commentable_id | commentable_type |
+---------+----------------+-------------------+
| 1 | 1 | AppPost |
| 1 | 2 | AppPost |
| 1 | 3 | AppPost |
| 1 | 1 | AppVideo |
| 1 | 2 | AppVideo |
| 1 | 3 | AppVideo |
+---------+----------------+-------------------+
thank you for replying @Keval Mangukiya but what I need is the parameters inside $this->morphMany() function the documentation does not elaborate the parameters of that function
– silent_terius
Nov 10 at 4:24
add a comment |
up vote
0
down vote
up vote
0
down vote
Polymorphic Relations
Table Structure
Polymorphic relations allow a model to belong to more than one other model on a single association. For example, imagine users of your application can "comment" on both posts and videos. Using polymorphic relationships, you can use a single comments table for both of these scenarios. First, let's examine the table structure required to build this relationship:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
Two important columns to note are the commentable_id and commentable_type columns on the comments table. The commentable_id column will contain the ID value of the post or video, while the commentable_type column will contain the class name of the owning model. The commentable_type column is how the ORM determines which "type" of owning model to return when accessing the commentable relation.
Model Structure
Next, let's examine the model definitions needed to build this relationship:
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Comment extends Model
/**
* Get all of the owning commentable models.
*/
public function commentable()
return $this->morphTo();
class Post extends Model
/**
* Get all of the post's comments.
*/
public function comments()
return $this->morphMany('AppComment', 'commentable');
class Video extends Model
/**
* Get all of the video's comments.
*/
public function comments()
return $this->morphMany('AppComment', 'commentable');
Retrieving Polymorphic Relations
Once your database table and models are defined, you may access the relationships via your models. For example, to access all of the comments for a post, we can use the comments dynamic property:
$post = AppPost::find(1);
foreach ($post->comments as $comment)
//
You may also retrieve the owner of a polymorphic relation from the polymorphic model by accessing the name of the method that performs the call to morphTo. In our case, that is the commentable method on the Comment model. So, we will access that method as a dynamic property:
$comment = AppComment::find(1);
$commentable = $comment->commentable;
The commentable relation on the Comment model will return either a Post or Video instance, depending on which type of model owns the comment.
See this link: polymorphic-relations:
You can entry like that:
+---------+----------------+-------------------+
| user_id | commentable_id | commentable_type |
+---------+----------------+-------------------+
| 1 | 1 | AppPost |
| 1 | 2 | AppPost |
| 1 | 3 | AppPost |
| 1 | 1 | AppVideo |
| 1 | 2 | AppVideo |
| 1 | 3 | AppVideo |
+---------+----------------+-------------------+
Polymorphic Relations
Table Structure
Polymorphic relations allow a model to belong to more than one other model on a single association. For example, imagine users of your application can "comment" on both posts and videos. Using polymorphic relationships, you can use a single comments table for both of these scenarios. First, let's examine the table structure required to build this relationship:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
Two important columns to note are the commentable_id and commentable_type columns on the comments table. The commentable_id column will contain the ID value of the post or video, while the commentable_type column will contain the class name of the owning model. The commentable_type column is how the ORM determines which "type" of owning model to return when accessing the commentable relation.
Model Structure
Next, let's examine the model definitions needed to build this relationship:
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Comment extends Model
/**
* Get all of the owning commentable models.
*/
public function commentable()
return $this->morphTo();
class Post extends Model
/**
* Get all of the post's comments.
*/
public function comments()
return $this->morphMany('AppComment', 'commentable');
class Video extends Model
/**
* Get all of the video's comments.
*/
public function comments()
return $this->morphMany('AppComment', 'commentable');
Retrieving Polymorphic Relations
Once your database table and models are defined, you may access the relationships via your models. For example, to access all of the comments for a post, we can use the comments dynamic property:
$post = AppPost::find(1);
foreach ($post->comments as $comment)
//
You may also retrieve the owner of a polymorphic relation from the polymorphic model by accessing the name of the method that performs the call to morphTo. In our case, that is the commentable method on the Comment model. So, we will access that method as a dynamic property:
$comment = AppComment::find(1);
$commentable = $comment->commentable;
The commentable relation on the Comment model will return either a Post or Video instance, depending on which type of model owns the comment.
See this link: polymorphic-relations:
You can entry like that:
+---------+----------------+-------------------+
| user_id | commentable_id | commentable_type |
+---------+----------------+-------------------+
| 1 | 1 | AppPost |
| 1 | 2 | AppPost |
| 1 | 3 | AppPost |
| 1 | 1 | AppVideo |
| 1 | 2 | AppVideo |
| 1 | 3 | AppVideo |
+---------+----------------+-------------------+
edited Nov 10 at 3:58
answered Nov 10 at 3:45
Keval Mangukiya
1231310
1231310
thank you for replying @Keval Mangukiya but what I need is the parameters inside $this->morphMany() function the documentation does not elaborate the parameters of that function
– silent_terius
Nov 10 at 4:24
add a comment |
thank you for replying @Keval Mangukiya but what I need is the parameters inside $this->morphMany() function the documentation does not elaborate the parameters of that function
– silent_terius
Nov 10 at 4:24
thank you for replying @Keval Mangukiya but what I need is the parameters inside $this->morphMany() function the documentation does not elaborate the parameters of that function
– silent_terius
Nov 10 at 4:24
thank you for replying @Keval Mangukiya but what I need is the parameters inside $this->morphMany() function the documentation does not elaborate the parameters of that function
– silent_terius
Nov 10 at 4:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235721%2fexplain-eloquent-morphmany-parameters%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown