Put SearchTrait.php in app directory. Then use SearchTrait in your model, like so
use App\SearchTrait;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {| <?php | |
| use Illuminate\Pagination\Paginator; | |
| //This example is a bit more comlex since I have columns that are foreign keys of the Ticket table. | |
| public function index(Request $request) | |
| { | |
| if($request->ajax()) { | |
| $columns = ['tickets.id', 'client_name', 'location', 'priority_name', 'status_name', 'date']; | |
| $draw = $request->draw; | |
| $start = $request->start; //Start is the offset | |
| $length = $request->length; //How many records to show |
| # This is a sample build configuration for PHP. | |
| # Check our guides at https://confluence.atlassian.com/x/e8YWN for more examples. | |
| # Only use spaces to indent your .yml configuration. | |
| # You can specify a custom docker image from Docker Hub as your build environment. | |
| # run composer check-platform-reqs for a list of required extensions. | |
| image: php:7.2-fpm | |
| pipelines: | |
| default: | |
| # Not needed unless you're doing feature tests. | |
| # - step: |
| Comparing Solutions: Number in Array | |
| I made sure all of my solutions worked in the following scenarios: | |
| a)The given array does not contain the given number | |
| b)The given array contains a single instance of the given number | |
| c)The given array contains multiple instances of the given number | |
| function checkByLoop(arr, num){ | |
| for(var x = 0; x < arr.length; x++){ |
| <?php | |
| namespace App\Http\Controllers; | |
| use Illuminate\Http\Request; | |
| trait RestControllerTrait { | |
| public function index() { | |
| $model = self::MODEL; |
| <?php | |
| //Don't forget to change the namespace! | |
| namespace App\Traits; | |
| use Cron\CronExpression; | |
| use Illuminate\Support\Carbon; | |
| use Illuminate\Console\Scheduling\ManagesFrequencies; | |
| trait Schedulable{ |
| <?php | |
| namespace App\Http\Middleware; | |
| use Closure; | |
| use Illuminate\Http\Request; | |
| use Illuminate\Http\Response; | |
| use Symfony\Component\DomCrawler\Crawler; | |
| class PjaxMiddleware |
| <?php | |
| // This can be found in the Symfony\Component\HttpFoundation\Response class | |
| const HTTP_CONTINUE = 100; | |
| const HTTP_SWITCHING_PROTOCOLS = 101; | |
| const HTTP_PROCESSING = 102; // RFC2518 | |
| const HTTP_OK = 200; | |
| const HTTP_CREATED = 201; | |
| const HTTP_ACCEPTED = 202; |
| //SearchController.php | |
| public function autocomplete(){ | |
| $term = Input::get('term'); | |
| $results = array(); | |
| $queries = DB::table('users') | |
| ->where('first_name', 'LIKE', '%'.$term.'%') | |
| ->orWhere('last_name', 'LIKE', '%'.$term.'%') | |
| ->take(5)->get(); |
| ## Introduction | |
| This post aims to provide a simple example of how to use traits with Laravel Query Scopes. The trait will sort columns based on "sort" and "direction" being found within the request, while protecting the database of unwanted sorting by adding a "sortables" property to the model. The end result looks like this with a Post model as an example: | |
| ``` | |
| Post::sort(request()) | |
| ``` | |
| ### Sortable trait |