binarycabin/laravel-author

A simple way to generate the model's author user when created

1.0.0 2017-11-14 11:39 UTC

This package is auto-updated.

Last update: 2024-11-06 10:39:03 UTC


README

A simple way to generate the model's author user when created

composer require binarycabin/laravel-author

This package adds a very simple trait to automatically save the user id who created this model.

Simply add the "\BinaryCabin\LaravelAuthor\Traits\HasAuthorUser;" trait to your model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Project extends Model
{

    use \BinaryCabin\LaravelAuthor\Traits\HasAuthorUser;

}

If your column name is not "author_user_id", simply add a new property to your model named "authorUserIdFieldName":

protected $authorUserIdFieldName = 'user_id';

This trait also provides a relationship:

dump($project->authorUser)

and query scope:

dump(\App\Project::byAuthorUser($userId)->get());