Laravel起步二:MVC

模型

app/Task.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

namespace App;

use App\User;
use Illuminate\Database\Eloquent\Model;

class Task extends Model
{

/**
* The attributes that are mass assignable.
*
* @var array
*/

protected $fillable = ['name'];

/**
* The attributes that should be cast to native types.
*
* @var array
*/

protected $casts = [
'user_id' => 'int',
];

/**
* Get the user that owns the task.
*/

public function user()
{

return $this->belongsTo(User::class);
}
}

视图

resources/views/tasks/index.blade.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@extends('layouts.app')

@section('content')
<div class="container">
<div class="col-sm-offset-2 col-sm-8">
<div class="panel panel-default">
<div class="panel-heading">
New Task
</div>

<div class="panel-body">
<!-- Display Validation Errors -->
@include('common.errors')

<!-- New Task Form -->
<form action="{{ url('task') }}" method="POST" class="form-horizontal">
{{ csrf_field() }}

<!-- Task Name -->
<div class="form-group">
<label for="task-name" class="col-sm-3 control-label">Task</label>

<div class="col-sm-6">
<input type="text" name="name" id="task-name" class="form-control" value="{{ old('task') }}">
</div>
</div>

<!-- Add Task Button -->
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button type="submit" class="btn btn-default">
<i class="fa fa-btn fa-plus"></i>Add Task
</button>
</div>
</div>
</form>
</div>
</div>

<!-- Current Tasks -->
@if (count($tasks) > 0)
<div class="panel panel-default">
<div class="panel-heading">
Current Tasks
</div>

<div class="panel-body">
<table class="table table-striped task-table">
<thead>
<th>Task</th>
<th>&nbsp;</th>
</thead>
<tbody>
@foreach ($tasks as $task)
<tr>
<td class="table-text"><div>{{ $task->name }}</div></td>

<!-- Task Delete Button -->
<td>
<form action="{{url('task/' . $task->id)}}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}

<button type="submit" id="delete-task-{{ $task->id }}" class="btn btn-danger">
<i class="fa fa-btn fa-trash"></i>Delete
</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@endif
</div>
</div>
@endsection

控制器

app/Http/Controllers/TaskController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Task;
use App\Repositories\TaskRepository;

class TaskController extends Controller
{

/**
* The task repository instance.
*
* @var TaskRepository
*/

protected $tasks;

/**
* Create a new controller instance.
*
* @param TaskRepository $tasks
* @return void
*/

public function __construct(TaskRepository $tasks)
{

$this->middleware('auth');
$this->tasks = $tasks;
}

/**
* Display a list of all of the user's task.
*
* @param Request $request
* @return Response
*/

public function index(Request $request)
{

// $tasks = Task::where('user_id', $request->user()->id)->get();
return view('tasks.index', [
'tasks' => $this->tasks->forUser($request->user()),
]);
}

/**
* Create a new task.
*
* @param Request $request
* @return Response
*/

public function store(Request $request)
{

$this->validate($request, [
'name' => 'required|max:255',
]);
$request->user()->tasks()->create([
'name' => $request->name,
]);
return redirect('/tasks');
}

/**
* Destroy the given task.
*
* @param Request $request
* @param Task $task
* @return Response
*/

public function destroy(Request $request, Task $task)
{

$this->authorize('destroy', $task);
$task->delete();
return redirect('/tasks');
}
}

路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php

use App\Task;
use Illuminate\Http\Request;

Route::group(['middleware' => ['web']], function () {

// Task Routes...
Route::get('/tasks', 'TaskController@index');
Route::post('/task', 'TaskController@store');
Route::delete('/task/{task}', 'TaskController@destroy');

Route::auth();

});

效果展示

IMG_20160624_4

IMG_20160624_5

IMG_20160624_6

IMG_20160624_7

参考资料

  1. Laravel中文文档
  2. laravel/quickstart-intermediate
八宝粥 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!