我的路线:
Route::get('/user/{username}', [
'uses' => '\MostWanted\Http\Controllers\ProfileController@getProfile',
'as' => 'profile.index',
]);`
我的控制器:
use MostWanted\Models\User;
use Illuminate\Http\Request;
class ProfileController extends Controller{
public function getProfile($username){
$user = User::where('username', $username)->first();
if (!$user) {
abort(404);
}
return view('profile.index')
->with('user', $user);
}
}
数据库中的用户名字段存在且已填写。当我进入搜索时,用户会弹出,但URL始终在{username}上未命中。当我手动输入{username}时,例如mostwanted.dev:8000/user/yes.man
,它正常运作。
编辑:这是挂钩到(userblock.blade.php)
的视图<div class="media">
<a class="pull-left" href="{{ route('profile.index', ['username' => $user->username]) }}">
<img class="media-object" alt="{{ $user->getNameOrUsername() }}" src="">
</a>
<div class="media-body">
<h4 class="media-heading">
<a href="{{ route('profile.index', ['username' => $user->username]) }}">{{ $user->getNameOrUsername() }}</a>
</h4>
@if ($user->location)
<p>{{ $user->location }}</p>
@endif
</div>
这是User.php:
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
protected $table = 'users';
protected $fillable = [
'username',
'first_name',
'last_name',
'email',
'password',
'location',
'gender',
'date_of_birth',
];
protected $hidden = [
'password',
'remember_token',
];
public function getName()
{
if ($this->first_name && $this->last_name) {
return "{$this->first_name} {$this->last_name}";
}
if ($this->first_name) {
return $this->first_name;
}
return null;
}
public function getUsername()
{
if ($this->first_name && $this->last_name) {
return "{$this->first_name}.{$this->last_name}";
}
if ($this->first_name) {
return $this->first_name;
}
return null;
}
public function getNameOrUsername()
{
return $this->getName() ?: $this->username;
}
public function getUsernameOrName()
{
return $this->getUsername() ?: $this->username;
}
public function getFirstNameOrUsername()
{
return $this->first_name ?: $this->username;
}
}
编辑**:搜索结果视图:
@extends('templates.default')
@section('content')
<h3>Your search for "{{ Request::input('query') }}"</h3>
@if (!$users->count())
<p>No results found.</p>
@else
<div class="row">
<div class="col-lg-12" style="padding: 50px;">
@foreach ($users as $user)
@include('user/partials/userblock')
@endforeach
</div>
</div>
@endif
@stop
SearchController:
use DB;
use MostWanted\Models\User;
use Illuminate\Http\Request;
class SearchController extends Controller
{
public function getResults(Request $request)
{
$query = $request->input('query');
if (!$query) {
return redirect()->route('home');
}
$users = User::where(DB::raw("CONCAT(first_name, ' ', last_name)"),
'LIKE', "%{$query}%")
->orWhere('username', 'LIKE', "%{query}%")
->get();
return view('search.results')->with('users', $users);
}
}
EDIT ***:
ProfileController可:
use MostWanted\Models\User;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
public function getProfile($username)
{
$user = User::where('username', $username)->first();
if (!$user) {
abort(404);
}
return view('profile.index')
->with('user', $user);
}
}
答案 0 :(得分:1)
尝试修改 userblock.blade.php
<div class="media">
<a class="pull-left" href="{{ url('user/'.$user->username) }}">
<img class="media-object" alt="{{ $user->getNameOrUsername() }}" src="">
</a>
<div class="media-body">
<h4 class="media-heading">
<a href="{{ url('user/'.$user->username) }}">{{ $user->getNameOrUsername() }}</a>
</h4>
@if ($user->location)
<p>{{ $user->location }}</p>
@endif
</div>