Skip to content

Commit

Permalink
Chapter 3
Browse files Browse the repository at this point in the history
  • Loading branch information
LearningLaravel committed Oct 2, 2018
1 parent 4bcfc75 commit 2c95a01
Show file tree
Hide file tree
Showing 17 changed files with 623 additions and 7 deletions.
15 changes: 15 additions & 0 deletions app/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
protected $guarded = ['id'];

public function ticket()
{
return $this->belongsTo('App\Ticket');
}
}
22 changes: 22 additions & 0 deletions app/Http/Controllers/CommentsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\CommentFormRequest;
use App\Comment;

class CommentsController extends Controller
{
public function newComment(CommentFormRequest $request)
{
$comment = new Comment(array(
'post_id' => $request->get('post_id'),
'content' => $request->get('content')
));

$comment->save();

return redirect()->back()->with('status', 'Your comment has been created!');
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/PagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public function about()

public function contact()
{
return view('contact');
return view('tickets.create');
}
}
122 changes: 122 additions & 0 deletions app/Http/Controllers/TicketsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\TicketFormRequest;
use App\Ticket;
use Illuminate\Support\Facades\Mail;

class TicketsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$tickets = Ticket::all();
return view('tickets.index', compact('tickets'));
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('tickets.create');
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(TicketFormRequest $request)
{
$slug = uniqid();
$ticket = new Ticket(array(
'title' => $request->get('title'),
'content' => $request->get('content'),
'slug' => $slug
));

$ticket->save();

$data = array(
'ticket' => $slug,
);

// Mail::send('emails.ticket', $data, function ($message) {
// $message->from('[email protected]', 'Learning Laravel');
//
// $message->to('[email protected]')->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!');
}
}
30 changes: 30 additions & 0 deletions app/Http/Requests/CommentFormRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CommentFormRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'content'=> 'required|min:3',
];
}
}
31 changes: 31 additions & 0 deletions app/Http/Requests/TicketFormRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class TicketFormRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required|min:3',
'content'=> 'required|min:10',
];
}
}
15 changes: 15 additions & 0 deletions app/Ticket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Ticket extends Model
{
protected $fillable = ['title', 'content', 'slug', 'status', 'user_id'];

public function comments()
{
return $this->hasMany('App\Comment', 'post_id');
}
}
72 changes: 67 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions database/migrations/2018_10_01_180148_create_tickets_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

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

class CreateTicketsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tickets', function (Blueprint $table) {
$table->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');
}
}
Loading

0 comments on commit 2c95a01

Please sign in to comment.