PHP code example of schenke-io / laravel-relation-manager
1. Go to this page and download the library: Download schenke-io/laravel-relation-manager 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/ */
schenke-io / laravel-relation-manager example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class User extends Model
{
public function posts(): HasMany
{
return $this->hasMany(Post::class);
}
}
use SchenkeIo\LaravelRelationManager\Attributes\Relation;
use SchenkeIo\LaravelRelationManager\Enums\EloquentRelation as RelationEnum;
class User extends Model
{
#[Relation(RelationEnum::hasMany, Post::class)]
public function posts()
{
return $this->hasMany(Post::class);
}
}
#[Relation(RelationEnum::hasMany, Comment::class, addReverse: true)]
public function comments()
{
return $this->hasMany(Comment::class);
}
use SchenkeIo\LaravelRelationManager\Attributes\Relation;
use SchenkeIo\LaravelRelationManager\Enums\EloquentRelation as RelationEnum;
class User extends Model
{
#[Relation(RelationEnum::noRelation)]
public function internalMethod()
{
// this will be ignored by the scanner
}
}
use SchenkeIo\LaravelRelationManager\Phpunit\RelationTestTrait;
class ModelRelationTest extends TestCase
{
use RelationTestTrait;
public function test_user_has_many_posts()
{
$this->assertModelHasMany(User::class, Post::class);
}
}
it('has the correct relations', function () {
expect(User::class)->toHasMany(Post::class);
expect(Post::class)->toBelongsTo(User::class);
});
namespace Tests\Feature;
use SchenkeIo\LaravelRelationManager\Phpunit\AbstractRelationTest;
class RelationshipTest extends AbstractRelationTest
{
/**
* Optional: Path to your relationships file.
* Defaults to the one found by PathResolver (.relationships.json).
*/
protected ?string $relationshipJsonPath = null;
/**
* Optional: Directory containing your models.
* Defaults to the one defined in .relationships.json config
* or 'app/Models'.
*/
protected ?string $modelDirectory = null;
/**
* Set to true for Strict Mode.
*/
protected bool $strict = true;
}
use SchenkeIo\LaravelRelationManager\Pest\RelationTestBridge;
RelationTestBridge::all(
relationshipJsonPath: null, // optional, defaults to PathResolver
modelDirectory: null, // optional, defaults to config or 'app/Models'
strict: true // recommended, defaults to false
);
bash
php artisan relation:extract
bash
php artisan relation:draw
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.