Created
July 1, 2020 16:47
-
-
Save ryangjchandler/8dd4c5cf1c19eb687e7f6131e3be3fc0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
protected $hidden = ['published_at']; | |
static::creating(function (Post $post) { | |
$post->visible('published_at'); | |
Validator::validate($post->toArray(), static::$rules); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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