Laravel奇怪的查询并不想工作

时间:2015-11-01 17:01:47

标签: php mysql laravel-4

我试图获得表comments的用户名,但它并没有那么好用。

首先,当我执行查询

select `comments`.`username` from `comments` inner join `threads` on `comments`.`tid` = `threads`.`tid` where `comments`.`deleted_at` is null and `threads`.`cid` = 4 order by `comments`.`posted_at` desc limit 1

它确实返回了返回所需的值。

提醒一下,此查询中的数字4代表int();

现在我尝试了很多东西才能在Laravel中正确地完成这项工作,但它并没有......

所以我的视图中有一个foreach循环,这需要显示comments.username的用户名。

我该怎么办呢。

每当我尝试某些东西时,我就会把阵列变成刺痛,或其他东西。

使用id调用cid的{​​{1}}。

这是我的代码:

$categorie->id

我真的不知道如何解决这个问题。

修改

Controller(ForumController):

@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-query here">query here</a>
                  @else
                  <b>-</b>
                  @endif
                </div>
              </td>
            </tr>
            @endif
            @endforeach

型号:

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

class Comment extends Eloquent
{
    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

    protected $table = 'comments';

    public $timestamps = false;

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

}

我这样做的错误:

class Thread extends Eloquent
{
    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

    protected $table = 'threads';

    public $timestamps = false;
}

这只会返回{{ DB::statement('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') }} 但我需要1

当我这样做时:

username

错误:

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

当我使用它时:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'username' in field list is ambiguous (SQL: select `username` from `comments` inner join `threads` on `comments`.`tid` = `threads`.`tid` where `comments`.`deleted_at` is null and `threads`.`cid` = 3 order by `comments`.`posted_at` desc limit 1)

它不会返回任何错误,但它也没有显示正确的值?

0 个答案:

没有答案