Error trying to output the Eloquent relationship i

2019-08-16 10:40发布

问题:

I'm getting the error "Trying to get property 'company_name' of non-object". I studied about the Eloquent relationship and try to implement in my code. But it gives me that error in the view (products.show)

Which part are wrong?

Is it okay to have many different relationship to other model as well?

In 'Vendor Model':

public function getRouteKeyName()
{
    return 'roc_no';
}

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

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

In 'Product Model':

public function getRouteKeyName()
{
    return 'slug';
}

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

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

In 'User Model':

    public function vendor()
    {
        return $this->hasOne('App\Vendor');
    }

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

    public function roles()
    {
        return $this->belongsToMany('App\Role', 'role_users');
    }

In the 'products.show':

...    
{!! $product->description !!}
<!-- The error is at $product->vendor->company_name -->
Company Name: <a href="/vendors/{{ $product->vendor_roc_no }}">{{ $product->vendor->company_name }}</a>

In 'ProductController':

    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|string|max:255',
            'slug' => 'required|string|max:100',
            'description' => 'required',
            'image' => 'nullable',
        ]);

        $product = new Product;
        $product->name = $request->name;
        $product->slug = $request->slug;
        $product->description = $request->description;
        $product->vendor_roc_no = auth()->user()->vendor->roc_no;
        $product->save();

        return redirect('/account/products')->with('success', 'Product added successfully.');
    }

    public function show(Product $product)
    {    
        return view('products.show')->with('product', $product);
    }

Updated: In vendors table:

Schema::create('vendors', function (Blueprint $table) {
            $table->increments('id');
            $table->string('company_name');
            $table->string('roc_no');
            $table->string('company_address');
            $table->string('company_tel');
            $table->string('company_fax')->nullable();
            $table->string('company_email');
            $table->string('company_website')->nullable();
            $table->string('company_logo')->nullable();
            $table->text('company_profile')->nullable();
            $table->unsignedInteger('user_id');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users');
        });

In products table:

Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('slug')->unique();
            $table->text('description');
            $table->string('image')->nullable();
            $table->string('vendor_roc_no');
            $table->timestamps();

            // $table->foreign('vendor_id')->references('id')->on('vendors');
        });

回答1:

As far as I know, you'll need to establish the relationship for accessing properties from the other model, so in this case, you want to get the company_name that is on the vendor for this you'll need to tell your model to bring vendor. Example:

$user->with('anyRelation')->get();
// Then you can use like, $user->anyRelation->property;

Now I noticed something in your show method, you're sending Product class but not an eloquent object maybe just using Eloquent would help? Example

$products = Product:all(); // this would return every record on your db
return view('products.show')->with('products', $products);

I hope this helps :)