JoewyZN's avatar

Relationships not working

Hello .. I have the following

My products object

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

    public function brands()
    {
        return $this->belongsTo('App\Brand');
    }

    public function categories()
    {
        return $this->belongsTo('App\Category');
    }

    public function subcategories()
    {
        return $this->belongsTo('App\Subcategory');
    }

    protected $table = 'products';

    protected $fillable = ['id', 'title','quantity','price','brand','category','subcategory','created_by','created_at','updated_at'];

}

My Category Model

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

    public function subcategories()
    {
        return $this->hasMany('App\Subcategory');
    }

    protected $table = 'categories';

    protected $fillable = ['id', 'title'];

    public $timestamps=false;
    public $incrementing=true;

}


My View

@if(sizeof($products)>0)
            <div class="card-content table-responsive">
                <table class="table">
                    <thead class="text-success">
                        <th>ID</th>
                        <th>TITLE</th>
                        <th>QUANTITY</th>
                        <th>PRICE</th>
                        <th>CATEGORY</th>
                        <th>SUBCATEGORY</th>
                        <th>BRAND</th>
                        <th></th>
                        <th></th>
                        <th></th>
                     
                    </thead>
                    <tbody>
                @foreach ($products as $product)
                    <tr>
                        <td>{{ $product['id'] }}</td>
                        <td>{{ $product['title'] }}</td>
                        <td>{{ $product['quantity']." Units" }}</td>
                        <td>{{ "K". $product['price'] }}</td>
                        <td>{{ $product->category }}</td> // <<<< CATEGORY
                        <td>{{ $product->subcategory }}</td>
                        <td>{{ $product->brand }}</td>
                        <td><a href="{{url('/account/stock/edit/')}}/{{ $product['id'] }}"><i class="material-icons text-gray">edit</i></a></td>
                        <td><a href="{{url('/account/stock/update/')}}/{{ $product['id'] }}"><i class="material-icons text-gray">autorenew</i></a></td>
                        <td><a href="{{url('/account/stock/delete/')}}/{{ $product['id'] }}"><i class="material-icons text-gray">delete_forever</i></a></td>
                    </tr>
                @endforeach
                 </tbody>
                </table>
            </div>  
            <div align="center"> {!! $products->render() !!} </div>
        @else
            <div class="card-content">
                <h2>No products were found in the system</h2>
            </div>
        @endif

My Controller

//DATA BEING SENT TO THE CONTROLLER

public function index()
{
    $products=Product::orderBy('title')->paginate(10);
    $products->setPath('view');
    return view('stock/view')->with('products',$products);
}
    

What am i doing wrong .. Dont get me wrong, i know about database concepts and how relationships work in general, maybe im not doing it correctly according to laravel, please help .. "Thank you's" later .. :)

0 likes
11 replies
michaelrtm's avatar

Do you have a Subcategory model with relationships on Category and Product, and do you have a Brand model setup too?

Do the Products only belong to one Category and one Subcategory?

Can you show the store method on Products?

1 like
MaverickChan's avatar

$product is an object

$product->id is the right form.

and your category relationship , the function name is categories

so it should be

$product->categories

and so is the subcategories

1 like
JoewyZN's avatar

Hi michael, here is my subcategory model;

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class SubCategory extends Model 
{
    public function products()
    {
        return $this->hasMany('App\Product');
    }

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

    protected $table = 'subcategories';

    protected $fillable = ['id', 'title','parent'];

    public $timestamps=false;
    public $incrementing=true;

}

And yes, the products can only have one category and one subcategory.

For the store method on the products, its nothing complicated, im just storing the index of the category and subcategory.

JoewyZN's avatar

Bipin.. thank you, let me take a look ..

JoewyZN's avatar

MaverickChan.. thanks for that.. :)

michaelrtm's avatar

As @MaverickChan said, your methods are plural and you are trying to access singular. Rename your categories, subcategories and brands relation methods to category, subcategory and brand.

You also probably don't need to declare $table on subcategories, as Laravel (Symfony\Inflector) takes care of that for you.

JoewyZN's avatar

@michaelrtm .. But wont that be abit ambiguous ? Because my database table has the exact same column name, hence i just renamed it to categories and subcategories.

michaelrtm's avatar

Your column names should actually be category_id and subcategory_id, not just category and subcategory

The Eloquent relation looks for the model_id column in your database automatically.

Watch this series if you haven't, and pay attention to Episode 15

JoewyZN's avatar

So otherwise i have to explicitly specify the foreign key in each model right...

michaelrtm's avatar

That's correct, so it's kind of a waste of your time to not name your columns relationmodel_id :)

Please or to participate in this conversation.