PHP code example of glaivepro / ajaxable

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

    

glaivepro / ajaxable example snippets


use Illuminate\Database\Eloquent\Model;
use GlaivePro\Ajaxable\Traits\Ajaxable;

class Article extends Model
{
    use Ajaxable;
	//
}

public function allowAjaxableTo(string $action) : bool
{
	if ('create' == $action)
	{
		request()->validate([
			'name' => 'update', 'delete', 'retrieve', 'list', ];
	
	if (in_array($action, $allowedActionsForAuthorized))
		return auth()->check();
	
	return false;
}

respondAfterDelete(bool $success)
{
	if ($success)
		return [
			'success' => 1,
			'deleted_on' => \Carbon\Carbon::now()->format('M d');
		];
		
	return 'Deletion was refused because of reasons.';
}

public function respondAfter(string $action, bool $success)
{
	return ['success' => 'uncertain'];
}

protected static function boot()
{
    parent::boot();
    	
    static::deleting(function($tag) {
		if ($tag->articles()->count())
			return false;
		
		$tag->synonyms()->detach();
    });
}

public function allowAjaxableTo(string $action)
{
	if (!Gate::allows($action, $this))
		return false;
	
	if ('create' == request()->action 
	     && isset(request()->attributes['user_id'])
		 && 'self' == request()->attributes['user_id'] )
	{
		// you can tinker with request here
		request()->attributes['user_id'] = auth()->id();
		
		// and you can also work with an object as it's already instantiated
		$object->updatedByJohn = 'probably';
	}
}
html
<meta name="csrf-token" content="{{ csrf_token() }}">

<label>
	Title
	{{$article->editor('title')}}
</label>

<!-- provide jQuery somewhere please! -->
@