From 2c95a01693e08036c3f7c83fa9b74040b902bf44 Mon Sep 17 00:00:00 2001 From: Learning Laravel Date: Wed, 3 Oct 2018 01:16:10 +0700 Subject: [PATCH] Chapter 3 --- app/Comment.php | 15 +++ app/Http/Controllers/CommentsController.php | 22 ++++ app/Http/Controllers/PagesController.php | 2 +- app/Http/Controllers/TicketsController.php | 122 ++++++++++++++++++ app/Http/Requests/CommentFormRequest.php | 30 +++++ app/Http/Requests/TicketFormRequest.php | 31 +++++ app/Ticket.php | 15 +++ composer.lock | 72 ++++++++++- ...2018_10_01_180148_create_tickets_table.php | 36 ++++++ ...018_10_02_142926_create_comments_table.php | 35 +++++ resources/views/emails/ticket.blade.php | 14 ++ resources/views/emails/welcome.blade.php | 14 ++ resources/views/tickets/create.blade.php | 48 +++++++ resources/views/tickets/edit.blade.php | 52 ++++++++ resources/views/tickets/index.blade.php | 45 +++++++ resources/views/tickets/show.blade.php | 68 ++++++++++ routes/web.php | 9 +- 17 files changed, 623 insertions(+), 7 deletions(-) create mode 100644 app/Comment.php create mode 100644 app/Http/Controllers/CommentsController.php create mode 100644 app/Http/Controllers/TicketsController.php create mode 100644 app/Http/Requests/CommentFormRequest.php create mode 100644 app/Http/Requests/TicketFormRequest.php create mode 100644 app/Ticket.php create mode 100644 database/migrations/2018_10_01_180148_create_tickets_table.php create mode 100644 database/migrations/2018_10_02_142926_create_comments_table.php create mode 100644 resources/views/emails/ticket.blade.php create mode 100644 resources/views/emails/welcome.blade.php create mode 100644 resources/views/tickets/create.blade.php create mode 100644 resources/views/tickets/edit.blade.php create mode 100644 resources/views/tickets/index.blade.php create mode 100644 resources/views/tickets/show.blade.php diff --git a/app/Comment.php b/app/Comment.php new file mode 100644 index 0000000..5b4f806 --- /dev/null +++ b/app/Comment.php @@ -0,0 +1,15 @@ +belongsTo('App\Ticket'); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/CommentsController.php b/app/Http/Controllers/CommentsController.php new file mode 100644 index 0000000..539fee8 --- /dev/null +++ b/app/Http/Controllers/CommentsController.php @@ -0,0 +1,22 @@ + $request->get('post_id'), + 'content' => $request->get('content') + )); + + $comment->save(); + + return redirect()->back()->with('status', 'Your comment has been created!'); + } +} diff --git a/app/Http/Controllers/PagesController.php b/app/Http/Controllers/PagesController.php index a71ce60..c811b85 100644 --- a/app/Http/Controllers/PagesController.php +++ b/app/Http/Controllers/PagesController.php @@ -18,6 +18,6 @@ public function about() public function contact() { - return view('contact'); + return view('tickets.create'); } } \ No newline at end of file diff --git a/app/Http/Controllers/TicketsController.php b/app/Http/Controllers/TicketsController.php new file mode 100644 index 0000000..7c5f870 --- /dev/null +++ b/app/Http/Controllers/TicketsController.php @@ -0,0 +1,122 @@ + $request->get('title'), + 'content' => $request->get('content'), + 'slug' => $slug + )); + + $ticket->save(); + + $data = array( + 'ticket' => $slug, + ); + +// Mail::send('emails.ticket', $data, function ($message) { +// $message->from('yourEmail@domain.com', 'Learning Laravel'); +// +// $message->to('yourEmail@domain.com')->subject('There is a new ticket!'); +// }); + + return redirect('/contact')->with('status', 'Your ticket has been created! Its unique id is: '.$slug); + } + + /** + * Display the specified resource. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function show($slug) + { + $ticket = Ticket::whereSlug($slug)->firstOrFail(); + $comments = $ticket->comments()->get(); + return view('tickets.show', compact('ticket', 'comments')); + } + + /** + * Show the form for editing the specified resource. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function edit($slug) + { + $ticket = Ticket::whereSlug($slug)->firstOrFail(); + return view('tickets.edit', compact('ticket')); + } + + /** + * Update the specified resource in storage. + * + * @param \Illuminate\Http\Request $request + * @param int $id + * @return \Illuminate\Http\Response + */ + public function update($slug, TicketFormRequest $request) + { + $ticket = Ticket::whereSlug($slug)->firstOrFail(); + $ticket->title = $request->get('title'); + $ticket->content = $request->get('content'); + if($request->get('status') != null) { + $ticket->status = 0; + } else { + $ticket->status = 1; + } + $ticket->save(); + return redirect(action('TicketsController@edit', $ticket->slug))->with('status', 'The ticket '.$slug.' has been updated!'); + + } + + /** + * Remove the specified resource from storage. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function destroy($slug) + { + $ticket = Ticket::whereSlug($slug)->firstOrFail(); + $ticket->delete(); + return redirect('/tickets')->with('status', 'The ticket '.$slug.' has been deleted!'); + } +} diff --git a/app/Http/Requests/CommentFormRequest.php b/app/Http/Requests/CommentFormRequest.php new file mode 100644 index 0000000..9c1ad27 --- /dev/null +++ b/app/Http/Requests/CommentFormRequest.php @@ -0,0 +1,30 @@ + 'required|min:3', + ]; + } +} \ No newline at end of file diff --git a/app/Http/Requests/TicketFormRequest.php b/app/Http/Requests/TicketFormRequest.php new file mode 100644 index 0000000..a5c2507 --- /dev/null +++ b/app/Http/Requests/TicketFormRequest.php @@ -0,0 +1,31 @@ + 'required|min:3', + 'content'=> 'required|min:10', + ]; + } +} diff --git a/app/Ticket.php b/app/Ticket.php new file mode 100644 index 0000000..cd4eba8 --- /dev/null +++ b/app/Ticket.php @@ -0,0 +1,15 @@ +hasMany('App\Comment', 'post_id'); + } +} diff --git a/composer.lock b/composer.lock index 6a38e3d..ee1ba84 100644 --- a/composer.lock +++ b/composer.lock @@ -454,16 +454,16 @@ }, { "name": "laravel/framework", - "version": "v5.7.6", + "version": "v5.7.7", "source": { "type": "git", "url": "https://github.com/laravel/framework.git", - "reference": "93e761bb5367166ce98ba908d5eb0edd6be76792" + "reference": "0438455128c0850b5872c7f3c11b7ccdbbfcba3e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/framework/zipball/93e761bb5367166ce98ba908d5eb0edd6be76792", - "reference": "93e761bb5367166ce98ba908d5eb0edd6be76792", + "url": "https://api.github.com/repos/laravel/framework/zipball/0438455128c0850b5872c7f3c11b7ccdbbfcba3e", + "reference": "0438455128c0850b5872c7f3c11b7ccdbbfcba3e", "shasum": "" }, "require": { @@ -475,6 +475,7 @@ "league/flysystem": "^1.0.8", "monolog/monolog": "^1.12", "nesbot/carbon": "^1.26.3", + "opis/closure": "^3.1", "php": "^7.1.3", "psr/container": "^1.0", "psr/simple-cache": "^1.0", @@ -591,7 +592,7 @@ "framework", "laravel" ], - "time": "2018-09-25T14:29:00+00:00" + "time": "2018-10-02T14:12:00+00:00" }, { "name": "laravel/tinker", @@ -924,6 +925,67 @@ ], "time": "2018-09-18T07:03:24+00:00" }, + { + "name": "opis/closure", + "version": "3.1.1", + "source": { + "type": "git", + "url": "https://github.com/opis/closure.git", + "reference": "d3209e46ad6c69a969b705df0738fd0dbe26ef9e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/opis/closure/zipball/d3209e46ad6c69a969b705df0738fd0dbe26ef9e", + "reference": "d3209e46ad6c69a969b705df0738fd0dbe26ef9e", + "shasum": "" + }, + "require": { + "php": "^5.4 || ^7.0" + }, + "require-dev": { + "jeremeamia/superclosure": "^2.0", + "phpunit/phpunit": "^4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Opis\\Closure\\": "src/" + }, + "files": [ + "functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marius Sarca", + "email": "marius.sarca@gmail.com" + }, + { + "name": "Sorin Sarca", + "email": "sarca_sorin@hotmail.com" + } + ], + "description": "A library that can be used to serialize closures (anonymous functions) and arbitrary objects.", + "homepage": "https://opis.io/closure", + "keywords": [ + "anonymous functions", + "closure", + "function", + "serializable", + "serialization", + "serialize" + ], + "time": "2018-10-02T13:36:53+00:00" + }, { "name": "paragonie/random_compat", "version": "v9.99.99", diff --git a/database/migrations/2018_10_01_180148_create_tickets_table.php b/database/migrations/2018_10_01_180148_create_tickets_table.php new file mode 100644 index 0000000..a80be7b --- /dev/null +++ b/database/migrations/2018_10_01_180148_create_tickets_table.php @@ -0,0 +1,36 @@ +increments('id'); + $table->string('title', 255); + $table->text('content'); + $table->string('slug')->nullable(); + $table->tinyInteger('status')->default(1); + $table->integer('user_id')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('tickets'); + } +} \ No newline at end of file diff --git a/database/migrations/2018_10_02_142926_create_comments_table.php b/database/migrations/2018_10_02_142926_create_comments_table.php new file mode 100644 index 0000000..b83cf8b --- /dev/null +++ b/database/migrations/2018_10_02_142926_create_comments_table.php @@ -0,0 +1,35 @@ +increments('id'); + $table->text('content'); + $table->integer('post_id'); + $table->integer('user_id')->nullable(); + $table->tinyInteger('status')->default(1); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('comments'); + } +} diff --git a/resources/views/emails/ticket.blade.php b/resources/views/emails/ticket.blade.php new file mode 100644 index 0000000..0b4fe98 --- /dev/null +++ b/resources/views/emails/ticket.blade.php @@ -0,0 +1,14 @@ + + + + + + +

