Ad

Why Does My Query Work In My VM Dev Environment But Not In Production/online?

- 1 answer

I have a relatively simple set up for products and reviews. Here are the relevant parts of my Models:

Product

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

Review

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

In my controller, I grab the product:

$product = Product::find($id);

Then I grab the reviews:

$reviews = $product->reviews()->get()

This works fine in my VM dev environment, but when I upload it to my live site it doesn't work. If I try and access $product->reviews in Tinker, I get a 'call to undefined method` error. I have uploaded all the model files and added the relevant tables to the database. I can directly access the Reviews with

$reviews = App\Reviews::all()

I just can't access them via the product. What gives?

Ad

Answer

You can access reviews on product via

$product = Product::with('reviews')->where('id', $id)->get();

// then use the reviews
$reviews = $product->reviews;
Ad
source: stackoverflow.com
Ad