PHP code example of sofa / eloquent-cascade

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

    

sofa / eloquent-cascade example snippets




namespace App;

use Sofa\EloquentCascade\CascadeDeletes;

class Product extends \Illuminate\Database\Eloquent\Model
{
    use CascadeDeletes;

    protected $deletesWith = ['types', 'photos'];


    root@578687bd11c8:/var/www/html# php artisan tinker
    Psy Shell v0.7.2 (PHP 7.0.3 — cli) by Justin Hileman
    >>> DB::enableQueryLog()            
    => null
    >>> App\Product::find(200)->delete()
    => true
    >>> DB::getQueryLog()
    => [
         [
           "query" => "select * from `products` where `products`.`id` = ? limit 1",
           "bindings" => [200],
         ],
         [
           "query" => "delete from `products` where `id` = ?",
           "bindings" => [200],
         ],
         [
           "query" => "delete from `product_types` where `product_types`.`product_id` = ? and `product_types`.`product_id` is not null",
           "bindings" => [200],
         ],
         [
           "query" => "delete from `photos` where `photos`.`product_id` = ? and `photos`.`product_id` is not null",
           "bindings" => [200],
         ],
       ]

    

    >>> App\Product::whereIn('id', [202, 203])->delete()
    => 2
    >>> DB::getQueryLog()
    => [
         [
           "query" => "select * from `products` where `id` in (?, ?)",
           "bindings" => [202, 203],
         ],
         [
           "query" => "delete from `product_types` where `product_types`.`product_id` in (?, ?)",
           "bindings" => [202, 203],
         ],
         [
           "query" => "update `photos` set `deleted_at` = ?, `updated_at` = ? where `photos`.`product_id` in (?, ?) and `photos`.`deleted_at` is null",
           "bindings" => [
             "2016-05-31 09:44:41",
             "2016-05-31 09:44:41",
             202,
             203,
           ],
         ],
         [
           "query" => "delete from `products` where `id` in (?, ?)",
           "bindings" => [202, 203],
         ],
       ]

    



namespace App;

use Sofa\EloquentCascade\CascadeDeletes;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends \Illuminate\Database\Eloquent\Model
{
    use SoftDeletes, CascadeDeletes;

    // related Photo model uses SoftDeletes as well, but Type does not
    protected $deletesWith = ['types', 'photos'];


    >>> App\Product::whereIn('id', [300, 301])->delete()
    => 2
    >>> DB::getQueryLog()
    => [
         [
           "query" => "select * from `products` where `id` in (?, ?) and `products`.`deleted_at` is null",
           "bindings" => [300, 301],
         ],
         [
           "query" => "delete from `product_types` where `product_types`.`product_id` in (?, ?)",
           "bindings" => [300, 301],
         ],
         [
           "query" => "update `photos` set `deleted_at` = ?, `updated_at` = ? where `photos`.`product_id` in (?, ?) and `photos`.`deleted_at` is null",
           "bindings" => [
             "2016-05-31 09:52:30",
             "2016-05-31 09:52:30",
             300,
             301,
           ],
         ],
         [
           "query" => "update `products` set `deleted_at` = ?, `updated_at` = ? where `id` in (?, ?) and `products`.`deleted_at` is null",
           "bindings" => [
             "2016-05-31 09:52:30",
             "2016-05-31 09:52:30",
             300,
             301,
           ],
         ],
       ]

    

    >>> App\Product::find(302)->forceDelete()
    => true
    >>> DB::getQueryLog()
    => [
         [
           "query" => "select * from `products` where `products`.`id` = ? and `products`.`deleted_at` is null limit 1",
           "bindings" => [302],
         ],
         [
           "query" => "delete from `products` where `id` = ?",
           "bindings" => [302],
         ],
         [
           "query" => "delete from `product_types` where `product_types`.`product_id` = ? and `product_types`.`product_id` is not null",
           "bindings" => [302],
         ],
         [
           "query" => "delete from `photos` where `photos`.`product_id` = ? and `photos`.`product_id` is not null",
           "bindings" => [302],
         ],
       ]