Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{

protected $fillable = ['name', 'article_category_id', 'url', 'description'];

public static function boot()
{
parent::boot();

Article::observe(new \App\Observers\UserActionsObserver);
}

public function category() {
return $this->belongsTo(ArticleCategory::class, 'article_category_id');
}
}
17 changes: 17 additions & 0 deletions app/ArticleCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ArticleCategory extends Model
{
protected $fillable = ['name', 'description'];

public static function boot()
{
parent::boot();

ArticleCategory::observe(new \App\Observers\UserActionsObserver);
}
}
120 changes: 120 additions & 0 deletions app/Http/Controllers/ArticleCategoriesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace App\Http\Controllers;

use App\ArticleCategory;
use Illuminate\Http\Request;

class ArticleCategoriesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$articleCategories = ArticleCategory::orderBy('name', 'asc')->get();

return view('article_categories.index', compact('articleCategories'));
}

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

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$articleCategories = new ArticleCategory();

$articleCategories->name = $request->input('name');
$articleCategories->description = $request->input('description');

$articleCategories->save();

return redirect()->route('article-categories.index')
->with('success', 'The Article Category: ' . $articleCategories->name . ' has been successfully created!');
}

/**
* Display the specified resource.
*
* @param \App\ArticleCategory $articleCategory
* @return \Illuminate\Http\Response
*/
public function show(ArticleCategory $articleCategory)
{
return view('article_categories.show', compact('articleCategory'));
}

/**
* Show the form for editing the specified resource.
*
* @param \App\ArticleCategory $articleCategory
* @return \Illuminate\Http\Response
*/
public function edit(ArticleCategory $articleCategory)
{
return view('article_categories.edit', compact('articleCategory'));
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\ArticleCategory $articleCategory
* @return \Illuminate\Http\Response
*/
public function update(Request $request, ArticleCategory $articleCategory)
{
$articleCategory->name = $request->input('name') ?? $articleCategory->name;
$articleCategory->description = $request->input('description') ?? $articleCategory->description;

$articleCategory->update();

return redirect()->route('article-categories.index')
->with('success', 'The Article Category: ' . $articleCategory->name . ' has been successfully updated');
}

/**
* Remove the specified resource from storage.
*
* @param \App\ArticleCategory $articleCategory
* @return \Illuminate\Http\Response
*/
public function destroy(ArticleCategory $articleCategory)
{
$articleCategory->delete();

return redirect()->route('article-categories.index')
->with('success', 'The Article Category: ' . $articleCategory->name . ' has been successfully deleted!');
}

/**
* Delete all selected Article Categories at once.
*
* @param Request $request
*/
public function massDestroy(Request $request)
{
if ($request->input('ids')) {
$entries = ArticleCategory::whereIn('id', $request->input('ids'))->get();

foreach ($entries as $entry) {
$entry->delete();
}
}
}
}
135 changes: 135 additions & 0 deletions app/Http/Controllers/ArticlesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace App\Http\Controllers;

use App\Article;
use App\ArticleCategory;
use App\Http\Requests\StoreArticleRequest;
use Illuminate\Http\Request;

class ArticlesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$articles = Article::with('category')->orderBy('name', 'asc')->get();

return view('articles.index', compact('articles'));
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$relations = [
'articleCategories' => \App\ArticleCategory::get()->pluck('name', 'id')->prepend('Please select', ''),
];

return view('articles.create', $relations);
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(StoreArticleRequest $request)
{
$article = new Article();

$article->name = $request->input('name');
$article->article_category_id = $request->input('article_category_id');
$article->url = $request->input('url');
$article->description = $request->input('description');

$article->save();

return redirect()->route('articles.index')
->with('success', 'The Article: ' . $article->name . ' has been successfully created!');
}

/**
* Display the specified resource.
*
* @param \App\Article $article
* @return \Illuminate\Http\Response
*/
public function show(Article $article)
{
return view('articles.show', compact('article'));
}

/**
* Show the form for editing the specified resource.
*
* @param \App\Article $article
* @return \Illuminate\Http\Response
*/
public function edit(Article $article)
{

$relations = [
'article_categories' => \App\ArticleCategory::get()->pluck('name', 'id')->prepend('Please select', ''),
];

return view('articles.edit', compact('article') + $relations);
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Article $article
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Article $article)
{
$article->name = $request->input('name') ?? $article->name;
$article->article_category_id = $request->input('article_category_id') ?? $article->article_category_id;
$article->url = $request->input('url') ?? $article->url;
$article->description = $request->input('description') ?? $article->description;

$article->update();

return redirect()->route('articles.index')
->with('success', 'The Article: ' . $article->name . ' has been successfully updated');
}

/**
* Remove the specified resource from storage.
*
* @param \App\Article $article
* @return \Illuminate\Http\Response
*/
public function destroy(Article $article)
{
$article->delete();

return redirect()->route('articles.index')
->with('success', 'The Article: ' . $article->name . ' has been successfully deleted!');
}

/**
* Delete all selected Article Categories at once.
*
* @param Request $request
*/
public function massDestroy(Request $request)
{
if ($request->input('ids')) {
$entries = Article::whereIn('id', $request->input('ids'))->get();

foreach ($entries as $entry) {
$entry->delete();
}
}
}
}
32 changes: 32 additions & 0 deletions app/Http/Requests/StoreArticleRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreArticleRequest 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 [
'name' => 'required|max:255',
'article_category_id' => 'required',
'url' => 'required',
];
}
}
6 changes: 3 additions & 3 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'charset' => env('DB_CHARSET', 'utf8'),
'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
'prefix' => '',
'strict' => true,
'engine' => null,
'engine' => env('DB_ENGINE', null),
],

'pgsql' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

class CreateArticleCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('article_categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('description')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('article_categories');
}
}
Loading