PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/Frontend/ArticleController.php

https://bitbucket.org/clickmysoft/ecocar_rental
PHP | 171 lines | 79 code | 34 blank | 58 comment | 1 complexity | 24a4db95dc972677c2e7b53260a48dcf MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. namespace App\Http\Controllers\Frontend;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Article\ArticleModel;
  6. use Carbon\Carbon;
  7. use App\Models\Category\CategoryModel;
  8. use App\Models\Tag\TagModel;
  9. use App\Models\Article\CategoryPostModel;
  10. use App\Models\Article\TagPostModel;
  11. use Illuminate\Support\Facades\DB;
  12. use App\Models\Auth\User;
  13. class ArticleController extends Controller
  14. {
  15. public function __construct()
  16. {
  17. $this->article = ArticleModel::paginate(5);
  18. }
  19. public function index()
  20. {
  21. $data = ArticleModel::orderBy('id','desc')->paginate(5);
  22. return view('frontend.article.index')->with([
  23. 'data' => $data,
  24. ]);
  25. }
  26. /**
  27. * Show the form for creating a new resource.
  28. *
  29. * @return \Illuminate\Http\Response
  30. */
  31. public function create()
  32. {
  33. //
  34. }
  35. /**
  36. * Store a newly created resource in storage.
  37. *
  38. * @param \Illuminate\Http\Request $request
  39. * @return \Illuminate\Http\Response
  40. */
  41. public function store(Request $request)
  42. {
  43. //
  44. }
  45. /**
  46. * Display the specified resource.
  47. *
  48. * @param int $id
  49. * @return \Illuminate\Http\Response
  50. */
  51. public function show($id)
  52. {
  53. $article = $this->article ;
  54. $data = ArticleModel::find($id);
  55. if(empty($data)){
  56. abort(404);
  57. }
  58. $data['created_at_humans'] = $data['created_at']->diffForHumans();
  59. $data['created_at_time'] = $this->DateThai($data['created_at']);
  60. $data['category'] = CategoryModel::where('status','enabled')->get();
  61. $data['fk_create_by'] = $this->getUser($data['fk_create_by']);
  62. $x2 = CategoryPostModel::where('post_id',$id)->get()->pluck('category_id')->toArray();
  63. $x3 = TagPostModel::where('post_id',$id)->get()->pluck('tag_id')->toArray();
  64. $data['category_post'] = DB::table('category')
  65. ->select('category.*')
  66. ->whereIn('category.id',$x2)
  67. ->get()->toArray();
  68. $data['post_tag'] = DB::table('tags')
  69. ->select('tags.*')
  70. ->whereIn('tags.id',$x3)
  71. ->get()->toArray();
  72. //$data['post_tag'] = TagModelL::whereIn('id',$x3)->get()->toArray();
  73. // echo "<pre>";
  74. // print_r($data['post_tag']);
  75. // echo "</pre>";
  76. // // exit;
  77. //$data['category_post'] = CategoryPostModel::where('post_id',$data['id'])->get();
  78. // foreach ($data['category'] as $key => $value) {
  79. // echo $value->id;
  80. // $xx = CategoryPostModel::select(DB::raw('count(*) as count','category_id'))->where('category_id', '=', 2)->get()->toArray();
  81. // echo "<pre>";
  82. // print_r($xx);
  83. // echo "</pre>";
  84. // }
  85. // echo "<pre>";
  86. // print_r($data['category']);
  87. // echo "</pre>";
  88. // exit;
  89. return view('frontend.article.show',compact('data','article'));
  90. }
  91. /**
  92. * Show the form for editing the specified resource.
  93. *
  94. * @param int $id
  95. * @return \Illuminate\Http\Response
  96. */
  97. public function edit($id)
  98. {
  99. //
  100. }
  101. /**
  102. * Update the specified resource in storage.
  103. *
  104. * @param \Illuminate\Http\Request $request
  105. * @param int $id
  106. * @return \Illuminate\Http\Response
  107. */
  108. public function update(Request $request, $id)
  109. {
  110. //
  111. }
  112. /**
  113. * Remove the specified resource from storage.
  114. *
  115. * @param int $id
  116. * @return \Illuminate\Http\Response
  117. */
  118. public function destroy($id)
  119. {
  120. //
  121. }
  122. public function DateThai($strDate){
  123. $strYear = date("Y",strtotime($strDate))+543;
  124. $strMonth= date("n",strtotime($strDate));
  125. $strDay= date("j",strtotime($strDate));
  126. $strHour= date("H",strtotime($strDate));
  127. $strMinute= date("i",strtotime($strDate));
  128. $strSeconds= date("s",strtotime($strDate));
  129. $strMonthCut = Array("","ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");
  130. $strMonthThai=$strMonthCut[$strMonth];
  131. return "$strDay $strMonthThai $strYear";
  132. }
  133. public function getUser($id){
  134. $data = User::find($id);
  135. return $data['first_name'].' '.$data['last_name'];
  136. }
  137. }