2014-08-31 4 views
-4

Я хочу получить имя пользователя в таблице пользователя, идентификатора сообщения в почтовой таблице и вернуть View::make('post.index'). Мне нужно посмотреть в клике post.index.Нужна помощь laravel 4.2

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 
+2

Извинения заранее, но то, что вы написали крайне неясными , Можете ли вы отредактировать, чтобы было более ясно, какую проблему вы хотите решить? – rayryeng

+0

hi rayryeng спасибо, я обновляю сообщение, нужно помочь, чтобы получить author_id, чтобы получить имя пользователя в таблице пользователя – btn25

+0

Хорошо, это немного лучше. Надеюсь, кто-то сможет вам помочь. Удачи! – rayryeng

ответ

0

лучших практик: ваши имена классов модели должны быть User и Post. имена таблиц базы данных должны быть users и posts.

в Post.php файле

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

в User.php файле

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

в ваших PostController.php

public function index() 
{ 
    $user_of_the_post = Post::find(postId)->user; //replace the postId //this will return the user object of the specified post 
    $user_name_of_the_author = $user_of_the_post->username; 
    **pass the necessary data to the view and print and return the view** 
} 
+0

ожидания я обновить я получил ошибку – btn25

+0

это есть 'общественной функция пользователь() { возвращение $ this-> belongsTo («User»); } 'метод в модели Post? –

+0

да у меня в post.php в папке моделей – btn25

Смежные вопросы