0

我在我的 route.php 文件中定义了一个路由,如下所示:

Route::get('{username}/{search_term}', 'userProfileSearchController@search');

在我的控制器中,我通过用户名从 db 检索用户模型,然后执行搜索查询以根据传递的术语查找结果,然后我在控制器中返回如下视图:

return view('view', compact('result'));

但问题是它不能正确返回视图。
当然它也会返回视图并传递变量,但它会像这样影响根 url:

example.com/{username}/

并且我的样式表和脚本没有加载到页面上,因为 laravel 给出了错误的 url!
所以他们试图用这个网址加载页面:

example.com/{username}/js/jquery.js

更新
这是我的控制器:

<?php

namespace App\Http\Controllers;

use App\User;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\Interfaces\UserRepositoryInterface;

class SearchController extends Controller
{
     protected $user;

     public function __construct(UserRepositoryInterface $user)
     {
          $this->user = $user;
     }
     /**
      * Searching the user profile and sending the results
      *
      * @return \Illuminate\Http\Response
      */
      public function userProfileSearch($username, $search_term)
      {
           $result = $this->user->getUserProfileSearchResult($username, $search_term);
           return view('view', compact('result'));
      }
}

和我的用户存储库方法:

public function getUserProfileSearchResult($username, $search_term)
{
    $user = $this->user->where('username', $username)->firstOrFail();
    $userHomes = $user->homes()->searchUserHomes($search_term);
    return $userHomes;
}

最后是我的模型和 searchUserHomes 范围:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Home extends Model
{
     protected $table = 'homes';
     protected $fillable = ['street', 'city', 'zip_code', 'country', 'state', 'price', 'description'];
     protected $hidden = ['created_at', 'update_at'];

     public function scopeSearchUserHomes($query, $search_term)
     {
          $search_term = str_replace('-', ' ', $search_term);
          $query->where(function($query) use($search_term) {
               $query->where('street', 'LIKE', '%'.$search_term.'%');
          });
          for($i=1; $i<count($this->fillable); $i++)
          {
               $query->orWhere(function($query) use($i, $search_term){
               $query->where($this->fillable[$i], 'LIKE', '%'.$search_term.'%');
          });
      }
      return $query->get();
}
4

0 回答 0