doutan2111 2016-06-19 11: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 11: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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部