dongtan8532 2016-11-20 23:05 采纳率: 0%
浏览 60
已采纳

如何在默认用户模型上设置Eloquent关系

I am unable to set up eloquent relationship on the default User model.Spare table has a foreign key which is UserID referencing User table. Following is the code

User.php

class User extends Authenticatable
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

public function isAdmin()
{
    return $this->type; // this looks for an admin column in your users table
}
public function spares()
{
    return $this->hasMany('App\Spares');
}

}

Spare.php

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

SearchController.php

 public function index(Request $request)
{

    $spares=Spares::with('user','model')
        ->where ('description','LIKE','%'.$request->searchName.'%')
        ->paginate(5);
    return View::make('browse')->with('spares', $spares);

}

browse.blade.php cannot access {{$spare->user->name }}

@foreach($spares as $spare)

<tr>
<td class="col-md-6">
<div class="media">
<a class="thumbnail pull-left" href="#"> <img class="media-object"
         src='{{ asset("images/spares/$spare->imagePath") }}'
          tyle="width: 100px; height: 100px;padding-left: 10px"> </a>
<div  class="media-body"  style="padding-left: 10px;">

<h4 class="media-heading"><a href="#">{{$spare->description}}</a></h4>
<h5 class="media-heading"> by <a href="#">{{$spare->user->name }}</a></h5>
</div>
</div>
</td>

<td class="col-md-1 text-center"><strong>Rs. {{$spare->price}}/=</strong></td>

</tr>

@endforeach

following is the error

enter image description here

  • 写回答

1条回答 默认 最新

  • dsn5510 2016-11-20 23:37
    关注

    In reply to your comment:

    spares table is 'spares' in that foreign key is 'retailer_id' which references the User table

    From the documentation:

    Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with _id. However, if the foreign key on the Spare model is not user_id, you may pass a custom key name as the second argument to the belongsTo method:

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

    However, it's probably better to make Eloquent just succeed in resolving your relation without any override when not necessary. One way is to rename your method in Spare.php:

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

    Another would be to leave that method as is and rename the foreign key column name to user_id. But you'll of course lose the semantics of the word retailer in this case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题