PHP code example of nzesalem / lastus

1. Go to this page and download the library: Download nzesalem/lastus 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/ */

    

nzesalem / lastus example snippets


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddStatusFieldToPostsTable extends Migration
{
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            //...
            $table->tinyInteger('status')->default(0);
        });
        
        //...
    }
}

use Illuminate\Foundation\Auth\User as Authenticatable;
use Nzesalem\Lastus\Traits\LastusTrait;

class User extends Authenticatable
{
    use LastusTrait;

    // Key value pair of the statuses you want your model to have
    const STATUSES = [
        'UNVERIFIED' => 0,
        'ACTIVE' => 1,
        'SUSPENDED' => 2,
        'BLOCKED' => 3,
        'PENDING_APPROVAL' => 7,
    ];

    //...
}


$user = User::create([
    'name' => 'Salem Nzeukwu',
    'email' => '[email protected]',
    'password' => bcrypt('secret'),
    'status' => 'active', // This will be saved as '1' in the database
]);

echo $user->status; // This prints 'active'

// Sometime later
$user->status = 'pending-approval';
$user->save();

echo $user->status; // This now prints 'pending-approval'

$now = Carbon::now();

// Raw insert query
DB::table('users')->insert([
    'name' => 'Firstname Lastname',
    'email' => '[email protected]',
    'password' => bcrypt('secret'),
    'created_at' => $now,
    'updated_at' => $now,
    // Get the status code.
    'status' => User::getStatusCode('suspended'),
]);
// Raw select query
$user = User::whereRaw('status = ' . User::getStatusCode('suspended'))->first();

$user->status == 'suspended' // true

<select id="status" class="form-control" name="status" \User::statuses() as $status)
    <option value="{{ $status }}">{{ ucfirst($status) }}</option>
    @endforeach
</select>

print_r(Lastus::statuses(App\User::class));

@status('active')
    <p>This is only visible to users with an 'active' status.</p>
@endstatus

Route::group(['prefix' => 'dashboard', 'middleware' => ['status:active']], function() {
    Route::get('/', 'DashboardController@index');
});
sh
$ php artisan migrate