Skip to content

Commit ce64a35

Browse files
committed
laravel basic course
1 parent 34e56de commit ce64a35

File tree

8 files changed

+159
-120
lines changed

8 files changed

+159
-120
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 119 additions & 101 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/Http/Controllers/TaskController.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class TaskController extends Controller
99
{
1010
public function index()
1111
{
12-
$tasks = Task::latest()->get();
12+
$tasks = auth()->user()->tasks()->latest()->get();
1313

1414
return view('tasks.index', [
1515
'tasks' => $tasks
@@ -28,13 +28,17 @@ public function store()
2828
'body' => 'required'
2929
]);
3030

31-
$task = Task::create(request(['title', 'body']));
31+
$values = request(['title', 'body']);
32+
$values['user_id'] = auth()->id();
33+
34+
$task = Task::create($values);
3235

3336
return redirect('/tasks/'.$task->id);
3437
}
3538

3639
public function show(Task $task)
3740
{
41+
abort_unless(auth()->user()->owns($task), 403);
3842

3943
return view('tasks.show', [
4044
'task' => $task
@@ -43,13 +47,17 @@ public function show(Task $task)
4347

4448
public function edit(Task $task)
4549
{
50+
abort_unless(auth()->user()->owns($task), 403);
51+
4652
return view('tasks.edit', [
4753
'task' => $task
4854
]);
4955
}
5056

5157
public function update(Task $task)
5258
{
59+
abort_unless(auth()->user()->owns($task), 403);
60+
5361
request()->validate([
5462
'title' => 'required',
5563
'body' => 'required'
@@ -62,6 +70,8 @@ public function update(Task $task)
6270

6371
public function destroy(Task $task)
6472
{
73+
abort_unless(auth()->user()->owns($task), 403);
74+
6575
$task->delete();
6676

6777
return redirect('/tasks');

app/Task.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66

77
class Task extends Model
88
{
9-
protected $fillable = ['title', 'body'];
9+
protected $fillable = ['title', 'body', 'user_id'];
10+
11+
public function user() {
12+
return $this->belongsTo(User::class);
13+
}
1014
}

app/User.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,13 @@ class User extends Authenticatable
3636
protected $casts = [
3737
'email_verified_at' => 'datetime',
3838
];
39+
40+
public function owns(Task $task)
41+
{
42+
return auth()->id() == $task->user_id;
43+
}
44+
45+
public function tasks() {
46+
return $this->hasMany(Task::class);
47+
}
3948
}

database/migrations/2019_06_28_092731_create_tasks_table.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ public function up()
1515
{
1616
Schema::create('tasks', function (Blueprint $table) {
1717
$table->bigIncrements('id');
18+
$table->unsignedBigInteger('user_id');
1819
$table->string('title');
1920
$table->longText('body');
2021
$table->timestamps();
22+
23+
$table->foreign('user_id')->references('id')->on('users');
2124
});
2225
}
2326

resources/views/layouts/app.blade.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
<div class="collapse navbar-collapse" id="navbarSupportedContent">
3535
<!-- Left Side Of Navbar -->
3636
<ul class="navbar-nav mr-auto">
37-
37+
<li class="nav-item" >
38+
<a href="/tasks">Tasks</a>
39+
</li>
3840
</ul>
3941

4042
<!-- Right Side Of Navbar -->

routes/web.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,8 @@
1919

2020
Route::get('/projects', 'ProjectController@index');
2121

22-
Route::prefix('tasks')->middleware('auth')->group(function () {
23-
Route::get('/', 'TaskController@index');
24-
25-
Route::get('/create', 'TaskController@create');
26-
27-
Route::post('/', 'TaskController@store');
28-
29-
Route::get('/{task}', 'TaskController@show');
30-
31-
Route::get('/{task}/edit', 'TaskController@edit');
32-
33-
Route::put('/{task}', 'TaskController@update');
34-
35-
Route::delete('/{task}', 'TaskController@destroy');
36-
});
22+
23+
Route::resource('tasks', 'TaskController')->middleware('auth');
3724

3825

3926

0 commit comments

Comments
 (0)