PHP code example of vits / laravel-save-relationships
1. Go to this page and download the library: Download vits/laravel-save-relationships 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/ */
vits / laravel-save-relationships example snippets
use Vits\LaravelSaveRelationships\SaveRelationships;
class Author extends Model
{
use SaveRelationships;
protected $saveRelationships = 'books';
public function books(): HasMany
{
return $this->hasMany(Book::class);
}
}
...
$author->books = [1, 2, 3];
$author->save();
protected $saveRelationships = 'books,series';
// or
protected $saveRelationships = ['books', 'series'];
protected $saveRelationships = ['books' => 'saveBooks'];
protected function saveBooks($books)
{
//...
}