VentureCraft / revisionable

Easily create a revision history for any laravel model
http://twitter.com/duellsy
MIT License
2.55k stars 349 forks source link

Struggling to get _id ref / formatting working #84

Closed SuperlativeEntity closed 10 years ago

SuperlativeEntity commented 10 years ago

Hi Chris

I have the following migration:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateTables extends Migration {

    public function up()
    {
        Schema::create('person_types', function(Blueprint $table)
        {
            $table->increments('id');
            $table->timestamps();
            $table->string('description');
        });

        Schema::create('people', function(Blueprint $table)
        {
            $table->increments('id');
            $table->unsignedInteger('person_type_id');
            $table->foreign('person_type_id')->references('id')->on('person_types');
            $table->string('first_name');
            $table->string('last_name');
            $table->string('id_number', 13)->unique();
            $table->date('birth_date')->nullable();
            $table->unsignedInteger('age')->nullable();
            $table->string('email')->unique();
            $table->string('mobile', 10)->unique();
            $table->text('postal_address')->nullable();
            $table->text('physical_address')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

    }

    public function down()
    {
        Schema::drop('people');
        Schema::drop('person_types');
    }

}

and the following model:

use Watson\Validating\ValidatingTrait; // Model Validation
use Illuminate\Database\Eloquent\SoftDeletingTrait; // Soft Deletes
use Venturecraft\Revisionable\RevisionableTrait; // Model Auditing

class Person extends Eloquent
{
    use SoftDeletingTrait,RevisionableTrait,ValidatingTrait;

    protected $softDelete = true;
    protected $guarded = array();

    protected $rules = [
        'first_name'    => 'required',
        'last_name'     => 'required',
        'id_number'     => 'required|numeric|digits:13|id_number|unique:people',
        'mobile'        => 'required|numeric|digits:10|unique:people',
        'email'         => 'required|email|unique:people',
        'age'           => 'numeric|between:1,120',
        'birth_date'    => 'date|date_past',
        'person_type_id'=> 'required|numeric'
    ];

    /**
     * Person Type: One to One Relationship
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function personType()
    {
        return $this->belongsTo('PersonType');
    }

    /**
     * Scope: Sort by Last Name Ascending
     *
     * @param $query
     * @return mixed
     */
    public function scopeLastNameAscending($query)
    {
        return $query->orderBy('last_name','ASC');
    }

}

However, I cannot get the foreign key description to display for the person_type_id, nor can I change the field format displayed. (I tried the $revisionFormattedFieldName. Also tried to do the accessor / mutator thing to no avail (copied and modified the examples from laravel's docs))

Any idea what I could be doing wrong?

image

duellsy commented 10 years ago

You can use the identifiebleName method for this, add the following to your PersonType model:

public function identifiableName()
{
    return $this->description;
}

(assuming you have a PersonType model, if not, just make one and pop this in it)

SuperlativeEntity commented 10 years ago

Worked. thanks man.