【问题标题】:Anchor tags for search results in Laravel 4Laravel 4 中搜索结果的锚定标签
【发布时间】:2016-01-21 21:49:44
【问题描述】:

我创建了一个查询数据库中多个表的搜索。我正在尝试更改每个结果的 href,但显然它们有不同的路由/控制器。我在查询中添加了一个自定义属性:

$enterprise = DB::table('enterprise')
                    ->selectRaw("'enterprise.show' as link")
                    ->where('name', 'LIKE', '%' . $term . '%')
                    ->orWhere('description', 'LIKE', '%' . $term . '%');
$entertainment = DB::table('entertainment')
                    ->selectRaw("'entertainment.show' as link")
                    ->where('name', 'LIKE', '%' . $term . '%')
                    ->orWhere('description', 'LIKE', '%' . $term . '%');

当我将查询与其他查询合并时,这很有效。然后,我的视图中有一个 foreach 循环:

@foreach($results as $r)
                <div class="col-lg-3" style="padding-top: 20px">
                    <div class="hpanel">
                        <div class="panel-body text-center h-200">
                            <a style="color: #3c763d" href="{{route($r->link, $r->id)}}">
                                <h4 class="font-extra-bold no-margins text-success">{{Str::limit($r->name, 15)}}</h4></a>
                            <small>{{ Str::limit($r->description, 50) }}</small>
                        </div>
                        <div class="panel-footer">
                            <i class="fa fa-gbp"><p style="display: inline"> {{$r->cost}}</p></i>
                        </div>
                    </div>
                </div>
            @endforeach

但这给了我未定义的属性 $r->id。因此,我将另一个选择添加到我的控制器 ->select('id', 'name' et al) 中,然后我得到未定义的属性 $link。

我不知道如何将每个结果链接到它们的相关路由/控制器。我怎样才能做到这一点?

【问题讨论】:

    标签: php mysql laravel eloquent


    【解决方案1】:

    我很确定这是因为您需要在 eloquent 查询的选择部分中添加其他列。

    类似:

    selectRaw("id, name, enterprise.show as link")

    【讨论】:

    • 谢谢,它是 selectRaw("'enterprise.show' as link, id, name, description")。任何其他方式的变量都会引发约束冲突
    猜你喜欢
    • 2014-11-28
    • 2012-03-20
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多