doujiunai2169
2017-05-07 18:13
浏览 143
已采纳

如何从laravel 5.3上的user_id获取用户名?

@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();
    });
}

图片转代码服务由CSDN问答提供 功能建议

  @foreach($ productChunk as $ product)
&lt; div class =“pull-left user_id  “&gt; {{$ product-&gt; user_id}}&lt; / div&gt; 
 @ endforeach 
   
 
 

目前这是我的代码。 它目前显示上传该产品的用户id如何使其显示上传者名称?

  public function getIndex()
 {
 $ products = Product :: all  (); 
返回视图('shop.index',compact(['products'])); 
} 
   
 
 

这是来自的代码 控制器

我已经建立了关系

  // Product.php 
public function user()
 {
 //  $ this-&gt; belongsTo(User :: class); 
 $ this-&gt; hasOne('App \ User'); 
} 
 
 
 // User.php 
&lt;?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()
 {
返回$ this-&gt; hasMany('App \ Product'); 
} 
保护$ fillable = [
'name','email','password',
]  ; 
 
保护$ hidden = [
'密码','remember_token',
]; 
} 
   
 
 

这是我的表架构

 <代码> //用户
  Schema :: create('users',function(Blueprint $ table){
 $ table-&gt; incrementments('id'); 
 $ table-&gt; string('name'); 
 $ table-&gt  ; string('email') - &gt; unique(); 
 $ table-&gt; string('password'); 
 $ table-&gt; rememberToken(); 
 $ table-&gt; timestamps();  
}); 
 // product 
Schema :: create('products',function(Blueprint $ table){
 $ table-&gt; increments('id'); 
 $ table-&gt; string(  'name'); 
 $ table-&gt; text('description'); 
 $ table-&gt; integer('price'); 
 $ table-&gt; integer('category_id'); 
 $  table-&gt; string('image') - &gt; nullable(); 
 $ table-&gt; integer('user_id'); 
 $ table-&gt; timestamps(); 
}); 
}  
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongwu8050 2017-05-07 20:00
    已采纳

    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

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题