Awesome
tinker.macro
Evaluate Laravel code at build-time, via Laravel Tinker
Installation
Install babel-plugin-macros
(along with tinker.macro
) and add it to your babel config:
npm install --save-dev babel-plugin-macros tinker.macro
.babelrc:
{
"plugins": ["macros"]
}
That’s it!
Basic Usage
import tinker from 'tinker.macro'
let isDebug = tinker`config('app.debug')`
// ↓ ↓ ↓ ↓ ↓ ↓ ↓
let isDebug = true
If you are executing a single function call you can import the function like this:
import { config } from 'tinker.macro'
let isDebug = config('app.debug')
// ↓ ↓ ↓ ↓ ↓ ↓ ↓
let isDebug = true
There is also a convenient way to access App\*
class methods and properties:
import { Article } from 'tinker.macro'
let article = Article.where('id', 7).first()
// ↓ ↓ ↓ ↓ ↓ ↓ ↓
let article = {
id: 7,
title: 'Hello, world',
body: 'Lorem ipsum dolor sit amet.'
// etc.
}
This is the same as:
import tinker from 'tinker.macro'
let articles = tinker`App\\Article::where('id', 7)->first()`
More Examples
route function
import tinker, { route } from 'tinker.macro'
// these are equivalent
let articleRoute1 = route('article', { id: 1 })
let articleRoute2 = tinker`route('article', ['id' => 1])`
// ↓ ↓ ↓ ↓ ↓ ↓ ↓
let articleRoute1 = 'http://localhost/article/1'
let articleRoute2 = 'http://localhost/article/1'
Retrieving a list of named routes
import tinker from 'tinker.macro'
let routes = tinker`
$routes = [];
foreach (app()->routes->getRoutes() as $route) {
$name = $route->getName();
if ($name !== null) {
$uri = $route->uri;
$routes[$name] = ($uri === '/' ? '' : '/') . $uri;
}
}
$routes;
`
// ↓ ↓ ↓ ↓ ↓ ↓ ↓
let routes = {
home: '/',
blog: '/blog',
post: '/blog/{slug}'
}
Re-evaluating on change
If you are using webpack you can ensure that expressions are re-evaluated when updating your Laravel app. Add the loader – tinker.macro/webpack
– to your webpack configuration, like so:
module.exports = {
// ...
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'tinker.macro/webpack'
}
// ...
]
}
// ...
}