Laravel hasMany return null












0















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');









share|improve this question

























  • 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











  • Check that your 'category_id' in the products table is unsigned. eg. $table->integer('category_id')->unsigned();

    – CUGreen
    Nov 15 '18 at 3:29
















0















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');









share|improve this question

























  • 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











  • Check that your 'category_id' in the products table is unsigned. eg. $table->integer('category_id')->unsigned();

    – CUGreen
    Nov 15 '18 at 3:29














0












0








0








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');









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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











  • 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 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

















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












3 Answers
3






active

oldest

votes


















1














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






share|improve this answer


























  • 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











  • 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



















4














Please do following changes





  1. Make change in product migration file (in products table)



     $table->integer('category_id')->unsigned();
    $table->foreign('category_id')->references('category_id')->on('categories');



  2. 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();





share|improve this answer































    1














    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');





    share|improve this answer























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      1














      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






      share|improve this answer


























      • 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











      • 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
















      1














      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






      share|improve this answer


























      • 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











      • 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














      1












      1








      1







      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






      share|improve this answer















      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







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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 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



















      • 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











      • 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













      4














      Please do following changes





      1. Make change in product migration file (in products table)



         $table->integer('category_id')->unsigned();
        $table->foreign('category_id')->references('category_id')->on('categories');



      2. 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();





      share|improve this answer




























        4














        Please do following changes





        1. Make change in product migration file (in products table)



           $table->integer('category_id')->unsigned();
          $table->foreign('category_id')->references('category_id')->on('categories');



        2. 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();





        share|improve this answer


























          4












          4








          4







          Please do following changes





          1. Make change in product migration file (in products table)



             $table->integer('category_id')->unsigned();
            $table->foreign('category_id')->references('category_id')->on('categories');



          2. 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();





          share|improve this answer













          Please do following changes





          1. Make change in product migration file (in products table)



             $table->integer('category_id')->unsigned();
            $table->foreign('category_id')->references('category_id')->on('categories');



          2. 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();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 9:45









          amba patelamba patel

          635213




          635213























              1














              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');





              share|improve this answer




























                1














                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');





                share|improve this answer


























                  1












                  1








                  1







                  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');





                  share|improve this answer













                  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');






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 8:38









                  Zeeshan TanveerZeeshan Tanveer

                  313




                  313






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      List item for chat from Array inside array React Native

                      Thiostrepton

                      Caerphilly