Laravel 5获取个人资料名称

时间:2015-08-05 14:39:40

标签: php laravel

我是laravel begginer,我不想做简单的CMS。好的,所以我有一个简单的问题: CMS

我想点击头像(个人资料图片) - 重定向到用户个人资料(http://example.com/profiles/nickname/id) 在DB中它保存: DB

正如您所看到的author_id,现在我需要从users表中获取作者姓名: users table

然后生成网址:http://example.com/profiles/Evaldas/2(Evaldas,因为author_id中的2topics table

我的路线档案:

Route::get('topic/{tname}/{tid}', 'viewTopic@showTopic');

我的viewTopic.php控制器:

<?php 

namespace App\Http\Controllers;
use DB;
use View;

class viewTopic extends Controller 
{
    public function showTopic($tname, $tid)
    {
        return View::make('posts', [
            'topics'  => DB::table('topics')
                ->where('id', $tid)
                ->where('seo_title', $tname)
                ->first(),
            'posts'  => DB::table('posts')
                ->where('topic_id', $tid)
                ->select()
                ->get()
        ]);
    }
}

布局:

@extends('layouts.main')
@section('content')
<div class="media">
  <div class="media-left">
    <a href="HERE MUST BE HREF TO PROFILE">
      <img class="media-object" src="http://localhost/uploads/avatars/2.jpg" style="width: 64px">
    </a>
  </div>
  <div class="media-body" rel="#author{{ $topics->author_id }}">
    <h4 class="media-heading">{{ $topics->title }}</h4>
    @if(!empty($topics->text))
    {{ $topics->text }}
    @else
    Message empty :(
    @endif
  </div>
    @foreach($posts as $post)
    <div class="media">
  <div class="media-left">
    <a href="HERE MUST BE HREF TO PROFILE">
      <img class="media-object" src="http://localhost/uploads/avatars/1.png" style="width: 64px">
    </a>
  </div>
  <div class="media-body" rel="#post{{ $post->pid }}">
  {{ $post->text }}
  </div>
</div>
@endforeach
</div>
@stop

提前致谢;)

1 个答案:

答案 0 :(得分:1)

您可以使用User对象进行查找...但更好的选择是加入“帖子”#39;表格给用户&#39;表格,并从结果中选择它 - 看看http://laravel.com/docs/5.1/queries#joins