Skip to content

Instantly share code, notes, and snippets.

@ryangjchandler
Created July 1, 2020 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryangjchandler/8dd4c5cf1c19eb687e7f6131e3be3fc0 to your computer and use it in GitHub Desktop.
Save ryangjchandler/8dd4c5cf1c19eb687e7f6131e3be3fc0 to your computer and use it in GitHub Desktop.
<?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