Skip to content

Commit 2c95a01

Browse files
Chapter 3
1 parent 4bcfc75 commit 2c95a01

17 files changed

+623
-7
lines changed

app/Comment.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Comment extends Model
8+
{
9+
protected $guarded = ['id'];
10+
11+
public function ticket()
12+
{
13+
return $this->belongsTo('App\Ticket');
14+
}
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use App\Http\Requests\CommentFormRequest;
7+
use App\Comment;
8+
9+
class CommentsController extends Controller
10+
{
11+
public function newComment(CommentFormRequest $request)
12+
{
13+
$comment = new Comment(array(
14+
'post_id' => $request->get('post_id'),
15+
'content' => $request->get('content')
16+
));
17+
18+
$comment->save();
19+
20+
return redirect()->back()->with('status', 'Your comment has been created!');
21+
}
22+
}

app/Http/Controllers/PagesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public function about()
1818

1919
public function contact()
2020
{
21-
return view('contact');
21+
return view('tickets.create');
2222
}
2323
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use App\Http\Requests\TicketFormRequest;
7+
use App\Ticket;
8+
use Illuminate\Support\Facades\Mail;
9+
10+
class TicketsController extends Controller
11+
{
12+
/**
13+
* Display a listing of the resource.
14+
*
15+
* @return \Illuminate\Http\Response
16+
*/
17+
public function index()
18+
{
19+
$tickets = Ticket::all();
20+
return view('tickets.index', compact('tickets'));
21+
}
22+
23+
/**
24+
* Show the form for creating a new resource.
25+
*
26+
* @return \Illuminate\Http\Response
27+
*/
28+
public function create()
29+
{
30+
return view('tickets.create');
31+
}
32+
33+
/**
34+
* Store a newly created resource in storage.
35+
*
36+
* @param \Illuminate\Http\Request $request
37+
* @return \Illuminate\Http\Response
38+
*/
39+
public function store(TicketFormRequest $request)
40+
{
41+
$slug = uniqid();
42+
$ticket = new Ticket(array(
43+
'title' => $request->get('title'),
44+
'content' => $request->get('content'),
45+
'slug' => $slug
46+
));
47+
48+
$ticket->save();
49+
50+
$data = array(
51+
'ticket' => $slug,
52+
);
53+
54+
// Mail::send('emails.ticket', $data, function ($message) {
55+
// $message->from('[email protected]', 'Learning Laravel');
56+
//
57+
// $message->to('[email protected]')->subject('There is a new ticket!');
58+
// });
59+
60+
return redirect('/contact')->with('status', 'Your ticket has been created! Its unique id is: '.$slug);
61+
}
62+
63+
/**
64+
* Display the specified resource.
65+
*
66+
* @param int $id
67+
* @return \Illuminate\Http\Response
68+
*/
69+
public function show($slug)
70+
{
71+
$ticket = Ticket::whereSlug($slug)->firstOrFail();
72+
$comments = $ticket->comments()->get();
73+
return view('tickets.show', compact('ticket', 'comments'));
74+
}
75+
76+
/**
77+
* Show the form for editing the specified resource.
78+
*
79+
* @param int $id
80+
* @return \Illuminate\Http\Response
81+
*/
82+
public function edit($slug)
83+
{
84+
$ticket = Ticket::whereSlug($slug)->firstOrFail();
85+
return view('tickets.edit', compact('ticket'));
86+
}
87+
88+
/**
89+
* Update the specified resource in storage.
90+
*
91+
* @param \Illuminate\Http\Request $request
92+
* @param int $id
93+
* @return \Illuminate\Http\Response
94+
*/
95+
public function update($slug, TicketFormRequest $request)
96+
{
97+
$ticket = Ticket::whereSlug($slug)->firstOrFail();
98+
$ticket->title = $request->get('title');
99+
$ticket->content = $request->get('content');
100+
if($request->get('status') != null) {
101+
$ticket->status = 0;
102+
} else {
103+
$ticket->status = 1;
104+
}
105+
$ticket->save();
106+
return redirect(action('TicketsController@edit', $ticket->slug))->with('status', 'The ticket '.$slug.' has been updated!');
107+
108+
}
109+
110+
/**
111+
* Remove the specified resource from storage.
112+
*
113+
* @param int $id
114+
* @return \Illuminate\Http\Response
115+
*/
116+
public function destroy($slug)
117+
{
118+
$ticket = Ticket::whereSlug($slug)->firstOrFail();
119+
$ticket->delete();
120+
return redirect('/tickets')->with('status', 'The ticket '.$slug.' has been deleted!');
121+
}
122+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class CommentFormRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
'content'=> 'required|min:3',
28+
];
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class TicketFormRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
'title' => 'required|min:3',
28+
'content'=> 'required|min:10',
29+
];
30+
}
31+
}

app/Ticket.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Ticket extends Model
8+
{
9+
protected $fillable = ['title', 'content', 'slug', 'status', 'user_id'];
10+
11+
public function comments()
12+
{
13+
return $this->hasMany('App\Comment', 'post_id');
14+
}
15+
}

composer.lock

Lines changed: 67 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateTicketsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('tickets', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->string('title', 255);
19+
$table->text('content');
20+
$table->string('slug')->nullable();
21+
$table->tinyInteger('status')->default(1);
22+
$table->integer('user_id')->nullable();
23+
$table->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('tickets');
35+
}
36+
}

0 commit comments

Comments
 (0)