Reputation: 113
I've created a search that is querying multiple tables in a database. I'm trying change the href for each result, but obviously they have different routes/controllers. I added a custom attribute in the query:
$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 . '%');
Which works when I union the queries with the rest. I then have a foreach loop in my view:
@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
But this gives me undefined property $r->id. So, I add in another select to my controller ->select('id', 'name' et al) and then I get undefined property $link.
I have no idea how to link each result to their relevant route/controller. How can I achieve this?
Upvotes: 1
Views: 72
Reputation: 701
I'm pretty sure it's because you need to add the other columns in the select portion of your eloquent query.
Something like:
selectRaw("id, name, enterprise.show as link")
Upvotes: 1