Created
September 10, 2016 01:03
-
-
Save robertholf/9065d3dcf8f183b7a93c0fb461be7734 to your computer and use it in GitHub Desktop.
Laravel 5.2 Model/Route/Controller (http://diydifm.com)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\App\Term; | |
use Illuminate\Http\Request; | |
use App\Models\App\Term\Term; | |
use App\Models\App\Term\TermExample; | |
use App\Http\Controllers\Controller; | |
use App\Classes\Breadcrumbs; | |
/** | |
* Class TermController | |
* @package App\Http\Controllers | |
*/ | |
class TermController extends Controller | |
{ | |
public function __construct() | |
{ | |
// Set Base Breadcrumb | |
Breadcrumbs::push('<i class="fa fa-home"></i>', route('marketing.index')); | |
} | |
public function index() | |
{ | |
// Get All Terms | |
$terms = Term::translated()->orderBy('slug')->get(); | |
// Get Most Popular Terms (Active Language) | |
$topTerms = Term::join('terms_translations as t', 't.term_id', '=', 'terms.id') | |
->where('locale', 'en') | |
->where('t.stat_viewed', '>', '0') | |
->groupBy('terms.id') | |
->orderBy('t.stat_viewed', 'desc') | |
->with('translations') | |
->take(10) | |
->get(); | |
// Specify Title | |
$title = 'Glossary'; // @TODO: Translate | |
// Set Breadcrumbs | |
Breadcrumbs::push($title, route('app.term.index')); | |
// Return View | |
return view('app.term.index', compact('title', 'terms', 'topTerms')); | |
} | |
/** | |
* Display the specified resource. | |
* | |
* @param int $id | |
* @return Response | |
*/ | |
public function show($slug) | |
{ | |
// Get Term | |
$term = Term::where('slug', $slug)->first(); | |
// Translate | |
$translatedTerm = $term->translate(); | |
// Update View Count +1 | |
$update = $translatedTerm->increment('stat_viewed'); | |
// Get Examples | |
$examples = TermExample::where('term_id', $term->id)->get(); | |
// Specify Title | |
$parentTitle = 'Glossary'; // @TODO: Translate | |
$title = $translatedTerm->title; | |
// Set Breadcrumbs | |
Breadcrumbs::push($parentTitle, route('app.term.index')); | |
Breadcrumbs::push($title, route('app.term.show', $term->slug)); | |
// Return View | |
return view('app.term.show', compact('title','term','examples')); | |
} | |
/** | |
* Search | |
*/ | |
public function searchSuggest(Request $request) | |
{ | |
$query = $request->input('query'); | |
if ($query) { | |
$terms = Term::whereTranslationLike('title', '%'.$query.'%')->orWhere('slug', 'like', '%'.$query.'%')->translated()->take(15)->get(); | |
} else { | |
$terms = NULL; | |
} | |
// Handle AJAX Requests | |
if (\Request::ajax()) { | |
return json_encode($terms); | |
} | |
return $terms; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Forum Controllers | |
*/ | |
Route::group(['middleware' => 'web'], function () { | |
Route::group(['prefix' => 'forum', 'namespace' => 'Forum'], function() { | |
Route::get('/', 'ForumController@index')->name('app.forum.index'); | |
Route::get('{topic}', 'ForumController@topic')->name('app.forum.topic'); | |
Route::group(['middleware' => 'auth'], function () { | |
Route::get('{topic}/create', 'ForumController@threadCreate')->name('app.forum.thread.create'); | |
Route::post('thread', 'ForumController@threadStore')->name('app.forum.thread.store'); | |
}); | |
Route::get('{topic}/{thread}', 'ForumController@threadShow')->name('app.forum.thread'); | |
Route::group(['middleware' => 'auth'], function () { | |
Route::post('thread/comment', 'ForumController@threadCommentStore')->name('app.forum.thread.comment'); | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models\App\Forum; | |
use App\Models\Actor\User\User; | |
use Dimsav\Translatable\Translatable; | |
use Illuminate\Database\Eloquent\Model; | |
class ForumThread extends Model | |
{ | |
use Translatable; | |
protected $table = 'forum_threads'; | |
public $translatedAttributes = [ | |
'title', 'text', | |
]; | |
public $useTranslationFallback = true; | |
protected $fillable = [ | |
'user_id', 'brand_id', 'slug', 'featured', 'reportable', 'is_pinned', | |
]; | |
public function getRouteKeyName() | |
{ | |
return 'slug'; | |
} | |
public function topic() | |
{ | |
return $this->belongsTo(ForumTopic::class, 'forum_topic_id'); | |
} | |
public function comments() | |
{ | |
return $this->hasMany(ForumComment::class)->whereNull('parent_id'); | |
} | |
public function user() | |
{ | |
return $this->belongsTo(User::class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment