【问题标题】:Using Laravel Mentions (at.JS and Laravel) to return A username and their ID使用 Laravel 提及(at.JS 和 Laravel)返回用户名和他们的 ID
【发布时间】:2016-06-18 09:28:38
【问题描述】:

我目前正在构建一个评论系统,并希望包括提及用户并标记他们的能力,随后我希望标签有一个指向他们的个人资料的链接(在这个例子中,它只是 /profile/{id })

我目前正在使用 Laravel Mentions 从我的用户模型中生成自动填充列表。我这样做是这样的:

function enableMentions(elem, type, column) {

$(elem).atwho({
    at: "@",
    limit: 5,
    displayTpl: '<li><span>${name}</span></li>',
    insertTpl: '<a href="/profile/${name}" data-type="mentionable" data-id="${id}" data-name="${name}">${name}</a>',
    callbacks: {
        remoteFilter: function(query, callback) {
            if (query.length <= 1) return;

            $.getJSON("/api/mentions/" + type, {
                q: query,
                c: column
            }, function(data) {
                callback(data);
            });
        }
    }
});
}

// My api/mentions route: Route::get('/api/mentions/{type}', 'ApiController@index');

//My api controller:
    public function index($type, Request $request)
{
    try {
        $resultColumns = [];
        $query = $request->get('q');
        $column = $request->get('c');

        $model = app()->make(config('mentions.' . $type));
        $records = $model->where($column, 'LIKE', "%$query%")
            ->get();
        foreach ($records as $record) {
            $resultColumns[] = $record->$column;
        }
        return response()->json($resultColumns);
    } catch (\ReflectionException $e) {
        return response()->json('Not Found', 404);
    }
}

And finally, initializing the at.js
 <script type="text/javascript">
 $(function(){
 enableMentions("#mention-commentContent", "users", "Username");
 });
 </script>
 <div contentEditable="true" id="mention-commentContent" class="user-form" name="commentContent" type="text"></div>

我对如何修改上述内容以返回分配给他们的用户名和 ID 感到困惑,这样我就可以将 ${name} 更改为 ${id} 并将其链接到他们的个人资料,对吧?

【问题讨论】:

    标签: javascript php jquery laravel


    【解决方案1】:

    这个包可能对Laravel Mentionsthis 有所帮助。

    基本思路是通过 JSON 从数据库中获取用户名,下面是他们在laracasts 中的使用方式:

    $("textarea#body").atwho({
                at: "@", limit: 5, callbacks: {
                    remoteFilter: function (t, e) {
                        t.length <= 2 || $.getJSON("/api/users", {q: t}, function (t) {
                            e(t)
                        })
                    }
                }
            })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-18
      • 2013-10-28
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多