PHP code example of sandaffo / laravel-model-validator

1. Go to this page and download the library: Download sandaffo/laravel-model-validator 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/ */

    

sandaffo / laravel-model-validator example snippets


namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    // define rules array
    public static $rules = [
        'title' => 'ant customize message
    public static $messages = [
        'title.

$p = new Post();
$p->title = "Test Post One";
$p->slug = "test-post-one";
$p->description = "Test post one description";

if ($p->isValid()) { // $p->isValid() returns true or false
    echo "Post is valid";
    $p->save();
} else {
    echo "Post is not valid";
    print_r($p->errors());
}

$p = new Post();
$p->title = "Test Post One";
$p->slug = "test-post-one";
$p->description = "Test post one description";

$p->isValid(); // true or false
$p->errors(); // []

$p->errorMessages(); // [] single dimensional array of all error messages

$p->getRules(); // to get the defined rules if you need somewhere
// [
//     "title" => "... }

$p->save(); // true

$p;
// App\Models\Post {
//     title: "Test Post One",
//     slug: "test-post-one",
//     description: "Test post one description",
//     updated_at: "2024-07-24 08:45:45",
//     created_at: "2024-07-24 08:45:45",
//     id: 1,
// }
bash
$ php artisan tinker