Learning Laravel!

+ +
+ You have a new ticket. The ticket id is {{ $ticket }}! +
+ + + \ No newline at end of file diff --git a/resources/views/emails/welcome.blade.php b/resources/views/emails/welcome.blade.php new file mode 100644 index 0000000..4c43b50 --- /dev/null +++ b/resources/views/emails/welcome.blade.php @@ -0,0 +1,14 @@ + + + + + + +

Learning Laravel!

+ +
+ Welcome to {{ $name }} website! +
+ + + \ No newline at end of file diff --git a/resources/views/tickets/create.blade.php b/resources/views/tickets/create.blade.php new file mode 100644 index 0000000..2b33414 --- /dev/null +++ b/resources/views/tickets/create.blade.php @@ -0,0 +1,48 @@ +@extends('master') +@section('title', 'Create a ticket') + +@section('content') +
+
+
+
Create a ticket
+
+
+
+
+ @foreach ($errors->all() as $error) +

{{ $error }}

+ @endforeach + @if (session('status')) +
+ {{ session('status') }} +
+ @endif + +
+
+ +
+ +
+
+
+ +
+ + Feel free to ask us any question. +
+
+ +
+
+ + +
+
+
+
+
+
+
+@endsection \ No newline at end of file diff --git a/resources/views/tickets/edit.blade.php b/resources/views/tickets/edit.blade.php new file mode 100644 index 0000000..46d5dfd --- /dev/null +++ b/resources/views/tickets/edit.blade.php @@ -0,0 +1,52 @@ +@extends('master') +@section('title', 'Edit a ticket') + +@section('content') +
+
+
+
Edit ticket
+
+
+
+
+ @foreach ($errors->all() as $error) +

