0

In table in view with data from products table I am traying to show names from user table. Now in this colum i've got only user_id from product table witch is foreign key. I made Laravel relations in models and foreign keys. And added directory in product controller for user model. This is part of my controller for showing product data:

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

This is part of my view wher i am trying to show data from user table, but have error in it

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

This is my user model:

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

This is my product model:

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

1 回答 1

0

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

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

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

{{ $product->user->name }}
于 2020-07-26T13:06:52.747 回答