Many-to-many relationship - it doesn't display elements
I want to have many-to-many relationship. So i created:
Into Game model
public function category(){
return $this->belongsToMany('AppCategory');
}
Into Category model
public function games(){
return $this->hasMany('AppGame');
}
Into controller
$category = Category::where('slug', $slug)->first();
dd($category->games());
return view('frontend.game.gamelist')->with('elements', $category->games());
Generally, I try display all games belongs to particular category. I see something like this
If I remove dd() view won't display any elements.But it doesn't problem with view.
@foreach($elements as $element)
//...
@endforeach
Why it doesn;t work?
laravel many-to-many relationship
add a comment |
I want to have many-to-many relationship. So i created:
Into Game model
public function category(){
return $this->belongsToMany('AppCategory');
}
Into Category model
public function games(){
return $this->hasMany('AppGame');
}
Into controller
$category = Category::where('slug', $slug)->first();
dd($category->games());
return view('frontend.game.gamelist')->with('elements', $category->games());
Generally, I try display all games belongs to particular category. I see something like this
If I remove dd() view won't display any elements.But it doesn't problem with view.
@foreach($elements as $element)
//...
@endforeach
Why it doesn;t work?
laravel many-to-many relationship
It must be$category->games
not$category->games()
, Call it like a property.
– ako
Nov 20 '18 at 13:56
add a comment |
I want to have many-to-many relationship. So i created:
Into Game model
public function category(){
return $this->belongsToMany('AppCategory');
}
Into Category model
public function games(){
return $this->hasMany('AppGame');
}
Into controller
$category = Category::where('slug', $slug)->first();
dd($category->games());
return view('frontend.game.gamelist')->with('elements', $category->games());
Generally, I try display all games belongs to particular category. I see something like this
If I remove dd() view won't display any elements.But it doesn't problem with view.
@foreach($elements as $element)
//...
@endforeach
Why it doesn;t work?
laravel many-to-many relationship
I want to have many-to-many relationship. So i created:
Into Game model
public function category(){
return $this->belongsToMany('AppCategory');
}
Into Category model
public function games(){
return $this->hasMany('AppGame');
}
Into controller
$category = Category::where('slug', $slug)->first();
dd($category->games());
return view('frontend.game.gamelist')->with('elements', $category->games());
Generally, I try display all games belongs to particular category. I see something like this
If I remove dd() view won't display any elements.But it doesn't problem with view.
@foreach($elements as $element)
//...
@endforeach
Why it doesn;t work?
laravel many-to-many relationship
laravel many-to-many relationship
asked Nov 20 '18 at 13:49
MrBelongsToMrBelongsTo
186
186
It must be$category->games
not$category->games()
, Call it like a property.
– ako
Nov 20 '18 at 13:56
add a comment |
It must be$category->games
not$category->games()
, Call it like a property.
– ako
Nov 20 '18 at 13:56
It must be
$category->games
not $category->games()
, Call it like a property.– ako
Nov 20 '18 at 13:56
It must be
$category->games
not $category->games()
, Call it like a property.– ako
Nov 20 '18 at 13:56
add a comment |
4 Answers
4
active
oldest
votes
You Category
model also needs to have belongsToMany
in its games()
relation.
public function games() {
return $this->belongsToMany('AppGame');
}
add a comment |
You are using lazy loaded data
Laravel eloquent Lazy Vs. Eager Loaded
Update your controller method:
$category = Category::with('games')->where('slug', $slug)->first();
dd($category->games);
return view('frontend.game.gamelist')->with('elements', $category);
You have to get all the category with games, so that you can get those data with an query also see them in dd
function.
If you only get category and then do category->games()
it will execute another query in your view.
add a comment |
When you do:
$category->games();
you are accessing the relationship itself (useful to attach contraints), not the elements of the relation. Check this other answer for a detail explanation.
Try this instead:
$category->games;
OBS
When doing a Many-to-Many relationship, the belongsToMany
method needs to be specified in both models.
Category.php
public function games()
{
return $this->belongsToMany('AppGame');
}
add a comment |
You need to have a belongsToMany
relationship in your Category
model as well to have a many-many relationship. You can achieve this by doing:
public function games()
{
return $this->belongsToMany('AppGame');
}
Then in your category controller, you have:
$category = Category::with('games')->where('slug', $slug)->get();
return view('frontend.game.gamelist', compact('category');
Then in your view, you can access the list of games by
@foreach($category->games as $game)
//...
@endforeach
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53394478%2fmany-to-many-relationship-it-doesnt-display-elements%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You Category
model also needs to have belongsToMany
in its games()
relation.
public function games() {
return $this->belongsToMany('AppGame');
}
add a comment |
You Category
model also needs to have belongsToMany
in its games()
relation.
public function games() {
return $this->belongsToMany('AppGame');
}
add a comment |
You Category
model also needs to have belongsToMany
in its games()
relation.
public function games() {
return $this->belongsToMany('AppGame');
}
You Category
model also needs to have belongsToMany
in its games()
relation.
public function games() {
return $this->belongsToMany('AppGame');
}
answered Nov 20 '18 at 13:51
JerodevJerodev
17.8k84370
17.8k84370
add a comment |
add a comment |
You are using lazy loaded data
Laravel eloquent Lazy Vs. Eager Loaded
Update your controller method:
$category = Category::with('games')->where('slug', $slug)->first();
dd($category->games);
return view('frontend.game.gamelist')->with('elements', $category);
You have to get all the category with games, so that you can get those data with an query also see them in dd
function.
If you only get category and then do category->games()
it will execute another query in your view.
add a comment |
You are using lazy loaded data
Laravel eloquent Lazy Vs. Eager Loaded
Update your controller method:
$category = Category::with('games')->where('slug', $slug)->first();
dd($category->games);
return view('frontend.game.gamelist')->with('elements', $category);
You have to get all the category with games, so that you can get those data with an query also see them in dd
function.
If you only get category and then do category->games()
it will execute another query in your view.
add a comment |
You are using lazy loaded data
Laravel eloquent Lazy Vs. Eager Loaded
Update your controller method:
$category = Category::with('games')->where('slug', $slug)->first();
dd($category->games);
return view('frontend.game.gamelist')->with('elements', $category);
You have to get all the category with games, so that you can get those data with an query also see them in dd
function.
If you only get category and then do category->games()
it will execute another query in your view.
You are using lazy loaded data
Laravel eloquent Lazy Vs. Eager Loaded
Update your controller method:
$category = Category::with('games')->where('slug', $slug)->first();
dd($category->games);
return view('frontend.game.gamelist')->with('elements', $category);
You have to get all the category with games, so that you can get those data with an query also see them in dd
function.
If you only get category and then do category->games()
it will execute another query in your view.
answered Nov 20 '18 at 13:56
Emtiaz ZahidEmtiaz Zahid
1,042516
1,042516
add a comment |
add a comment |
When you do:
$category->games();
you are accessing the relationship itself (useful to attach contraints), not the elements of the relation. Check this other answer for a detail explanation.
Try this instead:
$category->games;
OBS
When doing a Many-to-Many relationship, the belongsToMany
method needs to be specified in both models.
Category.php
public function games()
{
return $this->belongsToMany('AppGame');
}
add a comment |
When you do:
$category->games();
you are accessing the relationship itself (useful to attach contraints), not the elements of the relation. Check this other answer for a detail explanation.
Try this instead:
$category->games;
OBS
When doing a Many-to-Many relationship, the belongsToMany
method needs to be specified in both models.
Category.php
public function games()
{
return $this->belongsToMany('AppGame');
}
add a comment |
When you do:
$category->games();
you are accessing the relationship itself (useful to attach contraints), not the elements of the relation. Check this other answer for a detail explanation.
Try this instead:
$category->games;
OBS
When doing a Many-to-Many relationship, the belongsToMany
method needs to be specified in both models.
Category.php
public function games()
{
return $this->belongsToMany('AppGame');
}
When you do:
$category->games();
you are accessing the relationship itself (useful to attach contraints), not the elements of the relation. Check this other answer for a detail explanation.
Try this instead:
$category->games;
OBS
When doing a Many-to-Many relationship, the belongsToMany
method needs to be specified in both models.
Category.php
public function games()
{
return $this->belongsToMany('AppGame');
}
answered Nov 20 '18 at 13:58
HCKHCK
3,3151832
3,3151832
add a comment |
add a comment |
You need to have a belongsToMany
relationship in your Category
model as well to have a many-many relationship. You can achieve this by doing:
public function games()
{
return $this->belongsToMany('AppGame');
}
Then in your category controller, you have:
$category = Category::with('games')->where('slug', $slug)->get();
return view('frontend.game.gamelist', compact('category');
Then in your view, you can access the list of games by
@foreach($category->games as $game)
//...
@endforeach
add a comment |
You need to have a belongsToMany
relationship in your Category
model as well to have a many-many relationship. You can achieve this by doing:
public function games()
{
return $this->belongsToMany('AppGame');
}
Then in your category controller, you have:
$category = Category::with('games')->where('slug', $slug)->get();
return view('frontend.game.gamelist', compact('category');
Then in your view, you can access the list of games by
@foreach($category->games as $game)
//...
@endforeach
add a comment |
You need to have a belongsToMany
relationship in your Category
model as well to have a many-many relationship. You can achieve this by doing:
public function games()
{
return $this->belongsToMany('AppGame');
}
Then in your category controller, you have:
$category = Category::with('games')->where('slug', $slug)->get();
return view('frontend.game.gamelist', compact('category');
Then in your view, you can access the list of games by
@foreach($category->games as $game)
//...
@endforeach
You need to have a belongsToMany
relationship in your Category
model as well to have a many-many relationship. You can achieve this by doing:
public function games()
{
return $this->belongsToMany('AppGame');
}
Then in your category controller, you have:
$category = Category::with('games')->where('slug', $slug)->get();
return view('frontend.game.gamelist', compact('category');
Then in your view, you can access the list of games by
@foreach($category->games as $game)
//...
@endforeach
edited Nov 20 '18 at 14:10
answered Nov 20 '18 at 14:04
Peter SowahPeter Sowah
427110
427110
add a comment |
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%2f53394478%2fmany-to-many-relationship-it-doesnt-display-elements%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
It must be
$category->games
not$category->games()
, Call it like a property.– ako
Nov 20 '18 at 13:56