搜索结果的锚标签在Laravel 4中

时间:2016-01-21 21:49:45

标签: php mysql laravel eloquent

我创建了一个查询数据库中多个表的搜索。我正在尝试更改每个结果的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-&gt; id。所以,我在我的控制器中添加另一个选择 - &gt;选择('id','name'等)然后我得到未定义的属性$ link。

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

1 个答案:

答案 0 :(得分:1)

我非常确定,因为您需要在您的雄辩查询的选择部分添加其他列。

类似的东西:

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