{{ $error }}

+ @endforeach + @if (session('status')) +
+ {{ session('status') }} +
+ @endif + +
+
+ +
+ +
+
+
+ +
+ + Feel free to ask us any question. +
+
+
+ +
+
+
+ + +
+
+
+
+
+
+
+@endsection \ No newline at end of file diff --git a/resources/views/tickets/index.blade.php b/resources/views/tickets/index.blade.php new file mode 100644 index 0000000..c49beed --- /dev/null +++ b/resources/views/tickets/index.blade.php @@ -0,0 +1,45 @@ +@extends('master') +@section('title', 'View all tickets') +@section('content') + +
+
+
+
Tickets
+
+
+
+ @if (session('status')) +
+ {{ session('status') }} +
+ @endif + @if ($tickets->isEmpty()) +

There is no ticket.

+ @else + + + + + + + + + + @foreach($tickets as $ticket) + + + + + + @endforeach + +
IDTitleStatus
{{ $ticket->id }} + {{ $ticket->title }} + {{ $ticket->status ? 'Pending' : 'Answered' }}
+ @endif +
+
+
+ +@endsection \ No newline at end of file diff --git a/resources/views/tickets/show.blade.php b/resources/views/tickets/show.blade.php new file mode 100644 index 0000000..cd3ffca --- /dev/null +++ b/resources/views/tickets/show.blade.php @@ -0,0 +1,68 @@ +@extends('master') +@section('title', 'View a ticket') +@section('content') + +
+
+
+
{{ $ticket->title }}
+
+
+
+

Status: {{ $ticket->status ? 'Pending' : 'Answered' }}

+

{{ $ticket->content }}

+ Edit +
+ +
+ +
+
+
+
+
+ @foreach($comments as $comment) +
+
+ {{ $comment->content }} +
+
+ @endforeach + +
+
+ + @foreach($errors->all() as $error) +

{{ $error }}

+ @endforeach + + @if(session('status')) +
+ {{ session('status') }} +
+ @endif + + + + +
+ Reply +
+
+ +
+
+ +
+
+ + +
+
+
+
+
+ +
+ +@endsection \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index bb464e2..75e1bd7 100644 --- a/routes/web.php +++ b/routes/web.php @@ -13,4 +13,11 @@ Route::get('/', 'PagesController@home'); Route::get('/about', 'PagesController@about'); -Route::get('/contact', 'PagesController@contact'); \ No newline at end of file +Route::get('/contact', 'TicketsController@create'); +Route::post('/contact', 'TicketsController@store'); +Route::get('/tickets', 'TicketsController@index'); +Route::get('/ticket/{slug?}', 'TicketsController@show'); +Route::get('/ticket/{slug?}/edit','TicketsController@edit'); +Route::post('/ticket/{slug?}/edit','TicketsController@update'); +Route::post('/ticket/{slug?}/delete','TicketsController@destroy'); +Route::post('/comment', 'CommentsController@newComment');