mardi 15 décembre 2020

Laravel model function best prickets

im new in Laravel , I have an issue as below I make in category model query to check is category is exist or not as below

 public function scopeIsExist($query ,$id)
    {
        return $query->where(['deleted' => 1, 'id' => $id])->orderBy('id', 'DESC')->first();
    }

and my controller is

    public function edit($id)
    {


        $dataView['category'] = Category::IsExist($id);    

        if(!$dataView['category'])
        {
            return view('layouts.error');
        }else{
            $dataView['title'] = 'name';
            $dataView['allCategories'] = Category::Allcategories()->get();
            return  view('dashboard.category.edit')->with($dataView);
        }

    }

my problem is when I use method isEXIST if id not found it not redirect to error page but ween i remove ISEXIST AND replace it as below

$dataView['category'] = Category::where(['deleted' => 1, 'id' => $id])->orderBy('id', 'DESC')->first();

it work well . can any one help me



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire