PHP code example of robotsinside / laravel-deletable
1. Go to this page and download the library: Download robotsinside/laravel-deletable 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/ */
robotsinside / laravel-deletable example snippets
/*
* Package Service Providers...
*/
\RobotsInside\DeletableServiceProvider::class,
namespace App;
use Illuminate\Database\Eloquent\Model;
use RobotsInside\Deletable\Deletable;
class Post extends Model
{
use Deletable, SoftDeletes;
protected function deletableConfig(): array
{
return [
'relations' => [
'authors',
]
]
}
public function authors()
{
return $this->belongsToMany(Author::class);
}
}
namespace App\Http\Controllers;
use App\Post;
use RobotsInside\Deletable\Requests\DeletableRequest;
class PostController extends Controller
{
/**
* Remove the specified resource from storage.
*
* @param DeletableRequest $request
* @param Post $post
* @return \Illuminate\Http\Response
*/
public function destroy(DeletableRequest $request, Post $post)
{
$post->delete();
return redirect()->route('posts.index');
}
}
<div>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</div>
// Output: This Post has one or more Likes.
namespace App\Http\Requests;
use RobotsInside\Deletable\Requests\DeletableRequest;
class DeletePostRequest extends DeletableRequest
{
protected function getRouteModel()
{
return 'post';
}
}
namespace App\Http\Controllers;
use App\Http\Requests\DeletePostRequest;
class PostContoller extends Controller
{
...
/**
* Remove the specified resource from storage.
*
* @param App\Http\Requests\DeletePostRequest;
* @param App\Post $post
* @return \Illuminate\Http\Response
*/
public function destroy(DeletePostRequest $request, Post $post)
{
$post->delete();
return back();
}
}
namespace App\Models\Post;
use App\Models\Author;
use App\Models\Like;
use Illuminate\Database\Eloquent\Model;
use RobotsInside\Deletable\Deletable;
class Post extends Model
{
use Deletable;
public function deletableValidationMessage($model)
{
switch ($model) {
case Like::class:
return 'Posts with likes cannot be deleted.';
break;
case Author::class:
return 'Posts written by authors cannot be deleted.';
break;
default:
return 'This model cannot be deleted.';
break;
}
}
...
use App\Post;
$post = Post::create(['title' => 'My post']);
$author = Author::create(['name' => 'Billy Bob']);
$post->authors()->save($author);
Post::find(1)->safeDelete(); // My Post and Billy Bob will be deleted.
namespace App;
use Illuminate\Database\Eloquent\Model;
use RobotsInside\Deletable\Deletable;
class Post extends Model
{
use Deletable, SoftDeletes;
protected function deletableConfig()
{
return [
'mode' => 'custom',
'handler' => 'myHandler',
'relations' => [
'authors'
]
];
}
public function authors()
{
return $this->belongsToMany(Author::class);
}
public function myHandler()
{
app('log')->info('Unsafe delete of ' . __CLASS__);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.