Laravel Fetching one to Many Relationship

Stacey

Hello I am learning laravel and I am having an issue retrieving data from my relations.

In my database there are Product and Groups filled with dummy data.

I defined my relationship like this in product model:

     public function Group()
        {
     return $this->hasMany('App\Groups','product_id', 'id');
      }

And in my group vice versa with :

      public function Product()
      {
      return $this->belongsTo('App\Product','product_id', 'id');
      }

The way I am referencing to my products table is :

     $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');

Now I have column product_id in my database under groups, and it is linked to if from products id it seems.

The groups table contains of its auto incremented id and product_id foreign key column.

While products table has auto incremented id and name column.

The issue is here : How do I return the products that are not null or have value (of products id) in groups table.


I tried something like this in my filter controller:

        public function getProductsWithGroup()
{

      $Products = Product::with('groups')->get();
     return $Products ;

}

But that is giving me call to undefined relations.

I am not sure how to access belongsTo or hasMany methods and whether I need an extra group_id column in my products table.

Ijas Ameenudeen

You named the relationship wrong. It should be groups & define in lowercase as

public function groups()
{
    return $this->hasMany('App\Groups','product_id', 'id');
}

And use ->has() to check existence

public function getProductsWithGroup()
{
    $Products = Product::has('groups')->get();
    return $Products ;
}

->with() is used to eager load and ->has() is used to check existence & filter.

To get the products don't have any groups,

$Products = Product::doesntHave('groups')->get();

To see other ways to use ->has() check, https://laravel.com/docs/5.7/eloquent-relationships#querying-relationship-existence

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel save one to many relationship

From Dev

saving, deleting and fetching data from a one to many relationship core data

From Dev

Laravel One-to-many relationship

From Dev

Laravel Many to Many Relationship or One to Many Relationship

From Dev

one to many relationship not working in Laravel

From Dev

Laravel: one to one relationship becomes one to many relationship

From Dev

one to many And one to many relationship laravel

From Dev

One to Many Relationship - Laravel & Mysql

From Dev

One to Many Relationship in Laravel 5.5

From Dev

Fetching has many relationship data Laravel and using avg function

From Dev

Laravel One to Many of One to Many relationship

From Dev

Laravel One To Many Relationship

From Dev

Laravel One to Many Relationship not working

From Dev

Laravel One to Many relationship for system

From Dev

Laravel filter one to many relationship

From Dev

Fetching one to many relationship from same table

From Dev

Laravel 5.1: Many-to-many alongside one-to-many relationship

From Dev

Laravel - One to Many relationship is not working one way

From Dev

one to many relationship in laravel

From Dev

Laravel Many-to-one relationship

From Dev

Laravel insert in one to many relationship

From Dev

One to Many to One relationship in laravel eloquent

From Dev

Seeding Relationship one to many in Laravel

From Dev

Laravel one to many (polymorphic) relationship

From Dev

Laravel one to many relationship issue

From Dev

many to many Relationship Data fetching

From Dev

Laravel - Eloquent Relationship not working One to Many relationship

From Dev

Laravel Eloquent Many to Many to One relationship

From Dev

Create Laravel Relationship through Many-To-One and Many-To-Many

Related Related

HotTag

Archive