Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement the default selection of select box in laravel-admin

Shulou Source: shulou.com Published: 2022-06-01 02:30:13 10月04日 Update

This article introduces the relevant knowledge of "how to realize the default selection of select frame in laravel-admin". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The code is as follows:

Protected function form () {return Admin::form (Member::class, function (Form $form) {/ / $form- > display ('id',' ID'); $form- > text ('mac',' terminal mac'); $form- > text ('phone',' mobile number'); $form- > text ('password',' password'); / / determine whether edit $id = Request::route ('member'); $place_id ='' $rule_id =''; if ($id) {$model = $form- > model ()-> find ($id); $place_id = $model- > place_id; $rule_id = $model- > rule_id;} $form- > select ('place_id',' belongs')-> options ("/ api/getPlace/$place_id"); $form- > select ('rule_id',' Rule')-> options ("/ api/getRule/$rule_id") });} / implementation of api public function getPlace (Request $request) {$name = $request- > get ('p'); $id = $request- > id; $where = []; if ($name) {$where = ['name','like', "% $name%"];} $places = Place::where ($where)-> get (); $result = []; foreach ($places as $key= > $value) {$result [$key] [' id'] = $value- > Id $result [$key] ['text'] = $value- > name; if ($value- > Id = = $id) {$result [$key] [' selected'] = true;}} if (! $id) {array_unshift ($result, ['id'= >', 'text'= >' Please choose a place', 'selected'= > true]) } else {array_unshift ($result, ['id'= >'', 'text'= >' Please choose a place']);} return response ()-> json ($result);}

Pay attention

Response ()-> json () must be returned when it is returned, otherwise the type returned will be text/html.

This is the end of the content of "how to achieve the default selection of select boxes in laravel-admin". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Place content more knowledge type choice practicality achievement next code dilemma actual password situation ownership mobile phone mobile number article time case Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Microsoft Docker Apple Linux