Skip to content

Instantly share code, notes, and snippets.

@juniorb2ss
Created September 3, 2014 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juniorb2ss/d791aed99db23bf84fa6 to your computer and use it in GitHub Desktop.
Save juniorb2ss/d791aed99db23bf84fa6 to your computer and use it in GitHub Desktop.
<?php
namespace App\Modules\News\Models;
use Carbon\Carbon, Model\Category, Model\Tags, Model\Categorys, Debugbar;
class News extends \Eloquent {
public $with = [ 'tags', 'category' ]; // related relationship
/**
* All News to Index Page
* @return response data news
*/
public function getAll()
{
Debugbar::addMessage('Listing all getAll of database', 'News');
return $this->where('is_public', TRUE) // only news public in index page
->where('is_draft', FALSE) // no trash
->where('post_to', '<=', Carbon::now()) // Captures all news period date
->whereHas('category', function($q){
$q->where('enable', TRUE)->where('index', TRUE);
})
->orderBy('is_top', 'DESC') // priorit top index
->orderBy('id', 'DESC') // order by id desc
->paginate(3);
}
/**
* All News with category name
* @return response data news
*/
public function getAllWithCategory($category_name)
{
Debugbar::addMessage('Listing all getAllWithCategory of database', 'News');
$Category = Categorys::whereName( urldecode( $category_name ) )->get(); // Update visits
if($Category->count()){
$Category[0]->visits++;
$Category[0]->save();
}
return $this->where('is_public', TRUE) // only news public in index page
->where('is_draft', FALSE) // no trash
->where('post_to', '<=', Carbon::now()) // Captures all news period date
->whereHas('category', function($q) use($category_name){
$q->where('enable', TRUE)
->where('name', urldecode( $category_name ) ); // Where category name
})
->orderBy('is_top', 'DESC') // priorit top index
->orderBy('id', 'DESC') // order by id desc
->paginate(5);
}
/**
* All News with tag name
* @return response data news
*/
public function getAllWithTag($tag_name)
{
Debugbar::addMessage('Listing all getAllWithTag of database', 'News'); // debugbar log
$Tag = Tags::whereName( urldecode( $tag_name ) )->get(); // Update visits
if($Tag->count()){
$Tag[0]->visits++;
$Tag[0]->save();
}
return $this->where('is_public', TRUE) // only news public in index page
->where('is_draft', FALSE) // no trash
->where('post_to', '<=', Carbon::now()) // Captures all news period date
->whereHas('tags', function($q) use($tag_name){
$q->where('name', urldecode( $tag_name ) ); // Where tag name
})
->orderBy('is_top', 'DESC') // priorit top index
->orderBy('id', 'DESC') // order by id desc
->paginate(5);
}
/**
* News Information
* @param int $id reference (:table).news_id
* @return response data news
*/
public function getNew($id)
{
Debugbar::addMessage('Show getNew(<id>) of database', 'News'); // debugbar log
$New = News::find($id); // Update visits
if($New){
$New->visits++;
$New->save();
}
return parent::remember(1, 'new.'.$id)->find($id);
}
/**
* relationship news to categorys
* @return return object instance
*/
public function category()
{
Debugbar::addMessage('Returning relationship news > categorys', 'News'); // debugbar log
return $this->hasMany('Category', 'id', 'category_id');
}
/**
* relationship news to tags
* @return return object instance
*/
public function tags()
{
Debugbar::addMessage('Returning relationship news > news_tags > tags', 'News'); // debugbar log
return $this->belongsToMany('Tags', 'news_tags', 'news_id', 'tags_id');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment