Completed
Push — development ( 840081...a2e0b5 )
by Ashutosh
12:16
created

CategoryController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Product;
4
5
6
use Bugsnag;
7
use Illuminate\Http\Request;
8
use App\Http\Controllers\Controller;
9
use App\Model\Product\ProductCategory;
10
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
A parse error occurred: Cannot use Illuminate\Http\Request as Request because the name is already in use
Loading history...
11
12
class CategoryController extends Controller
13
{
14
    public $productCategory;
15
16
    public function __construct()
17
    {
18
        $this->middleware('auth');
19
        $this->middleware('admin');
20
21
        $productCategory = new ProductCategory();
22
        $this->productCategory = $productCategory;
23
24
    }
25
26
    /**
27
     * Display a listing of the resource.
28
     *
29
     * @return \Illuminate\Http\Response
30
     */
31
    public function index()
32
    {
33
        return view('themes.default1.category.index');
34
    }
35
36
    /**
37
     * Show the form for creating a new resource.
38
     *
39
     * @return \Illuminate\Http\Response
40
     */
41
 
42
   
43
    public function store(Request $request)
44
    {
45
46
       try{
47
       $productCategory = $this->productCategory->fill($request->input())->save();
48
       return redirect()->back()->with('success',\Lang::get('message.saved-successfully'));
49
       }catch (\Exception $ex)
50
       {
51
        return redirect()->back()->with('fails',$ex->getMessage());
52
       }  
53
 
54
    }
55
    
56
57
    
58
    /* 
59
    * Get All the categories
60
    */
61
    public function getCategory()
62
63
    {
64
        try{
65
        $allCategories = $this->productCategory->select('id','category_name')->get();
66
        return \DataTables::of($allCategories)
67
         ->addColumn('checkbox' , function($model){
68
            return "<input type='checkbox' class='category_checkbox' 
69
            value=".$model->id.' name=select[] id=check>';
70
         })
71
         ->addColumn('category_name',function($model){
72
            return ucfirst($model->category_name);
73
         })
74
         ->addColumn('action',function($model){
75
             return "<p><button data-toggle='modal' 
76
             data-id=".$model->id. " data-name= '$model->category_name' 
77
             class='btn btn-sm btn-primary btn-xs editCat'><i class='fa fa-edit'
78
             style='color:white;'> </i>&nbsp;&nbsp;Edit</button>&nbsp;</p>";
79
         })
80
         ->rawColumns(['checkbox','category_name','action'])
81
         ->make(true);
82
      } catch (\Exception $ex)
83
      {
84
        return redirect()->back()->with('fails',$ex->getMessage());
85
      }
86
    }
87
     
88
89
90
  
91
92
    public function update(Request $request, $id)
93
    {
94
       try{
95
       $cat_name = $request->input('category_name');
96
       $category = $this->productCategory->where('id',$id)->update(['category_name' =>$cat_name]);
97
       return redirect()->back()->with('success',\Lang::get('message.updated-successfully'));
98
      }catch(\Exception $ex){
99
        Bugsnag::notifyException($ex);
100
        return redirect()->back()->with('fails',$ex->getMessage());
101
      }
102
    }
103
104
    /**
105
     * Remove the specified resource from storage.
106
     *
107
     * @param int $id
108
     *
109
     * @return \Illuminate\Http\Response
110
     */
111
    public function destroy(Request $request)
112
    {
113
        try {
114
            $ids = $request->input('select');
115
            if (!empty($ids)) {
116
                foreach ($ids as $id) {
117
                    if ($id != 1) {
118
                        $category = $this->productCategory->where('id', $id)->first();
119
                        if ($category) {
120
                            $category->delete();
121
                        } else {
122
                            echo "<div class='alert alert-danger alert-dismissable'>
123
                    <i class='fa fa-ban'></i>
124
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
125
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
126
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
127
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
128
                </div>';
129
                            //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
130
                        }
131
                        echo "<div class='alert alert-success alert-dismissable'>
132
                    <i class='fa fa-ban'></i>
133
134
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
135
                    /* @scrutinizer ignore-type */\Lang::get('message.success').'
136
137
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
138
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
139
                </div>';
140
                    } else {
141
                        echo "<div class='alert alert-danger alert-dismissable'>
142
                    <i class='fa fa-ban'></i>
143
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
144
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
145
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
146
                        './* @scrutinizer ignore-type */\Lang::get('message.can-not-delete-default').'
147
                </div>';
148
                    }
149
                }
150
            } else {
151
                echo "<div class='alert alert-danger alert-dismissable'>
152
                    <i class='fa fa-ban'></i>
153
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
154
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
155
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
156
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
157
                </div>';
158
                //echo \Lang::get('message.select-a-row');
159
            }
160
        } catch (\Exception $e) {
161
            echo "<div class='alert alert-danger alert-dismissable'>
162
                    <i class='fa fa-ban'></i>
163
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
164
                    /* @scrutinizer ignore-type */
165
                    \Lang::get('message.failed').'
166
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
167
                        '.$e->getMessage().'
168
                </div>';
169
        }
170
    }
171
}
172