0

I'm stuck with a really stupid query.

I tried to find the solution for like days, but nothing helped and so I don't find the answer.

I want to get the newest reaction from a table, to display the username.

First of all, these are my Database schemes:

comments (Has a model Comment) Comments

threads (Has a model Thread) Threads

So My raw query looks like this:

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() of $categorie->id returns int(3) where the 3 stands for the categorie number.

When I execute the query (raw) in navicat, it returns this: Output

What is good, since that is the corract value it needs to return.

However when I rebuild this query in "Laravel-eloquent-style", the query looks like this:

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

Be aware of that the query is build in a foreach loop and has an if-statement before. But that will be sown later on.

This does return nothing.

When I inspect elements, I just got nothing.

I tried the DB::select(DB::raw('query')), but that doesn't work either.

I render my page in the 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);
    }

This works fine, and the view looks like this:

@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

The strangest part is that this:

{{ \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') }}

Just works. Like nothing is wrong with that, when I call that item, it returns the correct value.

My Models are just normal;

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

}

And

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

class Thread extends Eloquent
{
    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

    protected $table = 'threads';

    public $timestamps = false;
}

Hope someone can help me out!

2
  • can you var_dump(DB::getQueryLog()) after you run the query, we can see the difference in raw queries Commented Nov 6, 2015 at 16:43
  • laravel.io/bin/2W7QN Is what I get :) (From that query) Commented Nov 6, 2015 at 17:01

1 Answer 1

1

Try something like this

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

If you need - were 'deleted_at' not null

->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');
Sign up to request clarification or add additional context in comments.

2 Comments

The second code works like a charm. (After removing some typo's), the first code will I check too. If it works, I'll let you know in a comment. Thanks! <3
The first query in order to work, you have to define the model relationship between 'comments' and 'threads'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.