简体   繁体   中英

how to get username from user_id on laravel 5.3?

@foreach($productChunk as $product)
  <div class="pull-left user_id">{{ $product->user_id }}</div>
@endforeach

currently this is my code. it currently show the user's id that upload that product how to make it show the uploader name?

public function getIndex()
{
  $products = Product::all();
  return view('shop.index', compact(['products']));
}

and this is the code from the controller

i already made the relationship

//Product.php
public function user()
{
  //$this->belongsTo(User::class);
  $this->hasOne('App\User');
}


//User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use Notifiable;

public function product()
{
  return $this->hasMany('App\Product');
}
protected $fillable = [
    'name', 'email', 'password',
];

protected $hidden = [
    'password', 'remember_token',
];
}

this i the table schema for both

//user
Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
//product
Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->text('description');
        $table->integer('price');
        $table->integer('category_id');
        $table->string('image')->nullable();
        $table->integer('user_id');
        $table->timestamps();
    });
}

replace

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

with

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

in Product Model. and also use get() instead of all() in controller.

$products = Product::get();

UPDATE: In Prodcut.php your user() method is not returning a relation type. You didnt notice the answer i posted earlier, see the difference between your user() method and the method i have posted earlier. Your method need to return a relation type.

Replace

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

with

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

in Product.php and access user name in your view using $product->user->name

You must be having two models product.php and user.php. The user is linked to product using user_id field.

Then you need to add just add a relationship :

add following in product model :

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


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

Now inside your blade you can do :

@foreach($productChunk as $product)
  <div class="pull-left user_id">{{ $product->user->name }}</div>
@endforeach

Add eager loading in controller :

public function getIndex()
{
  $products = Product::with('user')->get();
  return view('shop.index', compact(['products']));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM