How to edit Query codation in voyager Browse method

Solved Posted In Voyager
  • tag-dev 1 year ago
    1 - I created custom controller from admin panel Bread for orders table
    2- my custom controller extends VoyagerBaseController  and copied  everything from VoaygerBaseController to my OrdersController and everything working good.
    -----------------------------------------------------------
    i want to change the condation of browse page query only 
    
    add more condation like
    ->where('is_active', 1)
    how do i do that 
    
    Thats The official code 
    --------
    
      //***************************************
        //               ____
        //              |  _ \
        //              | |_) |
        //              |  _ <
        //              | |_) |
        //              |____/
        //
        //      Browse our Data Type (B)READ
        //
        //****************************************
    
        public function index(Request $request)
        {
            // GET THE SLUG, ex. 'posts', 'pages', etc.
            $slug = $this->getSlug($request);
    
            // GET THE DataType based on the slug
            $dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();
    
            // Check permission
            $this->authorize('browse', app($dataType->model_name));
    
            $getter = $dataType->server_side ? 'paginate' : 'get';
    
            $search = (object)['value' => $request->get('s'), 'key' => $request->get('key'), 'filter' => $request->get('filter')];
            $searchable = $dataType->server_side ? array_keys(SchemaManager::describeTable(app($dataType->model_name)->getTable())->toArray()) : '';
            $orderBy = $request->get('order_by');
            $sortOrder = $request->get('sort_order', null);
    
            // Next Get or Paginate the actual content from the MODEL that corresponds to the slug DataType
            if (strlen($dataType->model_name) != 0) {
                $relationships = $this->getRelationships($dataType);
    
                $model = app($dataType->model_name);
                $query = $model::select('*')->with($relationships);
    
                // If a column has a relationship associated with it, we do not want to show that field
                $this->removeRelationshipField($dataType, 'browse');
    
                if ($search->value && $search->key && $search->filter) {
                    $search_filter = ($search->filter == 'equals') ? '=' : 'LIKE';
                    $search_value = ($search->filter == 'equals') ? $search->value : '%' . $search->value . '%';
                    $query->where($search->key, $search_filter, $search_value);
                }
    
                if ($orderBy && in_array($orderBy, $dataType->fields())) {
                    $querySortOrder = (!empty($sortOrder)) ? $sortOrder : 'DESC';
                    $dataTypeContent = call_user_func([
                        $query->orderBy($orderBy, $querySortOrder),
                        $getter,
                    ]);
                } elseif ($model->timestamps) {
                    $dataTypeContent = call_user_func([$query->latest($model::CREATED_AT), $getter]);
                } else {
                    $dataTypeContent = call_user_func([$query->orderBy($model->getKeyName(), 'DESC'), $getter]);
                }
    
                // Replace relationships' keys for labels and create READ links if a slug is provided.
                $dataTypeContent = $this->resolveRelations($dataTypeContent, $dataType);
            } else {
                // If Model doesn't exist, get data from table name
                $dataTypeContent = call_user_func([DB::table($dataType->name), $getter]);
                $model = false;
            }
    
            // Check if BREAD is Translatable
            if (($isModelTranslatable = is_bread_translatable($model))) {
                $dataTypeContent->load('translations');
            }
    
            // Check if server side pagination is enabled
            $isServerSide = isset($dataType->server_side) && $dataType->server_side;
    
            $view = 'voyager::bread.browse';
    
            if (view()->exists("voyager::$slug.browse")) {
                $view = "voyager::$slug.browse";
            }
    
            return Voyager::view($view, compact(
                'dataType',
                'dataTypeContent',
                'isModelTranslatable',
                'search',
                'orderBy',
                'sortOrder',
                'searchable',
                'isServerSide'
            ));
        }
    
    ---------------------
    i tasted 2 ways but both are fail 
    way 1)
    add condation like this with this like 
            $dataType = Voyager::model('DataType')->where('slug', '=', $slug)->where('is_active',1)->first();
    -------------
    way 2 )
    i try chnage voyager model like Orade::where()  for $dataType insted of voyager::model  
    $dataType = Order::where('is_active',1)->first();
    ------
    
    Thanks For your efforts and thanks for voyager team 
    
    
    

    1 - I created custom controller from admin panel Bread for orders table 2- my custom controller extends VoyagerBaseController and copied everything from VoaygerBaseController to my OrdersController and everything working good.

    i want to change the condation of browse page query only

    add more condation like ->where('is_active', 1) how do i do that

    Thats The official code

    //*************************************** // ____ // | _
    // | |) | // | _ < // | |) | // |____/ // // Browse our Data Type (B)READ // //****************************************

    public function index(Request $request)
    {
        // GET THE SLUG, ex. 'posts', 'pages', etc.
        $slug = $this->getSlug($request);
    
        // GET THE DataType based on the slug
        $dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();
    
        // Check permission
        $this->authorize('browse', app($dataType->model_name));
    
        $getter = $dataType->server_side ? 'paginate' : 'get';
    
        $search = (object)['value' => $request->get('s'), 'key' => $request->get('key'), 'filter' => $request->get('filter')];
        $searchable = $dataType->server_side ? array_keys(SchemaManager::describeTable(app($dataType->model_name)->getTable())->toArray()) : '';
        $orderBy = $request->get('order_by');
        $sortOrder = $request->get('sort_order', null);
    
        // Next Get or Paginate the actual content from the MODEL that corresponds to the slug DataType
        if (strlen($dataType->model_name) != 0) {
            $relationships = $this->getRelationships($dataType);
    
            $model = app($dataType->model_name);
            $query = $model::select('*')->with($relationships);
    
            // If a column has a relationship associated with it, we do not want to show that field
            $this->removeRelationshipField($dataType, 'browse');
    
            if ($search->value && $search->key && $search->filter) {
                $search_filter = ($search->filter == 'equals') ? '=' : 'LIKE';
                $search_value = ($search->filter == 'equals') ? $search->value : '%' . $search->value . '%';
                $query->where($search->key, $search_filter, $search_value);
            }
    
            if ($orderBy && in_array($orderBy, $dataType->fields())) {
                $querySortOrder = (!empty($sortOrder)) ? $sortOrder : 'DESC';
                $dataTypeContent = call_user_func([
                    $query->orderBy($orderBy, $querySortOrder),
                    $getter,
                ]);
            } elseif ($model->timestamps) {
                $dataTypeContent = call_user_func([$query->latest($model::CREATED_AT), $getter]);
            } else {
                $dataTypeContent = call_user_func([$query->orderBy($model->getKeyName(), 'DESC'), $getter]);
            }
    
            // Replace relationships' keys for labels and create READ links if a slug is provided.
            $dataTypeContent = $this->resolveRelations($dataTypeContent, $dataType);
        } else {
            // If Model doesn't exist, get data from table name
            $dataTypeContent = call_user_func([DB::table($dataType->name), $getter]);
            $model = false;
        }
    
        // Check if BREAD is Translatable
        if (($isModelTranslatable = is_bread_translatable($model))) {
            $dataTypeContent->load('translations');
        }
    
        // Check if server side pagination is enabled
        $isServerSide = isset($dataType->server_side) && $dataType->server_side;
    
        $view = 'voyager::bread.browse';
    
        if (view()->exists("voyager::$slug.browse")) {
            $view = "voyager::$slug.browse";
        }
    
        return Voyager::view($view, compact(
            'dataType',
            'dataTypeContent',
            'isModelTranslatable',
            'search',
            'orderBy',
            'sortOrder',
            'searchable',
            'isServerSide'
        ));
    }
    

    i tasted 2 ways but both are fail way 1) add condation like this with this like $dataType = Voyager::model('DataType')->where('slug', '=', $slug)->where('is_active',1)->first();

    way 2 ) i try chnage voyager model like Orade::where() for $dataType insted of voyager::model
    $dataType = Order::where('is_active',1)->first();

    Thanks For your efforts and thanks for voyager team

  • Best Answer

    (As selected by tag-dev)

    mark 1 year ago
    You can add your own where-statements after this line:
    ```
    $query = $model::select('*')->with($relationships);
    ```
    
    Like this:
    ```
    $query = $model::select('*')->with($relationships);
    
    $query = $query->where('is_active', true);
    ```
    
    ----------
    Best Regards,    
    Mark Topper (Maintainer of [Laravel Voyager](https://laravelvoyager.com))    
    Mail: [[email protected]](mailto:[email protected])    
    *I am available for freelance work - [hire me](mailto:[email protected])*
    

    You can add your own where-statements after this line:

    $query = $model::select('*')->with($relationships);
    

    Like this:

    $query = $model::select('*')->with($relationships);
    
    $query = $query->where('is_active', true);
    

    Best Regards,
    Mark Topper (Maintainer of Laravel Voyager)
    Mail: [email protected]
    I am available for freelance work - hire me

  • mark 1 year ago
    You can add your own where-statements after this line:
    ```
    $query = $model::select('*')->with($relationships);
    ```
    
    Like this:
    ```
    $query = $model::select('*')->with($relationships);
    
    $query = $query->where('is_active', true);
    ```
    
    ----------
    Best Regards,    
    Mark Topper (Maintainer of [Laravel Voyager](https://laravelvoyager.com))    
    Mail: [[email protected]](mailto:[email protected])    
    *I am available for freelance work - [hire me](mailto:[email protected])*
    

    You can add your own where-statements after this line:

    $query = $model::select('*')->with($relationships);
    

    Like this:

    $query = $model::select('*')->with($relationships);
    
    $query = $query->where('is_active', true);
    

    Best Regards,
    Mark Topper (Maintainer of Laravel Voyager)
    Mail: [email protected]
    I am available for freelance work - hire me

  • tag-dev 1 year ago
    okey MR Mark  i will test it thanks for answer me 

    okey MR Mark i will test it thanks for answer me

  • tag-dev 1 year ago
    Thank you so much mr Mark  it worked

    Thank you so much mr Mark it worked

  • alfredo-galano 9 months ago
    Hello, I'm a complete beginner(noob) in Laravel and study Voyager to take advantege from the features,   I guess when voyager make a BREAD it create a Controller wich extend from VoyagerBaseController, so, this is my idea: 
    **1-  Create in VoyagerBaseController**
    $_where = array(); 
    $_where_in = array(); 
    $_or_where = array(); 
    $_like = array(); 
    $_or_like = array(); 
    
    **2- In VoyagerBaseController index method**
    ........................................................
    
    $query = $model::select('*')->with($relationships);
           
    			 foreach ($this->_where as $_where) {
                $query->where($_where[0], $_where[1]);
            }
    
            foreach ($this->_where_in as $_where_in) {
                $query->where_in($_where_in[0], $_where_in[1]);
            }
    
            foreach ($this->_or_where as $or_where) {
               $query->or_where($or_where[0], $or_where[1]);
            }
    	.........................................			
    **	3- In your controller declare the values, for example  : $_where = array( array('is_active', true) ); **
    
    Doing that or something like that I believe the work will be more easy and even more people can use Voyager!

    Hello, I'm a complete beginner(noob) in Laravel and study Voyager to take advantege from the features, I guess when voyager make a BREAD it create a Controller wich extend from VoyagerBaseController, so, this is my idea: 1- Create in VoyagerBaseController $_where = array(); $_where_in = array(); $_or_where = array(); $_like = array(); $_or_like = array();

    2- In VoyagerBaseController index method ........................................................

    $query = $model::select('*')->with($relationships);

    		 foreach ($this->_where as $_where) {
            $query->where($_where[0], $_where[1]);
        }
    
        foreach ($this->_where_in as $_where_in) {
            $query->where_in($_where_in[0], $_where_in[1]);
        }
    
        foreach ($this->_or_where as $or_where) {
           $query->or_where($or_where[0], $or_where[1]);
        }
    .........................................			
    

    ** 3- In your controller declare the values, for example : $_where = array( array('is_active', true) ); **

    Doing that or something like that I believe the work will be more easy and even more people can use Voyager!

  • milfordthrt-jordan 1 month ago
    You can have various options regarding the queries that can be edited as well. Most of the times [www.bestbritishwriter.com ivory research](https://www.bestbritishwriter.com/ivoryresearch-com-review/) ivory research this link is giving the best ways to present in all demands.

    You can have various options regarding the queries that can be edited as well. Most of the times www.bestbritishwriter.com ivory research ivory research this link is giving the best ways to present in all demands.

Please login or signup to leave a response.

Login

or Sign Up

Sign Up

or Login
×
Want to create your own SAAS?

I can teach you to create your own Software as a Service. Click here to learn more!

Visit SAAS Adventure!