PHP code example of javoscript / laravel-macroable-models
1. Go to this page and download the library: Download javoscript/laravel-macroable-models library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
javoscript / laravel-macroable-models example snippets
// config/app.php
$providers = [
// ...
\Javoscript\MacroableModels\MacroableModelsServiceProvider::class,
// ...
];
// app/Providers/AppServiceProvider.php
// ...
use \Javoscript\MacroableModels\Facades\MacroableModels;
use \App\User;
// ...
public function boot()
{
MacroableModels::addMacro(User::class, 'sayHi', function() {
return 'Hi!';
});
}
// config/app.php
$providers = [
// ...
App\Providers\MacrosServiceProvider::class,
// ...
];
// app/Providers/MacrosServiceProvider.php
// ...
use \Javoscript\MacroableModels\Facades\MacroableModels;
use \App\User;
// ...
public function boot()
{
MacroableModels::addMacro(User::class, 'sayHi', function() {
return 'Hi!';
});
MacroableModels::addMacro(User::class, 'sayBye', function() {
return 'Bye bye';
});
}
MacroableModels::addMacro(\App\User::class, 'sayHi', function() { return "Hi!"; });
\App\User::first()->sayHi();
MacroableModels::addMacro(\App\User::class, 'say', function(string $something) { return $something; });
$user = \App\User::first();
$user->say("Hello world!");
MacroableModels::addMacro(\App\User::class, 'getId', function() { return $this->id; });
\App\User::first()->getId();
// 1
MacroableModels::addMacro(\App\User::class, 'posts', function() {
return $this->hasMany(App\Post::class);
});
$user = App\User::first();
$user->posts;
// null
// This will always return null, as the posts attribute wasn't defined
$user->posts()->get()
// This will correctly return the posts Eloquent collection
MacroableModels::addMacro(\App\User::class, 'greet', function() { return "Hi!"; });
\App\User::first()->greet();
// "Hi!"
MacroableModels::addMacro(\App\User::class, 'greet', function() { return "Hello human"; });
\App\User::first()->greet();
// "Hello human"
class Dog extends Illuminate\Database\Eloquent\Model
{
public function bark()
{
return "Woof!";
}
}
MacroableModels::addMacro(Dog::class, 'bark', function() { return "Miauuu!"; });
$dog = new Dog;
$dog->bark();
// "Woof!"
MacroableModels::removeMacro(\App\User::class, 'salute');
// false
MacroableModels::addMacro(\App\User::class, 'salute', function() { return "Hello!"; });
MacroableModels::removeMacro(\App\User::class, 'salute');
// true
MacroableModels::addMacro(\App\User::class, 'hi', function() { return "Hi!"; })
MacroableModels::addMacro(\App\Dog::class, 'hi', function() { return "Woof!"; })
MacroableModels::addMacro(\App\User::class, 'bye', function() { return "Bye bye"; })
MacroableModels::getAllMacros()
/*
[
"hi" => [
"App\User" => Closure() {#3362 …2},
"App\Dog" => Closure() {#3376 …2},
],
"bye" => [
"App\User" => Closure() {#3366 …2},
],
]
*/
MacroableModels::modelHasMacro(\App\User::class, 'salute');
// false
MacroableModels::addMacro(\App\User::class, 'salute', function() { return "Hi!"; });
MacroableModels::modelHasMacro(\App\User::class, 'salute');
// true
MacroableModels::addMacro(\App\User::class, 'hi', function() { return "Hi!"; });
MacroableModels::addMacro(\App\Dog::class, 'hi', function() { return "Woof!"; });
MacroableModels::modelsThatImplement('hi');
/*
[
"App\User",
"App\Dog",
]
*/
MacroableModels::addMacro(\App\User::class, 'say', function(String $something) { return $something; });
MacroableModels::addMacro(\App\User::class, 'sum', function(Integer $a, Integer $b) { return $a + $b; });
MacroableModels::macrosForModel(\App\User::class);
/*
[
"say" => [
"name" => "say",
"parameters" => [
ReflectionParameter {#3385
+name: "something",
position: 0,
typeHint: "string",
},
],
],
"sum" => [
"name" => "sum",
"parameters" => [
ReflectionParameter {#3357
+name: "a",
position: 0,
typeHint: "Integer",
},
ReflectionParameter {#3360
+name: "b",
position: 1,
typeHint: "Integer",
},
],
],
]
*/
php artisan make:provider MacrosServiceProvider