PHP code example of ka4ivan / laravel-model-releases
1. Go to this page and download the library: Download ka4ivan/laravel-model-releases 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/ */
ka4ivan / laravel-model-releases example snippets
return [
/**
* Models with relations for which slugs will be created using the command
*/
'models' => [
// \App\Models\Post::class => [
// 'relations' => [
// 'media',
// 'translations',
// ],
// ],
// \App\Models\Translations\PostTranslation::class => [],
// \App\Models\Media::class => [],
],
/**
* Release model
*/
'model' => \Ka4ivan\ModelReleases\Models\Release::class,
/**
* Number of days after which release data will be considered stale and will be purged.
*
* If the number of days is 0, data will not be purged.
*
* It is impossible to rollback to a purged release!
*/
'cleanup' => [
'outdated_releases_for_days' => 30,
],
];
use Illuminate\Database\Eloquent\SoftDeletes;
use Ka4ivan\ModelReleases\Models\Traits\HasReleases;
class Article extends Model
{
use SoftDeletes, // REQUIRED!!!
HasReleases;
}
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('slug')->nullable()->index();
$table->string('name')->nullable();
$table->longText('body')->nullable();
$table->string('status')->default(true);
$table->timestamps();
$table->softDeletes();
$table->releaseFields();
// $table->releaseUuidFields(); if the `id` field is a uuid
$table->uuid('category_id')->nullable()->index();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
};
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('articles', function (Blueprint $table) {
$table->softDeletes(); // If it wasn't there before
$table->releaseFields();
// $table->releaseUuidFields(); If the id field is a uuid
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('articles', function (Blueprint $table) {
$table->dropColumn('deleted_at'); // If it wasn't there before
$table->dropReleaseFields();
});
};
/**
* Release to which this model belongs
*
* @return BelongsTo
*/
public function release(): BelongsTo
{
return $this->BelongsTo(\ModelRelease::getReleaseModel());
}
/**
* A model that is not yet in release and is a draft of a model in release
*
* @return HasOne
*/
public function prerelease(): HasOne
{
return $this->hasOne(self::class, 'id', 'prerelease_id');
}
/**
* A model that is already fully released
*
* @return HasOne
*/
public function postrelease(): HasOne
{
return $this->hasOne(self::class, 'prerelease_id', 'id')->whereIn('release_id', \ModelRelease::getActiveReleasesIds());
}
/**
* The model that is in the release and is the original of the model that is not in the release
*
* @return BelongsTo
*/
public function origin(): BelongsTo
{
return $this->belongsTo(self::class, 'id', 'prerelease_id');
}