【问题标题】:Laravel 8 query working in development but not when deployed to HerokuLaravel 8 查询在开发中工作,但在部署到 Heroku 时没有
【发布时间】:2021-07-16 07:31:29
【问题描述】:

我在 Laravel 中有一个自动完成搜索控制器,它对搜索控制器进行 ajax 调用:

public function autocomplete(Request $request)
{ 
$data = Subject::where('title', 'LIKE', $request->subject.'%')->take(8)->get();
    $output = '<ul class="origin-top-right right-0 mt-2 w-56 shadow-lg bg-white ring-1 ring-black ring-opacity-5" style="display:block; position:relative; overflow-y: scroll;">';
    if (count($data)>0) {
        foreach ($data as $row){
            $output .= '<li><button style="text-align: left;" class="w-full block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900" role="menuitem">'.$row->title.'</button></li>';
        }
    }
    else {
        $output .= '<li><button style="text-align: left;" class="w-full block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900" role="menuitem">'.'No results'.'</button></li>';
    }
    
    $output .= '</ul>';
    return $output;
}

dev 数据库是 mysql,prod 是 pgsql,跟踪代码显示行 $data = Subject::where('title', 'LIKE', $request-&gt;subject.'%')-&gt;take(8)-&gt;get(); 在生产中返回空,但在开发中正确填写,我检查了我的 prod 数据库已正确播种并且主题模型工作正常,请帮助,谢谢!

【问题讨论】:

  • 您能否分享错误或更多详细信息以提供适当的解决方案。
  • 当然,谢谢,我用一个简单的 Country Autocomplete 重新创建了这个问题。这是 repo:github.com/vsahinid/laravel-autocomplete,这里是 heroku prod 服务器:autocomplete-laravel-prod.herokuapp.com。如果您克隆应用程序并在本地运行它,它可以工作,但是当您尝试从自动完成中选择一个国家时,它就不起作用了。

标签: php database heroku laravel-8 production


【解决方案1】:

我已经更新了你的控制器文件替换这个。

问题是,在 localhost 小写或大写中,当在实时服务器中大写和小写都是不同的值时,它可以正常工作,所以现在我已经解决了你的解决方案。

国家控制器

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Country;
use DB;

class CountryController extends Controller
{
    public function autocomplete(Request $request)
    { 
        // return Country::all();
      if($request->ajax()) {
        //$data = Country::where('name', 'LIKE', $request->country.'%')->take(10)->get();
        $data = Country::where(DB::raw('LOWER(name)'), 'like',  strtolower($request->country) . '%')->limit(10)->get();
    //dd($data->toArray());
        $output = '<ul class="origin-top-right right-0 mt-2 w-56 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5" style="display:block; position:relative; overflow-y: scroll;">';
    if (count($data)>0) {
        foreach ($data as $row){
            $output .= '<li><button style="text-align: left;" class="w-full block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900" role="menuitem">'.$row->name.'</button></li>';
        }
    }
    else {
        $output .= '<li><button style="text-align: left;" class="w-full block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900" role="menuitem">'.'No results'.'</button></li>';
    }

    $output .= '</ul>';
    return $output;
  }
}

public function update_countries(Request $request){
    $countries = Country::all();
    dd($request);
}
}

【讨论】:

  • 非常感谢,这很有意义!
  • 我已经在实时服务器上进行了测试,现在也可以享受我的编码了。
猜你喜欢
  • 1970-01-01
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
  • 2012-04-30
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
  • 1970-01-01
相关资源
最近更新 更多