php - one to many relationship help in laravel 4 -


i trying create relationship between category , product somehow couldn't use category connect product table , prints out product names , instead category's name

in database

table name: products columns: id, name, price, category_id, description table name: categories columns: id, name, description 

in products table

id: 1 name: product1 price: 10 category_id: 1 description: p1 --------------- id: 2 name: product2 price: 10 category_id: 1 description: p2 

in categories table

id: 1 name: category1 description: c1 --------------- id: 2 name: category2 description: c2 

product.php inside models folder

class product extends eloquent {     protected $product = 'products';     public function category()     {         return $this->belongsto('category');     } } 

category.php inside models folder

class category extends eloquent {     protected $category = 'categories';     public function product()     {         return $this->hasmany('product', 'category_id');     } } 

profilecontroller.php in controller folder

class profilecontroller extends basecontroller {     public function user($username)     {         $user = user::where('username', '=', $username);          if ($user->count())         {         $user = $user->first();         $title = 'user profile';         $category = category::find(1);         $products = category::find(1)->name;         return view::make('profile.user', compact('user', 'title', 'category', 'products'));     }      return 'user not found. please create account'; } 

}

user.blade.php inside profile folder inside view folder

@extends('layouts.master') @section('content')     {{ auth::user()->username }}     <br>     {{ auth::user()->email }}     <br>     <h1>{{ 'category name: '. $category->name }}</h1>     <br>     <h3>{{ 'category description: ', $category->description }}</h3>     <br>     {{ $products }} @stop 

at first {{$products}} used foreach loop

@foreach($products $product)     {{$product}} @endforeach 

but got error

errorexception invalid argument supplied foreach() (view: j:\wamp\www\test\app\views\profile\user.blade.php) 

so tried var_dump($products) , realized $products gives out category1 name of category want printing name of products has category_id 1

can give me hand this? did mess relationship or did stupid codes?

in controller:

$category = category::find(1); $products = $category->product; 

then in template can use:

@foreach ($products $product)     {{ $product->name }} @endforeach 

better yet use eager loading , forget assigning products manually:

controller:

$category = category::with('product')->where('id', 1)->first(); 

template:

@foreach ($category->product $product)     {{ $product->name }} @endforeach 

ps: read more on eager loading here: http://laravel.com/docs/eloquent#eager-loading in order prevent dreaded n + 1 query problem!


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -