PHP code example of pillar-science / laravel-multi-morph
1. Go to this page and download the library: Download pillar-science/laravel-multi-morph 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/ */
pillar-science / laravel-multi-morph example snippets
class MyFile extends Model
{
public function resource()
{
return $this->morphTo();
}
}
Schema::table('files', function (Blueprint $table) {
$table->string('resource_relationship')->after('resource_type')->nullable();
});
use PillarScience\LaravelMultiMorph\Eloquent\Concerns\HasMorphByRelationships;
/**
* refered as MorphedOneMany Model
*/
class User extends Model
{
use HasMorphByRelationships;
public function profilePicture()
{
return $this->morphOneRelationship(MyFile::class, 'resource');
}
public function resume()
{
return $this->morphOneRelationship(MyFile::class, 'resource');
}
}
use PillarScience\LaravelMultiMorph\Eloquent\Concerns\HasMorphByRelationships;
/**
* refered as MorphedTo Model
*/
class MyFile extends Model
{
use HasMorphByRelationships;
public function resource()
{
return $this->morphTo();
}
}
// $user is a User model
$file = new MyFile();
$file->resource()->associate($user, 'resume');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.