laravel-shift / blueprint

A code generation tool for Laravel developers.
MIT License
2.89k stars 273 forks source link

Add Foreign key column to Relationship in Model #523

Closed ManojKiranA closed 8 months ago

ManojKiranA commented 2 years ago

Synopsis:

While creating a model columns with custom column name (doesn't follow laravel convention). It doesn't add column name to relation.

So let's say i have following Yaml

Sample 1

models:
  Post:
    id
    content: text nullable
    created_by_id: id nullable bigInteger unsigned foreign:users.id
    timestamps
    softDeletes

Expected Behavior:

While generating Model relations It can automatically add created_by_id as second argument. And

public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(\App\Models\User::class,"created_by_id");
    }

Sample 2

models:
  Post:
    id
    content: text nullable
    created_by_id: id nullable bigInteger unsigned foreign:users.user_id
    timestamps
    softDeletes

Expected Behavior:

While generating Model relations It can automatically add created_by_id and as second argument as user_id

public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(\App\Models\User::class,"created_by_id","user_id");
    }
jasonmccreary commented 2 years ago

Feel free to submit a PR with the fix. Otherwise, I will patch this in a future live stream.

jasonmccreary commented 8 months ago

Finally got around to testing this, it seems to behave as expected. However, let me know if not.