当原始查询具有值时,Laravel连接查询返回Null

时间:2015-11-06 16:23:36

标签: php mysql laravel-4 eloquent

我遇到了一个非常愚蠢的问题。

我试图找到类似日子的解决方案,但没有任何帮助,所以我找不到答案。

我希望从表中获得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

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

当我在navicat中执行查询(raw)时,它会返回: Output

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

但是,当我在" Laravel-eloquent-style"中重建此查询时,查询如下所示:

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;
}

希望有人可以帮助我!

1 个答案:

答案 0 :(得分: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');

如果您需要 - 被删除了&#39;不为空

->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');