简体   繁体   中英

Conditional BREAD in Laravel / Voyager

Is there a way to apply certain conditions to the list of records displayed by Voyager's BREAD? For example, only display records where a certain column is blank (ie WHERE 'col_name' IS NULL )?

UPDATE (17/12/18):


You can override the controller and add the where clause. For example, if you need to override "Posts" list, then you should:

  1. Make a custom PostController: php artisan make:controller PostController
  2. Extend it from Voyager BREAD controller: class PostController extends \\TCG\\Voyager\\Http\\Controllers\\VoyagerBaseController
  3. Import Voyager's Facade: use TCG\\Voyager\\Facades\\Voyager;
  4. Copy the whole index function from VoyagerBaseController. You can find it in \\vendor\\tcg\\Voyager\\Http\\Controllers\\VoyagerBaseController
  5. Override it to add any logic or filter you need, for example:

$dataType = Voyager::model('DataType') ->where('slug', '=', $slug) ->where('col_name', '=', NULL) ->first();

You can also set $orderBy = 'col_name' and $sortOrder = 'asc'/'desc' custom values in the same function. Here Voyager's docs.

OLD:


Yes, there is. You need to edit the view and apply a condition filter. Here is explained how to override views (and controllers if you want to filter it before sending the data to the view).

Just define a Scope in your model:

public function scopeMyScope($query)
{
    return $query->where('col_name', null);
}

then go to Bread Form and select it form Scope drop down :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM