How does it works the Laravel 4 model relationships?

martinezjc

I wish to know how works the laravel 4 relationships in the model. This question is because i'm trying to get the rows from 2 tables with relation and not success till now.

class Products extends Eloquent {

protected $table = "Products";
protected $primaryKey = "ProductId";

public function plandetail()
{
        return $this->belongsTo("PlanDetail", "PlanDetail.ProductId")->select( array("ProductId", "Order") );
}

public function getProductsPlan($dealerId)
{
    return $this->with( "plandetail" )->where("Products.DealerId", $dealerId)->get();
}
} // end model

// In my controller

$product = new Products();
$products = $product->getProductsPlan($id);

foreach( $products as $product )
{
     print_r($product); // prints only the rows from the Products table
                        // not the PlanDetail table.
}

In the output is just print the product records and not the PlanDetail table rows. Do i need to add a join with the PlanDetail table to get these rows?

This is one example row of what im getting now:

Products Object ( [table:protected] => Products [primaryKey:protected] => ProductId [errors:Products:private] => [rules:Products:private] => Array ( [ProductBaseId] => required|numeric [DealerId] => required|numeric [ProductName] => required|max:255 [DisplayName] => required|max:255 [Bullets] => required [Cost] => required|numeric [SellingPrice] => required|numeric [UseWebServicePricing] => required|boolean [Term] => required|numeric [Type] => required|numeric [Deductible] => required|numeric [VehiclePlan] => required|numeric [Mileage] => required|numeric [TireRotation] => required|numeric [Interval] => required|numeric [UseRangePricing] => required|boolean [IsTaxable] => required|boolean [NotRegulated] => required|boolean [CreatedOn] => required [CreatedBy] => required [ModifiedBy] => required [ModifiedOn] => required ) [connection:protected] => [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [ProductId] => 3 [ProductBaseId] => 84 [DealerId] => 50 [ProductName] => Vehicle Service Contract [DisplayName] => US Warranty Service Contract [ProductDescription] => [Bullets] => Warranty Proteccion for the covered components,Rental Car Coverage,Towling Coverage,, [Cost] => 0.0 [SellingPrice] => 0.0 [BrochureImage] => [PDFContrator] => [UseWebServicePricing] => 1 [UseManualPricing] => 0 [BrochureHeight] => 0 [BrochureWidth] => 0 [Term] => 36 [Type] => Platinum [Deductible] => 100 [VehiclePlan] => None [Mileage] => 36000 [TireRotation] => 0 [Interval] => 0 [PENProductId] => 0 [DMSProductId] => 16 [CreatedBy] => GREIVIN BRITTON [CreatedOn] => 2015-01-08 22:26:47.000 [UseRangePricing] => 0 [ModifiedBy] => GREIVIN BRITTON [ModifiedOn] => 2015-01-28 15:06:11.000 [IsTaxable] => 0 [NotRegulated] => 0 ) [original:protected] => Array ( [ProductId] => 3 [ProductBaseId] => 84 [DealerId] => 50 [ProductName] => Vehicle Service Contract [DisplayName] => US Warranty Service Contract [ProductDescription] => [Bullets] => Warranty Proteccion for the covered components,Rental Car Coverage,Towling Coverage,, [Cost] => 0.0 [SellingPrice] => 0.0 [BrochureImage] => [PDFContrator] => [UseWebServicePricing] => 1 [UseManualPricing] => 0 [BrochureHeight] => 0 [BrochureWidth] => 0 [Term] => 36 [Type] => Platinum [Deductible] => 100 [VehiclePlan] => None [Mileage] => 36000 [TireRotation] => 0 [Interval] => 0 [PENProductId] => 0 [DMSProductId] => 16 [CreatedBy] => GREIVIN BRITTON [CreatedOn] => 2015-01-08 22:26:47.000 [UseRangePricing] => 0 [ModifiedBy] => GREIVIN BRITTON [ModifiedOn] => 2015-01-28 15:06:11.000 [IsTaxable] => 0 [NotRegulated] => 0 ) [relations:protected] => Array ( [plandetail] => ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 [softDelete:protected] => ) 

Thanks in advance :)

user1669496

It looks like there's a few things wrong here.

The belongsTo method expects the name of the relating model, and then the foreign key of that model on the Products table. It looks like your foreign key is actually on the relating model making this relationship either a hasOne or hasMany. In this case, since you say you are expecting 2 rows, I'm assuming hasMany.

Additionally, the second argument of this only expects the name of the column. It can already guess the name of the table because you gave it the relating model name.

With that in mind, this should be the relationship method you need.

public function plandetails()
{
    return $this->hasMany("PlanDetail", "ProductId")->select( array("ProductId", "Order") );
}

And the other function should be modified a bit. When you use with and you need to query based on the relation, you should pass in an array where the key is the name of the relation and the value is a callback function which will allow you to modify the query which will grab the relating items.

public function getProductsPlan($dealerId)
{
    return $this->with(array("plandetail", function($q) use ($dealerId)
    {
        $q->where('DealerID', $dealerId);
    }))->get();
}

Now when you do this...

$product = new Products();
$products = $product->getProductsPlan($id);

This should work...

foreach( $products as $product ) {
    foreach($product->plandetails as $plandetail) {
        print_r($plandetail);
    }
}

I also modified the relating function name so you would have to update your getProductsPlan function to reflect that when you use with. When you use hasMany, it makes more sense and is easier to read when you make the function name plural.

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 4 mockery mock model relationships

From Dev

Laravel 4: Model Relationships Not Working (sort of)?

From Dev

laravel 4 mockery mock model relationships

From Dev

Laravel Category Model Relationships

From Dev

Reverse model relationships in Laravel

From Dev

Laravel model relationships and model events

From Dev

Laravel 4 - relationships

From Dev

Laravel5: How are Eloquent model relationships expressed in the database?

From Dev

How do I traverse through model relationships in laravel with dot syntax

From Dev

Laravel model relationships Class not found

From Dev

Laravel - dd() model with all relationships

From Dev

Laravel return model relationships in JSON

From Dev

How does name spacing works using PSR4 in laravel 5?

From Dev

How many relationships does neo4j support in a graph?

From Dev

How does Laravel Views Cache works?

From Dev

Laravel 4 many to many relationships

From Dev

Laravel eloquent: Update A Model And its Relationships

From Dev

Laravel 5.1 testing with model relationships and factories

From Dev

Laravel 5.1 testing with model relationships and factories

From Dev

Laravel include relationships with model after update

From Dev

How to model the relationships among 3 model in Realm?

From Dev

how to use a simple Relationships in laravel

From Dev

How to query intermediate relationships in Laravel

From Dev

Why Laravel does not save relationships immediately?

From Dev

Why Laravel does not save relationships immediately?

From Dev

Laravel 4 - Chaining Many-to-Many relationships

From Dev

Laravel 4: Eloquent soft deletes and relationships

From Dev

How make categories relationships with several model?

From Dev

How to define model relationships for this Rails app for physiotherapists?

Related Related

  1. 1

    laravel 4 mockery mock model relationships

  2. 2

    Laravel 4: Model Relationships Not Working (sort of)?

  3. 3

    laravel 4 mockery mock model relationships

  4. 4

    Laravel Category Model Relationships

  5. 5

    Reverse model relationships in Laravel

  6. 6

    Laravel model relationships and model events

  7. 7

    Laravel 4 - relationships

  8. 8

    Laravel5: How are Eloquent model relationships expressed in the database?

  9. 9

    How do I traverse through model relationships in laravel with dot syntax

  10. 10

    Laravel model relationships Class not found

  11. 11

    Laravel - dd() model with all relationships

  12. 12

    Laravel return model relationships in JSON

  13. 13

    How does name spacing works using PSR4 in laravel 5?

  14. 14

    How many relationships does neo4j support in a graph?

  15. 15

    How does Laravel Views Cache works?

  16. 16

    Laravel 4 many to many relationships

  17. 17

    Laravel eloquent: Update A Model And its Relationships

  18. 18

    Laravel 5.1 testing with model relationships and factories

  19. 19

    Laravel 5.1 testing with model relationships and factories

  20. 20

    Laravel include relationships with model after update

  21. 21

    How to model the relationships among 3 model in Realm?

  22. 22

    how to use a simple Relationships in laravel

  23. 23

    How to query intermediate relationships in Laravel

  24. 24

    Why Laravel does not save relationships immediately?

  25. 25

    Why Laravel does not save relationships immediately?

  26. 26

    Laravel 4 - Chaining Many-to-Many relationships

  27. 27

    Laravel 4: Eloquent soft deletes and relationships

  28. 28

    How make categories relationships with several model?

  29. 29

    How to define model relationships for this Rails app for physiotherapists?

HotTag

Archive