beaumind / eloquent-complement by beaumind

add some usable features to laravel eloquent
15
1
0
Package Data
Maintainer Username: beaumind
Maintainer Contact: a.hassani.sbu@gmail.com (A.Hassani)
Package Create Date: 2017-01-08
Package Last Update: 2017-01-08
Language: PHP
License: MIT
Last Refreshed: 2024-04-27 03:02:18
Package Statistics
Total Downloads: 15
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 0
Total Forks: 0
Total Open Issues: 0

eloquent-complement

add some usable features to laravel eloquent for laravel 5+.

Installation

Simply Run the Composer require comand.

composer require beaumind/eloquent-complement
use Beaumind\EloquentComplement\EloquentComplement;

class Question extends Model
{
    use EloquentComplement;
    
    public function user()
    {
        return $this->belongsTo('User');
    }
    
    public function answers()
    {
        return $this->hasMany('Answer');
    }

}
class Answer extends Model
{
    ...
}
class User extends Model
{
    ...
}

Save Associated models

you can now save question and related models in one step. it is atomic and it will role back on failure. also it fill foreign keys automatically.

$question['body'] = 'some question body';

$question['user']['name'] = 'joe';
$question['user']['username'] = 'joe_m';
...

$question['answers']['body'] = 'some answer body';
$question['answers']['is_correct'] = true;

now save in database.

(New Question())->saveAssociated($question, ['user','answers']);