Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
protected $hidden = ['published_at'];
static::creating(function (Post $post) {
$post->visible('published_at');
Validator::validate($post->toArray(), static::$rules);
});
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Post extends Model
{
public static function boot()
{
parent::boot();
static::creating(function (Post $post) {
if (! $post->slug) {
$post->slug = Str::slug($post->title);
}
});
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Validator;
class Post extends Model
{
public static $rules = [
'title' => 'required|string|max:255',
'slug' => 'required|string',
'excerpt' => 'nullable|string|max:160',
'published_at' => 'nullable|date',
];
public static function boot()
{
parent::boot();
static::creating(function (Post $post) {
Validator::validate($post->toArray(), static::$rules);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment