Skip to content

Instantly share code, notes, and snippets.

@JordanDalton
Created February 8, 2019 01:50
Show Gist options
  • Save JordanDalton/c007ab3289ec287bbd2b0f7e476d205c to your computer and use it in GitHub Desktop.
Save JordanDalton/c007ab3289ec287bbd2b0f7e476d205c to your computer and use it in GitHub Desktop.
// Let vue dynamically adjust the axios request accordingly
axios[record.api.store.axios_method](record.api.store.url, this.data).then((response)=>{})
// Or in your component you can link to the item
<a :href="order.links.show">View Record</a>
{
"data": [
{
"links": {
"api": {
"store": {
"url": "https://localhost/someresource",
"method": "POST",
"axios_method": "post"
}
},
"ui": {
"show": "https://localhost/someresource",
}
}
}
]
}
<?php
/**
* Return the info for a given route.
*
* @param $name
* @param $parameters
*
* @return array
*
* @throws Exception
*/
function named_route_info($name, $parameters = []) : array
{
$info = app('router')->getRoutes()->getByName($name);
if(! $info) throw new Exception("Unable to locate route by alias '{$name}'");
return [
'url' => route($name, $parameters),
'method' => $info->methods[0],
'axios_method' => strtolower($info->methods[0])
];
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class Something extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'links' => [
'api' => [
'store' => named_route_info('some.named.route.store', $this)
],
'ui' => [
'show' => route('some.named.route.show', $this)
]
]
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment