laravel视图和数据加载查看

时间:2015-11-29 09:32:51

标签: laravel-5 laravel-routing laravel-response

我的route.php文件中定义了一条路径,如下所示:

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

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

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

但问题是它没有正确地返回视图 当然它会返回视图并传递变量,但它会像这样影响根URL:

example.com/{username}/

我的样式表和脚本不会在页面上加载,因为laravel提供了错误的网址!
所以他们试图用这个网址加载页面:

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();
}

0 个答案:

没有答案