尝试获取非对象的属性“name”(view:/home/laravel/web/laravel.swt101.eu/public\u html/abonamenty/resources/views/products/show.blade.php)

tzcvj98z  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(410)

在带有products表中的数据的视图中,我试图显示user表中的名称。现在在这个列中,我从产品表中得到了唯一的用户id,它是外键。我用模型和外键建立了拉威尔关系。并在产品控制器中为用户模型添加了目录。这是我的控制器的一部分,用于显示产品数据:

public function show(Product $product)
{
    $user = User::all('name','id');
    return view('products.show',compact('product', 'user'))->with('user', $user);
}

这是我试图显示用户表中的数据时视图的一部分,但其中有错误

<div class="col-xs-12 col-sm-12 col-md-12">
        <div class="form-group">
            <strong>Kontrahent:</strong>
            {{ $product->$user->name }}
        </div>
    </div>

这是我的用户模型:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use Kyslik\ColumnSortable\Sortable;
use Illuminate\Database\Eloquent\Model;

class User extends Authenticatable
{
    use Notifiable;
    use HasRoles;
    use Sortable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'surname', 'showname', 'business', 'NIP', 'PESEL', 'address', 'city', 'postalcode', 'phone', 'comments', 
    ];
    public $primaryKey = 'id';
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public $sortable = ['name',
                        'email',
                         'surname', 
                        'showname', 
                        'business',
                        'address',
                        'city',
                        'phone',
                        'role',
                       ];

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

这是我的产品型号:

<?php

namespace App;

use Kyslik\ColumnSortable\Sortable;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    /**
     * The attributes that are mass assignable.
     *  
     * @var array
     */
    use Sortable;

    protected $fillable = [
        'name', 'detail', 'id', 'created_at', 'updated at', 'category', 'launchdate', 'expirationdate', 'renewalprice', 'user_id', 'billingperiod', 'internalcost', 'status'
    ];

    public $sortable = ['id', 'name', 'created_at', 'updated at', 'category', 'launchdate', 'expirationdate', 'renewalprice', 'category', 'user_id'];

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

}
xam8gpfp

xam8gpfp1#

在产品模型中检查您的关系

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

请在定义关系时指定id,然后尝试使用

{{ $product->user->name }}

相关问题