0

I'm writing a sample ecommerce website with Laravel 5. I have 2 Tables:

Schema::create('products', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->text('description');
    $table->float('price');
    $table->integer('category_id');
    $table->timestamps();
});

and

Schema::create('featureds', function (Blueprint $table) {
    $table->integer('product_id')->unique()->unsigned();
});

Schema::table('featureds', function($table) {
    $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});

Models

class Product extends Model
{
    public function category(){
        return $this->belongsTo('App\Category');
    }    
}

class Featured extends Model
{
    public function product(){
        return $this->hasOne('App\Product', 'product_id');
    }
}

Then, I have a Controller, where I take 4 featured products:

$featured_products = Featured::limit(4)->get();
return view('home', ['featured_products' => $featured_products]);

Now, I'm trying to show these featured products in my view. If i show the product_id from the Featured model, everything is ok:

@foreach($featured_products as $prod)
  {{$prod->product_id}}
@endforeach

But I want to take the name of the product referred by the featured. I tried this way:

@foreach($featured_products as $prod)
  @foreach($prod as $p)
    {{$p->name}}
  @endforeach
@endforeach

Because featured_products (in the controller) seems to be a collection, but it doesn't work!

Tirth Patel
  • 5,443
  • 3
  • 27
  • 39
luca89pe
  • 97
  • 1
  • 14

1 Answers1

0

In your Featured model, you have a relationship in method product() when you want to access the relation from the view, you could call the method name as property, in you case, you have a method named product() so you have to call product property like this:

@foreach($featured_products as $prod)
    {{ $prod->product->name }}
@endforeach

It will automatically write the product name based on the relationship you have configured in the model.

Reference: https://laravel.com/docs/5.2/eloquent-relationships

Edit:

Sorry my bad, I guess you are defining a wrong relation, your Product model, should have a featured() method which uses hasOne relation, while Featured model should have a product() method using belongsTo relation. So in you App\Featured model, you have to define the relation like this:

return $this->belongsTo('App\Product');

And in your App\Product model you should define relation like so:

return $this->hasOne('App\Featured');

Hope it works

Bagaskara Wisnu Gunawan
  • 1,166
  • 1
  • 12
  • 15
  • Hi, first thank you for the help. I tried this way, but it returns this: `ErrorException in Connection.php line 673: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.product_id' in 'where clause' (SQL: select * from 'products' where 'products'.'product_id' is null and 'products'.'product_id' is not null limit 1) (View: /home/vagrant/Code/projects/mwt-lamp/resources/views/widgets/featured.blade.php) (View: /home/vagrant/Code/projects/mwt-lamp/resources/views/widgets/featured.blade.php)` – luca89pe Apr 04 '16 at 12:04
  • Sorry my bad, I guess you are defining a wrong relation, your `Product` model, should have a `featured()` method which uses `hasOne` relation, while `Featured` model should have a `product()` method using `belongsTo` relation. So in you `App\Featured` model, you have to define the relation like this `return $this->belongsTo('App\Product');`. And in your `App\Product` model you should define relation like so `return $this->hasOne('App\Featured');`. Hope it works – Bagaskara Wisnu Gunawan Apr 05 '16 at 06:12
  • Yeah, it works, i just changed `hasOne` to `belongsTo` on function `product()` into `Featured` model. Can you edit the answer, so i can set it as Answer? Thank you – luca89pe Apr 06 '16 at 20:35
  • okay, I have edited my answer, thank you and glad it works! Cheers! – Bagaskara Wisnu Gunawan Apr 07 '16 at 04:42