Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

Laravel Backpack select2_from_ajax select change the result and default value

For the backpack-demo, I would like to show the ID and Title together. For the search result no issue, selected at the select2 also no issue, the problem is after save, it will not display, it become blank.

How can I set it?

Like below blank data

backpack-demo/app/Http/Controllers/Admin/MonsterCrudController.php

[ // select2_from_ajax: 1-n relationship
                'label'                => 'Select2_from_ajax', // Table column heading
                'type'                 => 'select2_from_ajax',
                'name'                 => 'select2_from_ajax', // the column that contains the ID of that connected entity;
                'entity'               => 'article', // the method that defines the relationship in your Model
                'attribute'            => 'id_title', // foreign key attribute that is shown to user
                'model'                => "BackpackNewsCRUDappModelsArticle", // foreign key model
                'data_source'          => url('api/article'), // url to controller search function (with /{id} should return model)
                'placeholder'          => 'Select an article', // placeholder for the select
                'minimum_input_length' => 2, // minimum characters to type before querying results
                'tab'                  => 'Relationships',
                'wrapperAttributes'    => ['class' => 'form-group col-md-12'],
            ],

backpack-demo/app/Http/Controllers/Api/ArticleController.php

        if ($search_term) {
            return Article::
            selectRaw("CONCAT('Article ID: ', id,' | Title: ', Title) AS id_title, articles.*")
            ->where('title', 'LIKE', '%'.$search_term.'%')->paginate(10);
        } else {
            return Article::selectRaw("CONCAT('Article ID: ', id,' | Title: ', Title) AS id_title, articles.*")
            ->paginate(10);
        }

select2_from_ajax


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The solution is at the Article Model create another accessor function

public function getIdTitleAttribute()
{
    return 'Article ID: ' . $this->id . ' | Title: ' . $this->title;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...