i have a route defined in my route.php file like this:
Route::get('{username}/{search_term}', 'userProfileSearchController@search');
in my controller i retrieve the user model from db by the username and then performing a search query for finding the results based on the passed term then i return to a view like below in my controller:
return view('view', compact('result'));
but the problem is it doesn't return the view properly.
of course it returns the view and pass the variable too but it affects the root url like this:
example.com/{username}/
and my stylesheets and scripts doesn't load on page because of the wrong url laravel is giving!
so they trying to load in page with this url:
example.com/{username}/js/jquery.js
UPDATE
this is my controller:
<?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'));
}
}
and my user repository method:
public function getUserProfileSearchResult($username, $search_term)
{
$user = $this->user->where('username', $username)->firstOrFail();
$userHomes = $user->homes()->searchUserHomes($search_term);
return $userHomes;
}
and finally my model and the searchUserHomes scope:
<?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();
}