Php Laravel奇怪的查询不';我不想工作
我正在尝试获取表Php Laravel奇怪的查询不';我不想工作,php,mysql,laravel-4,Php,Mysql,Laravel 4,我正在尝试获取表注释的用户名,但效果不太好 首先,当我执行查询时 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 它确实返回
注释的用户名,但效果不太好
首先,当我执行查询时
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()
现在,我尝试了很多东西,以使这项工作适当地在拉威尔,但它不
因此,我的视图中有一个foreach
循环,需要显示comments.username
中的用户名
我怎样才能做到这一点
每次我尝试某样东西,我都会感到刺痛,或者其他什么东西
使用$categorie->id
调用cid
的id
这是我的代码:
@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
型号:
执行此操作时出现的错误:
{{ 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') }}
{{Comment::join('threads', 'comments.tid', '=', 'threads.tid')->where('threads.cid', '=', $categorie->id)->orderBy('comments.posted_at', 'DESC')->limit(1)->pluck('username')}}
这只返回1
,但我需要用户名
当我这样做时:
{{ 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') }}
{{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)
当我使用此选项时:
{{Comment::join('threads', 'comments.tid', '=', 'threads.tid')->where('threads.cid', '=', $categorie->id)->orderBy('comments.posted_at', 'DESC')->first()->pluck('username')}}
它不会返回任何错误,但也不会显示正确的值?粘贴控制器和模型代码,以及如果您正在获取,您将获得的错误。我已更新我的问题。粘贴控制器和模型代码,以及如果您正在获取,您将获得的错误。我已更新我的问题。
{{Comment::join('threads', 'comments.tid', '=', 'threads.tid')->where('threads.cid', '=', $categorie->id)->orderBy('comments.posted_at', 'DESC')->first()->pluck('username')}}