A sample of BDs in laravel, depending on the input, is as clear as possible



  • I've got a function of a missing list of maps from the base. It needs to be improved so that when the field is transferred through the $request fields to search (the series, the number, balance) a request is made (the fields may be more, if the other is not an option).

    public function card_list(Request $request)
    {
        $q = $request->input('card') ;
    
    $this->data['cards'] = Card::where('id', '>', 0)->paginate(15);
    
    return view('pages.card_list', $this->data);
    

    }



  • That's it. https://github.com/xEdelweiss/my-perfect-back-end/blob/master/app/Traits/Model/FindByRequestTrait.php I:

    trait FindByRequestTrait {
        public function scopeFindByRequest($query, $request = NULL)
        {
            if (is_null($request)) {
                $request = Input::all();
            }
    
        $findable = isset($this->findable) ? $this->findable : [];
    
        foreach ($request as $field => $value) {
            if (!in_array($field, $findable)) {
                continue;
            }
    
            // специфические условия обрабатываются отдельно
            // ... см. по линке
    
            // остальные вот так
            if (is_array($value)) {
                $query->whereIn($field, $value);
            } elseif (is_scalar($value)) {
                $query->where($field, '=', $value);
            }
        }
    }
    

    }

    phpDoc and tegam search are removed from the example, but in github.

    The model should add a new characteristic. $findable With the fields on which the track can be searched and connected:

    class Card extends Model
    {
    use FindByRequestTrait;

    protected $findable = ['id', ...];
    
    // ...
    

    }

    Use:

    Card::findByRequest()->paginate(15);




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2