我有一个问题数据库,可以在localhost:8000 / questions / {id}中查看。我在现有的laravel项目中创建了一个聊天机器人。现在,我想向用户提供问题的链接。 例如,如果我要链接到id = 55的问题,则漫游器必须以链接localhost:8000 / questions / 55答复我。 我该怎么办?
web.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());
}
}
答案 0 :(得分:0)
首先,我们从问题表中获取所有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
我希望这对您有帮助