Skip to content

Commit

Permalink
Adding user_id field to the table
Browse files Browse the repository at this point in the history
  • Loading branch information
PovilasKorop committed Apr 26, 2022
1 parent 8ac31d5 commit cc5b4a4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public function create()

public function store(StoreProjectRequest $request)
{
Project::create($request->validated());
Project::create($request->validated()
// + ['user_id' => auth()->id()]
);

return redirect()->route('projects.index');
}
Expand Down
11 changes: 10 additions & 1 deletion app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,14 @@ class Project extends Model
{
use HasFactory;

protected $fillable = ['name'];
protected $fillable = ['name', 'user_id'];

protected static function boot()
{
parent::boot();

self::creating(function($model) {
$model->user_id = auth()->id();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('projects', function (Blueprint $table) {
$table->foreignId('user_id')->constrained();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('projects', function (Blueprint $table) {
//
});
}
};

0 comments on commit cc5b4a4

Please sign in to comment.