doutan2111 2016-06-19 19:28
浏览 65
已采纳

当您知道id时,Laravel会从其他表中获取数据

I have a table "reviews" with a column named "from_user". So basically the user who placed the review. In this column the users's id is stored from the users table. Is it possible to find out the users name in my view?

This is what it looks like now:

 @foreach($authUser->reviews as $review)
   <p>{{ $review->body }} - {{$review->from_user }}</p>
 @endforeach

So this code now obviously displays the id of the user.

This is what my users table looks like:

ID | NAME | USERNAME | ...

This is what my reviews table looks like:

ID | from_user | on_user | body

so the from_user from my reviews table equals the ID of my users table

So my question is is it possible to access my username in my foreach loop in my view?

I am fairly new to laravel so any help is appreciated!

Many thanks in advance

EDIT: User Model

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword, Messagable;

    protected $table = 'users';

    protected $fillable = ['name', 'email', 'password', 'firstname', 'lastname', 'straat', 'postcode', 'plaats', 'provincie', 'role', 'specialisatie'];

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

    public function projects()
    {
        return $this->hasMany('App\Project');
    }

    public function reviews()
    {
        return $this->hasMany('App\Review');
    }

    public function comments()
    {
        return $this->hasMany('App\Comment');
    }
}

EDIT2 my Review model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Review extends Model
{
    protected $guarded = [];

    protected $table = 'reviews';

    public function User()
    {
        return $this->belongsTo('App\User');
    }
}
  • 写回答

1条回答 默认 最新

  • dongquan8753 2016-06-19 19:36
    关注

    Updated

    In your review model you need to tell the relation what foreign key to use. By default it add relationName_id.

    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 Phone model is not user_id, you may pass a custom key name as the second argument to the belongsTo method. #Source

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Review extends Model
    {
        protected $guarded = [];
    
        protected $table = 'reviews';
    
        public function user()
        {
           return $this->belongsTo('App\User', 'from_user');
        }
    }
    

    And in your view

    @foreach($authUser->reviews as $review)
       <p>{{ $review->body }} - {{ $review->user->name }}</p>
     @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能