php - Laravel 5.2 数据库查询中的条件

标签 php mysql database laravel-5

当有人在文本框中写入条目时,我想检查他们使用的单词并在我拥有的常用单词数据库中找到匹配项。数据库中的每个单词都有一个我需要它返回的数值。

我需要编写一个 if 语句来检查查询是否返回值,如果是,我需要它返回 -1。

这是我到目前为止所拥有的,但现在它只返回一个单词。

    Entry::create($request->all());

    // GETTING THE ARRY OF WORDS FROM entry_body( $entry_text)
    $entry_text = $request->only('entry_body');

    // REMOVE THE KEY AND JUST GET THE STRING VALUE
    $entry_text = array_pull($entry_text,"entry_body");
    $entry_noTag = strip_tags($entry_text);

    // CONVERTE THE STRING ARRAY
    $entry_explode = explode(" ", $entry_noTag);

    // EACH WORD GETS ANALAYZED 
    $matched_words = Words::whereIn('word', $entry_explode)->get();

    foreach ($entry_explode as $word) {
        if ($matched_words == false){
            return '-1';
        }
        else {
            return $word;
        }
    };

我可以获得返回匹配值的代码。我需要它做的是,当数组中的单词不匹配时,它应该返回-1,这样我就知道在数据库中找不到该单词。

最佳答案

当然,您的第一次返回将返回并结束该函数。将所有值放入数组中,如下所示:

$arrayOfWords = array();
foreach ($matched_words as $word) {
    if (in_array($word->word,$entry_explode)) {
        $arrayOfWords['found'][] = $word->word;
    } else {
        $arrayOfWords['notFound'][] = $word->word;
    }
}

return $arrayOfWords;

关于php - Laravel 5.2 数据库查询中的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37079811/

相关文章:

php - 在laravel 5中搜索belongsToMany关系

php - 将 JavaScript 数组发送到 PHP

php - 使用 php 将复选框输入插入 postgresql

MySQL 处理按小时定义的休息时间

c# - 保护 MYSQL 表免遭不当访问

php - Pthreads PHP : execute Foreach Loop in Parallel

php - 在 PHP MYSQL 中跟踪对话

php - 启用文件信息扩展时的 Laravel "Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?)"

database - 什么是表格表达式语句?

asp.net - SubmitChanges() 上的 LINQ to Entities 超时