Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers\Dashboard\Admin_Panel;
- use Validator;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use App\Http\Controllers\Dashboard\Admin_Panel\GuardParams as GuardParams;
- use App\Http\Controllers\Dashboard\DashboardController as DashboardController;
- class ArticlesController extends DashboardController
- {
- use GuardParams;
- protected $_mainTable = 'articles';
- protected $_mainModel = 'App\Article';
- protected $_param_btn = 'article';
- protected $_param_title = 'Articles';
- protected $_param_success = 'article';
- protected $_uploads_folder = '/images/uploads';
- public function __construct() {
- /*\DB::listen(function($query) {
- \Log::info(
- $query->sql,
- $query->bindings,
- $query->time
- );
- });*/
- parent::__construct();
- if(!request()->ajax()) { // not need to do this, datatable or other response type
- $this->_page_params = $this->get_page_params();
- }
- }
- protected function item_description($item) {
- return $item->title. ' (' .$item->id. ')';
- }
- protected function get_items_params() {
- $allType = request()->type_all;
- $params = array(
- 'active' => array(
- 'section_title' => 'Active ' .$this->_param_title,
- 'page_title' => 'Active ' .$this->_param_title,
- 'table_title' => 'Active ' .$this->_param_title,
- 'box_class' => 'box-success',
- ),
- 'inactive' => array(
- 'section_title' => $this->_param_title. ' deleted articles',
- 'page_title' => $this->_param_title. ' deleted articles',
- 'table_title' => $this->_param_title. ' deleted articles',
- 'box_class' => 'box-danger',
- ),
- );
- if(!isset($params[$allType])) {
- abort(404);
- }
- return $params[$allType];
- }
- private function get_table_builder() {
- $model = $this->_mainModel;
- $builder = $model::select(['articles.id', 'articles.title', 'articles.text', 'users.name', 'articles.created_at', 'categories.name as category', 'articles.deleted_at'/*, 'images.file_name as main_img_file'*/])
- /*->selectRaw('count(*) as comments_count, group_concat(comments.text, "<br>") as comments')*/
- /*->selectRaw('group_concat(comments.text, "<br>") as comments')*/
- ->leftJoin('users', 'users.id','=','articles.id_user')
- /*->leftJoin('images', 'articles.id','=','images.id_article')*/
- /*->where('images.main', 1)*/
- ->leftJoin('categories', 'categories.id','=','articles.id_category');
- /*->leftJoin('comments', 'articles.id', '=', 'comments.id_article')
- ->groupBy('articles.id');*/
- /*$col_interval = request()->column_date_interval;
- if (!is_null($col_interval)) {
- $range = [date("Y-m-d H:i:s", strtotime(request()->min_date)), date("Y-m-d H:i:s", strtotime(request()->max_date))];
- $builder->whereBetween($col_interval, $range);
- }*/
- if(request()->type_all == 'inactive')
- $builder->onlyTrashed();
- else
- $builder->withoutTrashed();
- // $builder->withTrashed();
- // \Log::info('Table builder: ' .$builder->toSql());
- return $builder;
- }
- private function get_columns() {
- // inner array -> data: col_unique name from select, if same cols names diferent tables, use alias in select
- // inner array -> name: table.column in database structure
- return array(
- 'ID' => array('data' => 'id', 'name' => 'articles.id', 'visible' => false),
- /*'Main Image' => array('data' => 'main_img_file', 'sortable' => false, 'searchable' => false),*/
- 'Title' => array('data' => 'title'),
- 'Text' => array('data' => 'text', 'name' => 'articles.text', 'visible' => false),
- 'User' => array('data' => 'name', 'name' => 'users.name'),
- 'Category' => array('data' => 'category', 'name' => 'categories.name'),
- /*'Comments' => array('data' => 'comments', 'name' => 'comments.text'),*/
- /*'Comments Count' => array('data' => 'comments_count', 'name' => 'comments_count', 'searchable' => false),*/
- 'Created' => array('data' => 'created_at', 'name' => 'articles.created_at', 'searchable' => false),
- 'Actions' => array('data' => 'actions', 'orderable' => false, 'searchable' => false)
- );
- }
- /* private function get_table_builder() {
- $model = $this->_mainModel;
- $builder = $model::with([
- 'comments' => function($q) {
- $q->select('id_article', 'text');
- },
- 'category' => function($q) {
- $q->select('id', 'name');
- },
- 'user' => function($q) {
- $q->select('id', 'name', 'id_role');
- },
- 'user.role' => function($q) {
- $q->select('id', 'name');
- },
- 'main_img' => function($q) {
- $q->select('id_article', 'file_name');
- }
- ])
- ->join('users', 'users.id','=','articles.id_user')
- ->join('categories', 'categories.id','=','articles.id_category')
- ->join('roles', 'roles.id','=','users.id_role')
- ->join('comments', 'articles.id', '=', 'comments.id_article')
- ->select(['articles.id', 'articles.title', 'articles.text', 'articles.created_at', 'articles.id_category', 'articles.deleted_at', 'articles.id_user']);
- ->selectRaw('count(comments.id_article) as comments_count')
- ->groupBy('articles.id');
- if(request()->type_all == 'inactive')
- $builder->onlyTrashed();
- else
- $builder->withoutTrashed();
- // $builder->withTrashed();
- return $builder;
- }
- private function get_columns() {
- // inner array -> data: col_unique name from select, if same cols names diferent tables, use alias in select
- // inner array -> name: table.column in database structure
- return array(
- 'ID' => array('data' => 'id', 'name' => 'articles.id', 'visible' => false),
- 'Main Image' => array('data' => 'main_img', 'sortable' => false, 'searchable' => false),
- 'Title' => array('data' => 'title'),
- 'Text' => array('data' => 'text', 'name' => 'articles.text'),
- 'User' => array('data' => 'user.name', 'name' => 'user.name'),
- 'Role' => array('data' => 'user.role.name', 'name' => 'user.role.name', 'visible' => false),
- 'Category' => array('data' => 'category.name', 'name' => 'category.name'),
- 'Comments' => array('data' => 'comments', 'name' => 'comments.text'),
- 'Comments Count' => array('data' => 'comments_count', 'name' => 'comments_count', 'searchable' => false),
- 'Created' => array('data' => 'created_at', 'visible' => false),
- 'Actions' => array('data' => 'actions', 'orderable' => false, 'searchable' => false)
- );
- } */
- private function get_table() {
- $builder = $this->get_table_builder();
- $datatable = \Datatables::of($builder);
- if(request()->type_all == 'inactive')
- $datatable->onlyTrashed();
- $datatable->addColumn('actions', function ($item) {
- if($item->trashed())
- $actions = ['view', 'restore', 'force_delete'];
- else
- $actions = ['view', 'edit', 'delete'];
- $actions = $this->get_buttons($item->id, $actions, $this->item_description($item));
- return $this->get_buttons_html($actions);
- });
- /*$datatable->addColumn('user_role', function ($item) {
- return $item->user->role->name;
- });*/
- $datatable->editColumn('created_at', function ($item) {
- return $item->created_at->format('d-m-Y');
- });
- $datatable->editColumn('text', function ($item) {
- return str_limit(strip_tags($item->text), 20, '...');
- });
- /* $datatable->editColumn('comments', function ($item) {
- return $item->comments->map(function($i) {
- return str_limit($i->text, 30, '...');
- })->implode('<br>');
- });
- $datatable->addColumn('comments_count', function ($item) {
- return count($item->comments);
- }); */
- /*$datatable->editColumn('main_img_file', function ($item) {
- return '<img class="table_image" src="' .url($this->_uploads_folder. '/' .$item->main_img_file). '" alt="' .$item->main_img_file. '">';
- }); */
- $datatable->setRowClass(function ($item) {
- return $item->id % 2 == 0 ? 'success' : 'danger';
- });
- $col_interval = request()->column_date_interval;
- if (!is_null($col_interval)) {
- $datatable->filter(function($q) use ($col_interval) {
- $range = [date("Y-m-d H:i:s", strtotime(request()->min_date)), date("Y-m-d H:i:s", strtotime(request()->max_date))];
- $q->whereBetween($col_interval, $range);
- });
- }
- $datatable->rawColumns(['actions']);
- // setting setTotalRecords
- // return $datatable->rawColumns(['actions', 'comments'])->setTotalRecords(1000);
- /*return $datatable->rawColumns(['actions', 'comments'])
- ->limit(function ($query) {
- $query->where('articles.id', '>', 499000);
- })
- ->setTotalRecords(1000);*/
- // return $datatable->setTotalRecords($builder->count());
- return $datatable;
- }
- public function get_all() {
- $datepicker = Null; // no datepicker
- $datepicker = array(
- 'min_label' => 'From',
- 'max_label' => 'To',
- 'columnNameSQL' => 'articles.created_at',
- );
- // is datatable
- if(request()->ajax()) {
- $st = microtime(true);
- $ret = $this->get_table()->make(true);
- // \Log::info(microtime(true)-$st);
- return $ret;
- }
- $cols_params = $this->get_columns();
- $cols_names = array_keys($cols_params);
- $cols_params = array_values($cols_params);
- $itemsParams = $this->get_items_params();
- $myTables = array(
- array(
- 'title' => $itemsParams['table_title'],
- 'box_class' => $itemsParams['box_class'],
- 'cols_names' => $cols_names,
- 'cols_params' => $cols_params,
- 'costum_filter_params' => []
- // 'rows' => $rows,
- ),
- );
- $this->_page_params['section_title'] = $itemsParams['section_title'];
- $this->_page_params['page_title'] = $itemsParams['page_title'];
- $dataToView = array(
- 'page_params' => $this->_page_params,
- 'data' => array(
- 'tables' => $myTables,
- 'datepicker' => $datepicker,
- 'add_btn' => array(
- 'href' => $this->_baseUrl. '/' .$this->_guard. '/' .$this->_mainTable. '/add',
- 'title' => 'Add ' .$this->_param_btn,
- 'text' => 'Add ' .$this->_param_btn,
- ),
- )
- );
- $dataToView['datatable'] = array(
- 'server_side' => True,
- );
- $dataToView['data']['export_btn'] = array(
- 'href' => $this->_baseUrl. '/' .$this->_guard. '/' .$this->_mainTable. '/export_csv/' .request()->type_all,
- 'title' => 'Export data to csv/excel',
- 'text' => 'Export data to csv/excel'
- );
- return view('dashboard/sections/datatable_server')->with($dataToView);
- }
- public function export_csv() {
- $builder = $this->get_table_builder();
- $has_data = false;
- foreach($builder->cursor() as $key => $row) {
- if(!$has_data) {
- $t = $this->get_columns();
- $delimiter = ';';
- $file_name = $this->_mainTable. '.csv';
- $export_file = storage_path('/exports/' .$file_name);
- file_put_contents($export_file, '');
- $text = "\xEF\xBB\xBF".implode($delimiter, array_keys($t)). "\n";
- $cols = array_column(array_values($t), 'data');
- $cols = array_diff($cols, ['actions']); // don't use actions
- $has_data = true;
- }
- if($key%10000 == 0) {
- file_put_contents($export_file, $text, FILE_APPEND);
- $text = ''; // save ram for big data
- }
- $text .= implode($delimiter, array_map(function($c) use ($row) {
- return $row->$c;
- }, $cols)). "\n";
- }
- if(!$has_data) { // no text
- return redirect()->back()->with('flash_error', 'No data to export');
- }
- file_put_contents($export_file, $text, FILE_APPEND);
- return response()->download($export_file);
- /*return response($text)->withHeaders([
- 'Content-Type' => 'application/octet-stream',
- 'Content-disposition' => 'attachment; filename="' .$file_name,
- 'Content-Transfer-Encoding' => 'Binary',
- ]);*/
- /*header("Content-Type: application/octet-stream");
- header("Content-Transfer-Encoding: Binary");
- header('Content-disposition: attachment; filename="' .$this->_mainTable. '.csv"');
- return $text;*/
- }
- //INSPECT/VIEW ITEM
- public function view_item() {
- $model = $this->_mainModel;
- $item = $model::with(['category', 'main_img', 'thumbnail_img', 'user'])->withTrashed()->findOrFail(request()->id);
- $data = array(
- 'inspect' => array(
- 'Title' => $item->title,
- 'Category' => (!is_null($item->category)) ? $item->category->name : '',
- 'User' => '<a href="' .url($this->_baseUrl. '/' .$this->_guard. '/users/view/' .$item->user->id). '">' .$item->user->name. '</a>',
- 'Main Image' => '<br><a target="_blank" href="' .url($this->_uploads_folder. '/' .$item->main_img->file_name). '"><img class="image_inspect" src="' .url($this->_uploads_folder. '/' .$item->main_img->file_name). '" alt="' .$item->main_img->file_name. '"></a>',
- 'Thumbnail' => '<br><a target="_blank" href="' .url($this->_uploads_folder. '/' .$item->thumbnail_img->file_name). '"><img class="image_inspect" src="' .url($this->_uploads_folder. '/' .$item->thumbnail_img->file_name). '" alt="' .$item->thumbnail_img->file_name. '"></a>',
- 'On main Slider' => ($item->on_main_slider==1)?'yes':'no',
- 'Created At' => $item->created_at->format('d-m-Y H:i'),
- 'Updated At' => $item->updated_at->format('d-m-Y H:i'),
- 'Deleted At' => (is_null($item->deleted_at))?'':$item->deleted_at->format('d-m-Y H:i'),
- 'Body' => '<pre style="white-space: normal;">'.$item->text.'</pre>',
- ),
- );
- if($item->trashed())
- $buttons = array('restore');
- else
- $buttons = array('delete', 'edit');
- $item_desc = $this->item_description($item);
- $data['buttons'] = $this->get_buttons($item->id, $buttons, $item_desc);
- $this->_page_params['section_title'] = $this->_param_title. ': ' .$item_desc;
- $this->_page_params['page_title'] = 'Dashboard | '.$this->_param_title;
- $dataToView = array(
- 'page_params' => $this->_page_params,
- 'data' => $data
- );
- return view('dashboard/sections/inspect')->with($dataToView);
- }
- protected function get_form_inputs($item=null) {
- $categories = collect(array((object) array(
- 'name' => '',
- 'id' => '',
- 'opt_text' => '',
- 'props' => array(
- 'value' => '',
- 'disabled' => 'disabled',
- 'selected' => 'selected'
- )
- )));
- $on_main_slider = collect(
- array(
- (object) array(
- 'opt_text' => '',
- 'props' => array(
- 'type' => 'checkbox',
- 'value' => '1',
- 'name' => 'on_main_slider'
- )
- ),
- )
- );
- $categories = $categories->merge(\App\Category::all());
- //dd(old());
- foreach ($categories as $category)
- {
- $category->opt_text = $category->name;
- if(!isset($category->props)) {
- $props = array(
- 'value' => $category->id,
- );
- if(old('id_category') == $category->id)
- {
- $props['selected'] = 1;
- }
- else if (!is_null($item) && $item->id_category == $category->id)
- $props['selected'] = 1;
- $category->props = $props;
- }
- }
- if(!is_null(old('on_main_slider')))
- $on_main_slider[0]->props['checked'] = true;
- else if(!is_null($item) && $item->on_main_slider == 1)
- $on_main_slider[0]->props['checked'] = true;
- $inputs = array(
- '_token' => array(
- 'type' => 'hidden',
- 'props' => array(
- 'name' => '_token',
- 'type' => 'hidden',
- 'value' => csrf_token()
- ),
- ),
- 'title' => array(
- 'input-group-addon' => '<i class="fa fa-font"></i>',
- 'label' => 'Title',
- 'type' => 'text',
- 'parent_class' => 'input-group',
- 'props' => array(
- 'class' => 'form-control',
- 'name' => 'title',
- 'value' => $this->get_input_value($item, old('title'), 'title', ''),
- 'id' => 'title',
- 'required' => true
- ),
- ),
- 'category' => array(
- 'input-group-addon' => '<i class="fa fa-user"></i>',
- 'label' => 'Category',
- 'type' => 'select',
- 'parent_class' => 'input-group',
- 'select_props' => array(
- 'class' => 'form-control',
- 'name' => 'id_category',
- 'id' => 'id_category',
- 'required' => 1
- ),
- 'param' => 'name',
- 'opt1' => '',
- 'opts' => $categories,
- ),
- 'text' => array(
- 'input-group-addon' => '<i class="fa fa-font"></i>',
- 'value' => $this->get_input_value($item, old('text'), 'text', ''),
- 'label' => 'Text',
- 'type' => 'textarea',
- 'parent_class' => 'input-group max_viewport',
- 'props' => array(
- 'name' => 'text',
- 'id' => 'text',
- 'class' => 'form-control tinymce',
- 'rows' => '5'
- ),
- ),
- 'main_img' => array(
- 'input-group-addon' => '<i class="fa fa-euro"></i>',
- 'label' => 'Main Image <small class="text-muted">(Horizontal top)</small>',
- 'type' => 'file',
- 'parent_class' => 'input-group',
- 'props' => array(
- 'type' => 'file',
- 'name' => 'main_img',
- 'id' => 'main_img'
- ),
- ),
- 'thumbnail_img' => array(
- 'input-group-addon' => '<i class="fa fa-euro"></i>',
- 'label' => 'Thumbnail image',
- 'type' => 'file',
- 'parent_class' => 'input-group',
- 'props' => array(
- 'type' => 'file',
- 'name' => 'thumbnail_img',
- 'id' => 'thumbnail_img'
- ),
- ),
- 'on_main_slider' => array(
- 'input-group-addon' => '<i class="fa fa-tags"></i>',
- 'label' => 'On main Slider',
- 'type' => 'checkbox',
- 'param' => 'name',
- 'parent_class' => '',
- 'id' => 'on_main_slider',
- 'opts' => $on_main_slider,
- ),
- );
- return $inputs;
- }
- protected function get_page_data($form_action, $item=null) {
- $data = array(
- 'form_params' => array(
- 'action' => $form_action,
- 'button_text' => 'Add ' .$this->_param_title,
- 'inputs' => $this->get_form_inputs($item)
- ),
- );
- if($item != null) {
- $data['form_params']['button_text'] = 'Edit ' .$this->_param_title;
- $data['form_params']['inputs']['id'] = array(
- 'type' => 'hidden',
- 'props' => array(
- 'name' => 'id',
- 'type' => 'hidden',
- 'value' => $item->id
- ),
- );
- }
- return $data;
- }
- public function add() {
- $form_action = $this->_baseUrl. '/' .$this->_guard. '/' .$this->_mainTable. '/post_add';
- $this->_page_params['section_title'] = 'Add ' .$this->_param_title;
- $this->_page_params['page_title'] = 'Dashboard | Add ' .$this->_param_title;
- $dataToView = array(
- 'page_params' => $this->_page_params,
- 'tinymce' => true,
- 'data' => $this->get_page_data($form_action),
- );
- return view('dashboard/sections/form')->with($dataToView);
- }
- public function edit() {
- $model = $this->_mainModel;
- $item = $model::findOrfail(request()->id);
- $form_action = $this->_baseUrl. '/' .$this->_guard. '/' .$this->_mainTable. '/post_edit';
- $this->_page_params['section_title'] = 'Editar ' .$this->_param_title. ': ' .$item->title;
- $this->_page_params['page_title'] = 'Dashboard | Editar ' .$this->_param_title. ': ' .$item->title;
- //dd($item->images);
- $extra_form_html='';
- if (!is_null($item->main_img))
- $extra_form_html .= '<div>Main Image<a target="_blank" href="' .url($this->_uploads_folder. '/' .$item->main_img->file_name). '"><img class="image_inspect" src="' .url($this->_uploads_folder. '/' .$item->main_img->file_name). '" alt="' .$item->main_img->file_name. '"></a></div>';
- if (!is_null($item->thumbnail_img))
- $extra_form_html .= '<div>Thumbnail Image<a target="_blank" href="' .url($this->_uploads_folder. '/' .$item->thumbnail_img->file_name). '"><img class="image_inspect" src="' .url($this->_uploads_folder. '/' .$item->thumbnail_img->file_name). '" alt="' .$item->thumbnail_img->file_name. '"></a><br></div>';
- $dataToView = array(
- 'tinymce' => true,
- 'page_params' => $this->_page_params,
- 'data' => $this->get_page_data($form_action, $item),
- 'extra_form_html' => $extra_form_html,
- );
- return view('dashboard/sections/form')->with($dataToView);
- }
- // POSTS
- private function val_item($inputs, $item=null) {
- $rules = array(
- 'title' => 'required',
- 'main_img' => 'required|image',
- 'thumbnail_img' => 'required|image',
- 'id_category' => 'required',
- );
- if(!is_null($item)) {
- $rules['main_img'] = 'image';
- $rules['thumbnail_img'] = 'image';
- }
- return Validator::make($inputs, $rules);
- }
- public function post_add() {
- $inputs = request()->except('_token', 'main_img', 'thumbnail_img');
- //dd($inputs);
- $validator = $this->val_item(request()->all());
- if($validator->fails()) {
- return redirect()->back()->withInput()->withErrors($validator);
- }
- $ret = $this->base64_to_images($inputs['text']);
- $inputs['text'] = $ret['html'];
- $inputs['id_user'] = \Auth::user()->id;
- $model = $this->_mainModel;
- $new_item = $model::create($inputs);
- $this->store_imgs($ret['images'], $new_item);
- if(!is_null(request()->on_main_slider)) {
- $inputs['on_main_slider'] = 1;
- }
- $new_item->images()->createMany([
- [
- 'file_name' => $this->store_file(request()->main_img),
- 'main' => 1,
- ],
- [
- 'file_name' => $this->store_file(request()->thumbnail_img),
- 'thumbnail' => 1,
- ]
- ]);
- return redirect()->back()->with('flash_success', $this->_param_title. ' added (' .$new_item->title. ')');
- }
- public function post_edit() {
- $model = $this->_mainModel;
- $item = $model::findOrfail(request()->id);
- $inputs = request()->except('_token', 'main_img', 'thumbnail_img');
- //dd($inputs);
- $validator = $this->val_item(request()->all(), $item);
- if($validator->fails()) {
- return redirect()->back()->withInput()->withErrors($validator);
- }
- $ret = $this->base64_to_images($inputs['text']);
- $inputs['text'] = $ret['html'];
- $this->store_imgs($ret['images'], $item);
- $inputs['on_main_slider'] = 0;
- if(!is_null(request()->on_main_slider)) {
- $inputs['on_main_slider'] = 1;
- }
- $item->update($inputs);
- $name_main_img = '';
- if (!is_null($item->main_img))
- $name_main_img = $item->main_img->file_name;
- $name_thumbnail_img = '';
- if (!is_null($item->thumbnail_img))
- $name_thumbnail_img = $item->thumbnail_img->file_name;
- if(!is_null(request()->main_img)) {
- $item->images()->where('main', 1)->delete();
- $item->images()->create([
- 'file_name' => $this->store_file(request()->main_img, $name_main_img),
- 'main' => 1,
- ]);
- }
- if(!is_null(request()->thumbnail_img)) {
- $item->images()->where('thumbnail', 1)->delete();
- $item->images()->create([
- 'file_name' => $this->store_file(request()->thumbnail_img, $name_thumbnail_img),
- 'thumbnail' => 1,
- ]);
- }
- return redirect()->back()->with('flash_success', $this->_param_title. ' edited (' .$item->title. ')');
- }
- private function store_imgs($imgs, $item) {
- $del_imgs_builder = $item->images()->select(['file_name'])->where('main', 0)->where('thumbnail', 0)->whereNotIn('file_name', $imgs);
- $del_imgs = $del_imgs_builder->get();
- foreach ($del_imgs as $img) {
- $img_path = public_path($this->_uploads_folder . '/' . $img->file_name);
- if(is_file($img_path)) {
- unlink($img_path);
- }
- }
- $del_imgs_builder->delete();
- $create_imgs = [];
- foreach($imgs as $img) {
- if($item->images()->where('file_name', $img)->count() == 0) {
- $create_imgs[] = array('file_name' => $img);
- }
- }
- $item->images()->createMany($create_imgs);
- }
- private function store_file($file=null, $file_name=null) {
- if(!is_null($file)) {
- if(!is_null($file_name)) {
- $f_path = public_path($this->_uploads_folder. '/' .$file_name);
- if(is_file($f_path)) {
- unlink($f_path);
- }
- }
- // $original_f_name = $file->getClientOriginalName();
- $fileName = time(). '_' .str_random(15). '_' .$file->getClientOriginalName();
- $file->move(public_path($this->_uploads_folder), $fileName);
- return $fileName;
- }
- return $file_name;
- }
- public function delete() {
- $model = $this->_mainModel;
- $item = $model::withTrashed()->findOrFail(request()->id);
- $item->delete();
- return redirect()->back();
- }
- public function force_delete() {
- $model = $this->_mainModel;
- $item = $model::with(['images'])->withTrashed()->findOrFail(request()->id);
- foreach ($item->images as $img) {
- $img_path = public_path($this->_uploads_folder . '/' . $img->file_name);
- if(is_file($img_path)) {
- unlink($img_path);
- }
- }
- $item->images()->delete();
- $item->forceDelete();
- return redirect($this->_baseUrl. '/' .$this->_guard. '/' .$this->_mainTable. '/inactive/all');
- }
- public function restore() {
- $model = $this->_mainModel;
- $item = $model::withTrashed()->findOrFail(request()->id);
- $item->restore();
- return redirect()->back();
- }
- }
Add Comment
Please, Sign In to add comment