laravel-5 - 在 laravel 中提供页面的链接
问题描述
我有一个问题数据库,可以在 localhost:8000/questions/{id} 中查看。我在现有的 laravel 项目中创建了一个聊天机器人。现在,我想向用户提供问题的链接。例如,如果我想要一个 id=55 的问题的链接,那么机器人必须使用链接 localhost:8000/questions/55 回复我。我怎么做?
网页.php
Route::resources([ 'questions' => 'QuestionController', ]);
Route::match(['get', 'post'], '/botman', 'BotManController@handle');
QuestionController.php
public function show(Question $question) {
return view('question')->with('question', $question);
}
botman.php
use BotMan\BotMan\BotMan;
use BotMan\BotMan\BotManFactory;
use BotMan\BotMan\Cache\DoctrineCache;
use BotMan\BotMan\Drivers\DriverManager;
use App\Conversations\StartConversation;
DriverManager::loadDriver(\BotMan\Drivers\Web\WebDriver::class);
$cachedriver = new Doctrine\Common\Cache\PhpFileCache('cache');
BotManFactory::create(config('botman', new
DoctrineCache($cachedriver)));
$botman = app('botman');
$botman->hears('Hello|Hi',
function($bot) {
$bot->typesAndWaits(1);
$bot->startConversation(new StartConversation);
}
);
BotManController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use BotMan\BotMan\BotMan;
use BotMan\BotMan\BotManFactory;
use BotMan\BotMan\Messages\Conversations;
use App\Conversations\StartConversation;
class BotManController extends Controller {
public function handle() {
$botman = app('botman');
$botman->listen();
}
public function startConversation(Botman $bot) {
$bot->startConversation(new StartConversation());
}
}
解决方案
首先,我们从问题表中获取所有 id:
$questions = DB::table('questions')->select('id')->where('body', 'like', '%' . $answer . '%')->get();
$ids 这里是 id 的集合,所以我们必须为每个 id 创建一个链接:
$links = array();
foreach($questions as $question){
$links[] = route('questions.show', ['id' => $question->id]);
}
所以现在我们有所有需要作为答案返回的链接,使用$this->say
...完成它,如你所愿
您可能希望返回第一个链接而不是所有链接,然后从数据库中获取第一个 id 并使用它创建链接:
$question = DB::table('questions')->select('id')->where('body', 'like', '%' . $answer . '%')->first()
$link = route('questions.show', ['id' => $question->id]);
然后使用返回答案$this->say
我希望这有帮助
推荐阅读
- excel - StopTimer() 不工作。需要一种在 excel 中重新启动计时器的方法
- angular - 如何在 Angular 2+ 中将 Reactive Form 值与带下划线的模型同步
- c# - 为什么 Queryable.OrderBy 方法允许动态第二个参数?
- c - 使用 SLLI 访问循环中的数组元素
- python - 如何拆分'; ' 在 pandas 中使用 read_csv() 后在 pandas 中分离 CSV 文件?
- c# - 如何使用触发器更改按钮内容的属性
- java - 多个 setvalue 的完成侦听器
- azure-api-management - API 管理 使用 DevOPS 定制新的开发者门户
- android-constraintlayout - 如何为浮点类型定义资源并在“layout_constraintWidth_percent”的数据绑定中使用它?
- ruby-on-rails - 向正则表达式添加特殊字符