ciurinho's avatar

Dynamic URL as a JSON response

I'm looking at a website that uses JQuery UI Autocomplete that returns a JSON response:

{"id" : 1, "label" : "Amsterdam", "category" : "Location" , "subLabel" : "The Netherlands", "url" 
: "\/offices\/amsterdam"}

My question concerns the url part. Is it a dynamic url? Can I obtain this in Laravel?

0 likes
11 replies
ciurinho's avatar

@tomo_pongrac Thanks for the link, but I already have a working JQuery Autocomplete. My question is whether I can improve the way I redirect after selection by adding a "url" attribute to my object, like this website seems to do.

Right now I do this:

 select: function( event, ui ) {
        var category = ui.item.category;
        if(category == 'Restaurant') {
          location.href = 'restaurants/' + ui.item.id;
        } 
        if (category == 'City') {
            location.href = ui.item.label;
        }
      }
ciurinho's avatar

So I thought it would be easier to replace the conditionals with

location.href =  ui.item.url;

but without having to write each url manually. That's why I was wondering about the possibility of generating the url attribute dynamically.

tomopongrac's avatar

It is much better that you generate link on server before sending json.

Does json generate from data in database? you can store url in database...

ciurinho's avatar

Yes, I'm using a database. So where should I generate the url, in the controller? This is what I'm doing right now in my search controller

class SearchController extends Controller
{
 public function search(Request $request){
        $term = $request->input('term');
        $restaurants = Restaurant::where('label', 'LIKE', "$term%")->get();
        $collection = collect($restaurants);
        $cities   = City::where('label', 'LIKE', "$term%")->get();
        $merged = $collection->merge($cities);
            return response()->json($merged->all());
    }
}
tomopongrac's avatar

Yes, you can do that in controller if you don't use that url in other places.

ciurinho's avatar

@tomo_pongrac Can you give me a hint on how I should generate the url in the controller? I've been trying to loop over the array, generate the url and add it back to the array, but it just looks silly and the worst part is that it doesn't work.

ciurinho's avatar

I think I solved it.

 $restaurants = $restaurants->each(function($item, $key){
            $item["url"] = "restaurant/".$item->id;         
        });

Does this look like a good solution? What did you mean that I could store the url in the database? I think that I might prefer that solution but I can't think of a way to do it.

tomopongrac's avatar

@ciurinho

I think that when you save restaurant into database you can save url for that restaurant ...

but i think that your solution is good enough

ciurinho's avatar

Thanks for getting back at me and... sorry for dragging it, but I'm really curious about how I could save such values in the database. I tried to save the url in migrations, but I didn't further than

$table->string('url')->default("restaurant/"  ???); 

because I don't know how I could access the id. Is this what you meant or am I completely off here?

tomopongrac's avatar
Level 51

@ciurinho

in that solution you cant have default value because you dont know how url will look ... you can know id only when you create row in database so that can perform only on creating new restaurant, which you create, i guess, with form ... but know when i see how your url look method which you did is best solution ...

Please or to participate in this conversation.