douhuo3696 2016-10-17 10:05 采纳率: 0%
浏览 50
已采纳

Laravel - 使用Eloquent从两个表中检索所有用户的项目

I have two tables

1- projects 2- clients

I need to get the names from the client table and the project name from the projects table

my Projects class has

public function projectClient()
    {
        return $this->hasMany('User');
    }

and my User class has

    class User extends Eloquent implements UserInterface, RemindableInterface
{

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'client';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

    public function project()
    {
        return $this->belongsTo('Projects', 'id');
    }

}

and in my clients controller

$clients = User::with('project')->get();
        //dd($clients);
        return View::make('admin.manageClients.viewClients', compact('clients'));

now in the view I try this

@foreach($clients as $clt)
{{ $clt->first_name }} //work fine
{{ $clt->pro_title }} //didn't show the project title
@endforeach

how can I show this project title here?

Update

as @Mahfuzul Alam and @WebKenth suggest I did it like this

{{ $clt->project->pro_title }}

but something still wrong there

in my database I got two user and the the project_id = 1 but in my view the first user project_id = 1 and the second project_id = 2

  • 写回答

2条回答 默认 最新

  • dongyanling9248 2016-10-17 10:08
    关注

    Considering, pro_title as your project title column try {{ $clt->project->pro_title }}. If it don't work, they please paste code of your User class and let us take a look at that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?