2015-11-06 57 views
0

我坚持一个非常愚蠢的查询。Laravel加入查询返回空,而原始查询有一个值

我试图找到像天的解决方案,但没有任何帮助,所以我没有找到答案。

我想从表中获得newest反应,以显示用户名。

首先,这些都是我的数据库方案:

comments(具有型号CommentComments

threads(具有型号ThreadThreads

所以我的原始查询看起来像这个:

select `comments`.`username` from `comments` inner join `threads` on `comments`.`tid` = `threads`.`tid` where `comments`.`deleted_at` is null and `threads`.`cid` = '$categorie->id' order by `comments`.`posted_at` desc limit 1 

A var_dump()$categorie->id返回int(3)其中3代表分类编号。

当我执行在Navicat查询(原始),它返回: Output

什么是好的,因为这是它需要返回corract值。

然而,当我重建“Laravel,雄辩的风格”这一查询,查询看起来是这样的:

Comment::join('threads', 'comments.tid', '=', 'threads.tid')->where('threads.cid', '=', $categorie->id)->orderBy('comments.posted_at', 'DESC')->pluck('comments.username') 

要注意的是,查询是建立在foreach循环中,有一个if语句之前。但是,这将在稍后播种。

这没有任何回报。

当我检查元素时,我什么都没有。我试过了DB::select(DB::raw('query')),但那也行不通。

我使我的网页在ForumController:

public function index() 
    { 
     $forums = Forums::orderBy('disp_order', 'asc')->get(); 
     $categories = Categorie::orderBy('disp_order', 'asc')->get(); 

     return View::make('index')->with('forums', $forums)->with('categories', $categories); 
    } 

这工作得很好,并认为是这样的:

@foreach($forums as $forum) 
<div class="panel-group col-sm-12"> 
      <div class="panel panel-default"> 
       <div class="panel-heading" style="background-color: {{ $forum->color }};"> 

       <i class="fa fa-folder-open-o fa-fw"></i> 
        <strong><a href="Forum-{{ Str::slug($forum->name) }}">{{ $forum->name }}</a></strong> 

       </div> 
      <div id="forum4" class="panel-collapse collapse in"> 

      <div class="table-responsive"> 
       <table class="table table-hover"> 
       <thead> 
        <tr> 
        <th></th> 
        <th>Forum naam</th> 
        <th class="text-right">Topics</th> 
        <th class="">Laatste post info</th> 
        </tr> 
       </thead> 
       <tbody> 
       <tr> 

       @foreach($categories as $categorie) 
       @if($categorie->fid == $forum->fid) 
       <td class="topic-marker-forum"> 
       <i class="fa fa-comments fa-3x"></i> 
       </td> 

       <td class="col-md-6 col-sm-6"> 
       <div><a href="Categorie-{{ Str::slug($categorie->name) }}" title="{{ $categorie->name }}"><strong>{{ $categorie->name }}</strong></a></div> 
       <div class=""><em>{{ $categorie->description }}</em></div> 

       </td> 

       <td class="col-md-1 col-sm-1 text-right"><span class="badge">{{ Thread::where('cid', '=', $categorie->id)->remember(15)->count() }}</span></td> 

       <td class="col-md-4 col-sm-4 "> 
       <div> 
        @if(Thread::where('cid', '=', $categorie->id)->exists() && Comment::join('threads', 'comments.tid', '=', 'threads.tid')->where('threads.cid', '=', $categorie->id)->orderBy('comments.posted_at', 'DESC')->exists()) 
        <a href="{{ Config::get('app.url') }}/Thread-{{ Thread::where('cid', '=', $categorie->id)->orderBy('date_posted', 'DESC')->pluck('slug') }}">{{ Helper::HTMLFilter(Thread::where('cid', '=', $categorie->id)->orderBy('date_posted', 'DESC')->pluck('title')) }}</a><br> 

        <i class="fa fa-clock-o"></i> {{ \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', Comment::join('threads', 'comments.tid', '=', 'threads.tid')->where('threads.cid', '=', $categorie->id)->orderBy('comments.posted_at', 'DESC')->pluck('posted_at'))->format('d/m/Y H:i') }}<br> 

        <i class="fa fa-user"></i> <a href="{{ Config::get('app.url') }}/User-{{ Comment::join('threads', 'comments.tid', '=', 'threads.tid')->where('threads.cid', '=', $categorie->id)->orderBy('comments.posted_at', 'DESC')->pluck('comments.username') }}">{{ Comment::join('threads', 'comments.tid', '=', 'threads.tid')->where('threads.cid', '=', $categorie->id)->orderBy('comments.posted_at', 'DESC')->pluck('comments.username') }}</a> 
        @else 
        <b>-</b> 
        @endif 
       </div> 
       </td> 
      </tr> 
      @endif 
      @endforeach 

      </tbody> 
      </table></div> 
      </div> 
     </div> 
     </div> 
@endforeach 

最奇怪的是,这样的:

{{ \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', Comment::join('threads', 'comments.tid', '=', 'threads.tid')->where('threads.cid', '=', $categorie->id)->orderBy('comments.posted_at', 'DESC')->pluck('posted_at'))->format('d/m/Y H:i') }} 

只是工作。就像没有什么不对,当我打电话给它时,它会返回正确的值。

我的模特儿很正常;

<?php 
use Illuminate\Database\Eloquent\SoftDeletingTrait; 

class Comment extends Eloquent 
{ 
    use SoftDeletingTrait; 

    protected $dates = ['deleted_at']; 

    protected $table = 'comments'; 

    public $timestamps = false; 

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

} 

而且

<?php 
use Illuminate\Database\Eloquent\SoftDeletingTrait; 

class Thread extends Eloquent 
{ 
    use SoftDeletingTrait; 

    protected $dates = ['deleted_at']; 

    protected $table = 'threads'; 

    public $timestamps = false; 
} 

希望有人能帮助我!

+0

可你'的var_dump(DB :: getQueryLog())'运行查询后,我们可以看到原始查询的差异 –

+0

http://laravel.io/bin/2W7QN是我得到的:)(从该查询) – Robin

回答

1

尝试是这样的

Comment::with(['threads'=>function($query) use ($categorie){ 
       $query->where('threads.cid',$categorie->id) 
         ->whereNull('comments.deleted_at'); 
       }]) 
       ->orderBy('comments.posted_at', 'DESC') 
       ->limit(1) 
       ->pluck('comments.username'); 

如果你需要 - 是 'deleted_at' 不为空

->whereNotNull('comments.deleted_at'); 



DB::table('coments') 
      ->join('threads', 'comments.tid', '=', 'threads.tid') 
      ->where('threads.cid',$categorie->id) 
      ->whereNull('comments.deleted_at'); 
      ->orderBy('comments.posted_at', 'DESC') 
      ->limit(1) 
      ->pluck('comments.username'); 
+0

第二个代码就像一个魅力。 (删除一些错字后),我也会检查第一个代码。如果有效,我会在评论中告诉你。谢谢! <3 – Robin

+0

第一个查询为了工作,您必须定义'comments'和'threads'之间的模型关系 – user4621032