Created
February 8, 2019 01:50
-
-
Save JordanDalton/c007ab3289ec287bbd2b0f7e476d205c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": [ | |
{ | |
"links": { | |
"api": { | |
"store": { | |
"url": "https://localhost/someresource", | |
"method": "POST", | |
"axios_method": "post" | |
} | |
}, | |
"ui": { | |
"show": "https://localhost/someresource", | |
} | |
} | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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]) | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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