PHP code example of nhalstead / transferable

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

    

nhalstead / transferable example snippets




namespace App\Models;

use nhalstead\Transferable\Interfaces\NoDanglingRelationships;
use nhalstead\Transferable\Traits\TransferableRelationship;
use Illuminate\Database\Eloquent\Model;

class User extends Model implements NoDanglingRelationships
{
	use TransferableRelationship;

	protected $transferable = [
		"items"
	];

	public function items()
	{
		return $this->hasMany(Items::class);
	}
  
}



$oldUser = User::find(1);
$newUser = User::find(2);

// Doing `delete()` will trigger an exception so you need
//  to transfer any items that are connected to another model.

// Transfer relationships to another item.
$oldUser->transferTo($newUser); // Returns the total rows changed.

// Bob's your uncle, now oldUser can be deleted.
$oldUser->delete();



// Return the number of transferable items attached to this model
$newUser->countTransferable();

// Return boolean if it would have any dangling relationships if deleted.
// The false param tells it not to throw an Exception
$newUser->checkDangling(false);