尝试在Laravel中输出口才关系时出错 [英] Error trying to output the Eloquent relationship in Laravel

查看:93
本文介绍了尝试在Laravel中输出口才关系时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误消息试图获取非对象的属性'company_name'".我研究了Eloquent关系,并尝试在我的代码中实现.但这给了我视图(products.show)

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)

哪一部分错了?

也可以与其他模型建立许多不同的关系吗?

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

在供应商模型"中:

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

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

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

在产品型号"中:

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

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

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

在用户模型"中:

    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');
    }

在"products.show"中:

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>

在"ProductController"中:

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');
        });

在产品表中:

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');
        });

推荐答案

据我所知,您需要建立从其他模型访问属性的关系,因此,在这种情况下,您需要获取company_name为此,您需要告诉您的模型要带给供应商.示例:

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;

现在我注意到您的show方法中有一些东西,您正在发送Product类,但不是雄辩的对象,也许仅使用Eloquent会有所帮助吗?例子

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 :)

这篇关于尝试在Laravel中输出口才关系时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