Laravel hasMany return null
I have model Category:
class Category extends Model
{
protected $fillable = ['title', 'description', 'keywords', 'slug'];
public function getRouteKeyName()
{
return 'slug';
}
public function products()
{
return $this->hasMany(Product::class);
}
}
And have model product:
class Product extends Model
{
protected $guarded = ;
public function category()
{
return $this->belongsTo(Category::class);
}
}
In table products I have product with column category_id. But return null. Why? I can't get products in current category with $category->products, because always empty. How I can fix this?
In model product relation category working, but on model category not working..
On table products I have cascade:
$table->foreign('category_id')
->references('id')->on('categories')
->onDelete('cascade');
php laravel
add a comment |
I have model Category:
class Category extends Model
{
protected $fillable = ['title', 'description', 'keywords', 'slug'];
public function getRouteKeyName()
{
return 'slug';
}
public function products()
{
return $this->hasMany(Product::class);
}
}
And have model product:
class Product extends Model
{
protected $guarded = ;
public function category()
{
return $this->belongsTo(Category::class);
}
}
In table products I have product with column category_id. But return null. Why? I can't get products in current category with $category->products, because always empty. How I can fix this?
In model product relation category working, but on model category not working..
On table products I have cascade:
$table->foreign('category_id')
->references('id')->on('categories')
->onDelete('cascade');
php laravel
Can you show the migration of thecategorytable please ?
– Steve Chamaillard
Nov 14 '18 at 22:07
Can you show the code where you are trying to access the$category->productsrelation?
– Peter
Nov 15 '18 at 2:09
Check that your 'category_id' in the products table is unsigned. eg.$table->integer('category_id')->unsigned();
– CUGreen
Nov 15 '18 at 3:29
add a comment |
I have model Category:
class Category extends Model
{
protected $fillable = ['title', 'description', 'keywords', 'slug'];
public function getRouteKeyName()
{
return 'slug';
}
public function products()
{
return $this->hasMany(Product::class);
}
}
And have model product:
class Product extends Model
{
protected $guarded = ;
public function category()
{
return $this->belongsTo(Category::class);
}
}
In table products I have product with column category_id. But return null. Why? I can't get products in current category with $category->products, because always empty. How I can fix this?
In model product relation category working, but on model category not working..
On table products I have cascade:
$table->foreign('category_id')
->references('id')->on('categories')
->onDelete('cascade');
php laravel
I have model Category:
class Category extends Model
{
protected $fillable = ['title', 'description', 'keywords', 'slug'];
public function getRouteKeyName()
{
return 'slug';
}
public function products()
{
return $this->hasMany(Product::class);
}
}
And have model product:
class Product extends Model
{
protected $guarded = ;
public function category()
{
return $this->belongsTo(Category::class);
}
}
In table products I have product with column category_id. But return null. Why? I can't get products in current category with $category->products, because always empty. How I can fix this?
In model product relation category working, but on model category not working..
On table products I have cascade:
$table->foreign('category_id')
->references('id')->on('categories')
->onDelete('cascade');
php laravel
php laravel
edited Nov 15 '18 at 4:37
Peter
8841213
8841213
asked Nov 14 '18 at 21:55
DumitruDumitru
342112
342112
Can you show the migration of thecategorytable please ?
– Steve Chamaillard
Nov 14 '18 at 22:07
Can you show the code where you are trying to access the$category->productsrelation?
– Peter
Nov 15 '18 at 2:09
Check that your 'category_id' in the products table is unsigned. eg.$table->integer('category_id')->unsigned();
– CUGreen
Nov 15 '18 at 3:29
add a comment |
Can you show the migration of thecategorytable please ?
– Steve Chamaillard
Nov 14 '18 at 22:07
Can you show the code where you are trying to access the$category->productsrelation?
– Peter
Nov 15 '18 at 2:09
Check that your 'category_id' in the products table is unsigned. eg.$table->integer('category_id')->unsigned();
– CUGreen
Nov 15 '18 at 3:29
Can you show the migration of the
category table please ?– Steve Chamaillard
Nov 14 '18 at 22:07
Can you show the migration of the
category table please ?– Steve Chamaillard
Nov 14 '18 at 22:07
Can you show the code where you are trying to access the
$category->products relation?– Peter
Nov 15 '18 at 2:09
Can you show the code where you are trying to access the
$category->products relation?– Peter
Nov 15 '18 at 2:09
Check that your 'category_id' in the products table is unsigned. eg.
$table->integer('category_id')->unsigned();– CUGreen
Nov 15 '18 at 3:29
Check that your 'category_id' in the products table is unsigned. eg.
$table->integer('category_id')->unsigned();– CUGreen
Nov 15 '18 at 3:29
add a comment |
3 Answers
3
active
oldest
votes
Validate your definition
return $this->hasMany('AppProduct', 'category_id');
You have error here because attach on catergories but it will be on products
$table->foreign('category_id')
->references('id')->on('products')
->onDelete('cascade');
https://laravel.com/docs/5.6/eloquent-relationships#querying-relations
please add more details to your answer
– Supun Praneeth
Nov 15 '18 at 5:44
His mean you should try this for Category modelreturn $this->hasMany(Product::class, category_id);
– Truong Dang
Nov 15 '18 at 5:55
What exactly I mean one category have many products with foreign key category_Id but mistake you have it is on adding foreign key on categories
– Ismoil Shifoev
Nov 15 '18 at 6:35
add a comment |
Please do following changes
Make change in product migration file (in products table)
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('category_id')->on('categories');
Then change in Category model
public function products()
{
return $this->hasMany('AppProduct','category_id');
}
3.Then get all categories with products list
use AppCategory
// include category model where you want to call this function
Category::with('products')->get();
add a comment |
Everything is right, just make changes in the products migration
change this code
$table->foreign('category_id')->references('id')
->on('categories')->onDelete('cascade');
to
$table->foreign('category_id')->references('id')
->on('products'->onDelete('cascade');
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%2f53309309%2flaravel-hasmany-return-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Validate your definition
return $this->hasMany('AppProduct', 'category_id');
You have error here because attach on catergories but it will be on products
$table->foreign('category_id')
->references('id')->on('products')
->onDelete('cascade');
https://laravel.com/docs/5.6/eloquent-relationships#querying-relations
please add more details to your answer
– Supun Praneeth
Nov 15 '18 at 5:44
His mean you should try this for Category modelreturn $this->hasMany(Product::class, category_id);
– Truong Dang
Nov 15 '18 at 5:55
What exactly I mean one category have many products with foreign key category_Id but mistake you have it is on adding foreign key on categories
– Ismoil Shifoev
Nov 15 '18 at 6:35
add a comment |
Validate your definition
return $this->hasMany('AppProduct', 'category_id');
You have error here because attach on catergories but it will be on products
$table->foreign('category_id')
->references('id')->on('products')
->onDelete('cascade');
https://laravel.com/docs/5.6/eloquent-relationships#querying-relations
please add more details to your answer
– Supun Praneeth
Nov 15 '18 at 5:44
His mean you should try this for Category modelreturn $this->hasMany(Product::class, category_id);
– Truong Dang
Nov 15 '18 at 5:55
What exactly I mean one category have many products with foreign key category_Id but mistake you have it is on adding foreign key on categories
– Ismoil Shifoev
Nov 15 '18 at 6:35
add a comment |
Validate your definition
return $this->hasMany('AppProduct', 'category_id');
You have error here because attach on catergories but it will be on products
$table->foreign('category_id')
->references('id')->on('products')
->onDelete('cascade');
https://laravel.com/docs/5.6/eloquent-relationships#querying-relations
Validate your definition
return $this->hasMany('AppProduct', 'category_id');
You have error here because attach on catergories but it will be on products
$table->foreign('category_id')
->references('id')->on('products')
->onDelete('cascade');
https://laravel.com/docs/5.6/eloquent-relationships#querying-relations
edited Nov 15 '18 at 7:46
Ross Wilson
16.4k22741
16.4k22741
answered Nov 15 '18 at 5:41
Ismoil ShifoevIsmoil Shifoev
1,271813
1,271813
please add more details to your answer
– Supun Praneeth
Nov 15 '18 at 5:44
His mean you should try this for Category modelreturn $this->hasMany(Product::class, category_id);
– Truong Dang
Nov 15 '18 at 5:55
What exactly I mean one category have many products with foreign key category_Id but mistake you have it is on adding foreign key on categories
– Ismoil Shifoev
Nov 15 '18 at 6:35
add a comment |
please add more details to your answer
– Supun Praneeth
Nov 15 '18 at 5:44
His mean you should try this for Category modelreturn $this->hasMany(Product::class, category_id);
– Truong Dang
Nov 15 '18 at 5:55
What exactly I mean one category have many products with foreign key category_Id but mistake you have it is on adding foreign key on categories
– Ismoil Shifoev
Nov 15 '18 at 6:35
please add more details to your answer
– Supun Praneeth
Nov 15 '18 at 5:44
please add more details to your answer
– Supun Praneeth
Nov 15 '18 at 5:44
His mean you should try this for Category model
return $this->hasMany(Product::class, category_id);– Truong Dang
Nov 15 '18 at 5:55
His mean you should try this for Category model
return $this->hasMany(Product::class, category_id);– Truong Dang
Nov 15 '18 at 5:55
What exactly I mean one category have many products with foreign key category_Id but mistake you have it is on adding foreign key on categories
– Ismoil Shifoev
Nov 15 '18 at 6:35
What exactly I mean one category have many products with foreign key category_Id but mistake you have it is on adding foreign key on categories
– Ismoil Shifoev
Nov 15 '18 at 6:35
add a comment |
Please do following changes
Make change in product migration file (in products table)
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('category_id')->on('categories');
Then change in Category model
public function products()
{
return $this->hasMany('AppProduct','category_id');
}
3.Then get all categories with products list
use AppCategory
// include category model where you want to call this function
Category::with('products')->get();
add a comment |
Please do following changes
Make change in product migration file (in products table)
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('category_id')->on('categories');
Then change in Category model
public function products()
{
return $this->hasMany('AppProduct','category_id');
}
3.Then get all categories with products list
use AppCategory
// include category model where you want to call this function
Category::with('products')->get();
add a comment |
Please do following changes
Make change in product migration file (in products table)
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('category_id')->on('categories');
Then change in Category model
public function products()
{
return $this->hasMany('AppProduct','category_id');
}
3.Then get all categories with products list
use AppCategory
// include category model where you want to call this function
Category::with('products')->get();
Please do following changes
Make change in product migration file (in products table)
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('category_id')->on('categories');
Then change in Category model
public function products()
{
return $this->hasMany('AppProduct','category_id');
}
3.Then get all categories with products list
use AppCategory
// include category model where you want to call this function
Category::with('products')->get();
answered Nov 15 '18 at 9:45
amba patelamba patel
635213
635213
add a comment |
add a comment |
Everything is right, just make changes in the products migration
change this code
$table->foreign('category_id')->references('id')
->on('categories')->onDelete('cascade');
to
$table->foreign('category_id')->references('id')
->on('products'->onDelete('cascade');
add a comment |
Everything is right, just make changes in the products migration
change this code
$table->foreign('category_id')->references('id')
->on('categories')->onDelete('cascade');
to
$table->foreign('category_id')->references('id')
->on('products'->onDelete('cascade');
add a comment |
Everything is right, just make changes in the products migration
change this code
$table->foreign('category_id')->references('id')
->on('categories')->onDelete('cascade');
to
$table->foreign('category_id')->references('id')
->on('products'->onDelete('cascade');
Everything is right, just make changes in the products migration
change this code
$table->foreign('category_id')->references('id')
->on('categories')->onDelete('cascade');
to
$table->foreign('category_id')->references('id')
->on('products'->onDelete('cascade');
answered Nov 15 '18 at 8:38
Zeeshan TanveerZeeshan Tanveer
313
313
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.
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%2f53309309%2flaravel-hasmany-return-null%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
Can you show the migration of the
categorytable please ?– Steve Chamaillard
Nov 14 '18 at 22:07
Can you show the code where you are trying to access the
$category->productsrelation?– Peter
Nov 15 '18 at 2:09
Check that your 'category_id' in the products table is unsigned. eg.
$table->integer('category_id')->unsigned();– CUGreen
Nov 15 '18 at 3:29