需要帮助laravel 4.2

时间:2014-08-31 04:47:30

标签: laravel

我想获取表用户的用户名,post表中的帖子ID并返回View::make('post.index')。我需要在post.index刀片中查看。

Database
users table

id
username

post table
  id
  message
  posts_id

Post.php @models folder

    public function  posts()
    {
        return $this->belongsTo('User');
    }

User.php @models folder


    public function post()
    {
        return $this->hasMany('Post');
    }





PostController.php @controller folder
    public function index()
        {

        $posts = Post::with('username')->get();
        return View::make('post.index')->with('posts', $posts);
        }




post/index.blade.php

        the result here
        i need to the result of author id become the username 

1 个答案:

答案 0 :(得分:0)

了解最佳做法: 您的模型类名称应为UserPost。数据库表名称应为usersposts

<{1>}文件中的

Post.php
<{1>}文件中的

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

User.php

public function posts()
{
    return $this->hasMany('Post');
}
相关问题