Skip to content

Instantly share code, notes, and snippets.

@kooler62
Last active October 12, 2018 05:55
Show Gist options
  • Select an option

  • Save kooler62/b0579d68f59df4a18d603f337a3558ae to your computer and use it in GitHub Desktop.

Select an option

Save kooler62/b0579d68f59df4a18d603f337a3558ae to your computer and use it in GitHub Desktop.
Controller examples
<?php
namespace App;
use DB;
use Illuminate\Database\Eloquent\Model;
class BlogCategory extends Model
{
protected $table = 'blog_categories';
protected $primaryKey = 'id';
public function blog_posts()
{
return $this->hasMany('App\BlogPost','category_id');
}
static function header_categories(){
return BlogCategory::all()
->where('visibility', 'visible')
->sortBy('position');
}
public function category_posts_count($id){
return DB::table('blog_posts')
->where('category_id', $id)
->whereVisibilityAndStatus('public','PUBLISHED')
->count();
}
}
<?php
namespace App\Http\Controllers;
use App\BlogPost;
use Illuminate\Http\Request;
class BlogController extends Controller
{
public function index(){
$posts = BlogPost::with('BlogCategory')
->whereVisibilityAndStatus('public','PUBLISHED')
->whereHas('BlogCategory', function($query) {
$query->whereVisibility('visible');
})
->orderBy('created_at', 'desc')
->paginate(10);
return view('blog.posts',
[
'title' =>'',
'meta_keys' => '',
'meta_descriptions' => '',
'posts' => $posts,
]
);
}
public function show($slug){
$post = BlogPost::where('slug', $slug)
->whereVisibilityAndStatus('public', 'PUBLISHED');
$post->increment('views');
return view('blog.post',
[
'post' => $post->firstOrFail(),
]
);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use DB;
class BlogPost extends Model
{
protected $table = 'blog_posts';
protected $primaryKey = 'id';
public function BlogCategory()
{
return $this->belongsTo('App\BlogCategory','category_id');
}
static function category_posts_count($id){
return DB::table('blog_posts')
->where('category_id', $id)
->where('status', 'PUBLISHED')
->where('visibility', 'public')
->count();
}
}
<?php
namespace App\Http\Controllers\Admin;
use App\Product;
use Carbon\Carbon;
use App\Category;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::orderBy('position')->paginate(10);
return view('admin.products',
[
'page_title' => "Продукты",
'products' => $products
]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
foreach(Category::all() as $category){
$categories[$category->id] = "[$category->id]: $category->title";
}
return view('admin.create_product',
[
'page_title' => "Создание товара",
'products' => Product::all(),
'categories' => $categories,
]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request, Product $Product)
{
$this->validate($request, [
'title' => 'min:3|max:255',
'slug' => 'required|unique:products|min:3|max:255',
'position' => 'min:0',
'category_id' => 'required',
'img' => 'required'
]);
$request['created_at'] = Carbon::now();
$Product->create($request->except('_token'));
return redirect()->route('products.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
return view('admin.show_product',
[
'page_title' => "Просмотр продукта",
'product' => Product::find($id)
]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
foreach(Category::all() as $category){
$categories[$category->id] = "[$category->id]: $category->title";
}
return view('admin.edit_product',
[
'page_title' => "Изменение продукта",
'product' => Product::find($id),
'categories'=>$categories,
]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request,
[
'title' => 'min:3|max:255',
'slug' => 'required|min:3|max:255',
'position'=>'min:0',
'category_id' => 'required',
'img' => 'required'
]);
$Product = new Product();
$request['updated_at'] = Carbon::now();
$data = $request->except(['_token', '_method']);
$Product->edit($id, $data);
return redirect()->route('products.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Product::destroy($id);
return redirect()->route('products.index');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment