对这个项目有一个真正令人困惑的时间,我的问题是我试图让我的搜索工作,但由于某种原因,当数据库中存在该信息时,它也没有从我的查询中提取结果,当我点击在我的页面的右上角的用户名,它应该重定向到用户页面,但我得到这个错误“Application.php第879行中的NotFoundHttpException:”,URl看起来像这样“http://localhost/WorldLink/users/firstName%20=%3E%20Auth::user%28%29-%3EfirstName”,我已经筋疲力尽了尝试修复它的其他方法让我回来寻求帮助!我的代码在下面使用laravel 5.2:
Users.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Users extends Model
{
protected $table = 'users';
protected $fillable = [
'id', 'firstName', 'lastName', 'bio', 'homeLocation', 'currentLocation', 'email', 'password',
];
public function getName()
{
if ($this->firstName && $this->lastName) {
return "{$this->firstName} {$this->lastName}";
}
if ($this->firstName) {
return $this->firstName;
}
return null;
}
public function getNameOrLocation()
{
return $this->getName() ?: $this->currentLocation;
}
public function getFirstNameOrLocation()
{
return $this->firstName ?: $this->currentLocation;
}
public function getAllAvatarsUrl()
{
return "https://www.gravatar.com/avatar/{{ md5($this->email) }}?d=mm&s=40";
}
}
SearchController.php:
<?php
namespace App\Http\Controllers;
use DB;
use App\Users;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
class SearchController extends BaseController
{
public function getResults(Request $request)
{
$query = $request->input('query');
if (!$query) {
return back();
}
$users = Users::where(DB::raw("CONCAT(firstName, ' ', lastName)"), '
LIKE', "%{$query}%")
->orWhere('currentLocation', 'LIKE', "%{$query}%")
->get();
return view('search/results')->with('users', $users);
}
ProfileController.php
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
class ProfileController extends BaseController
{
public function getProfile($firstName)
{
$users = User::where('firstName', $firstName)->first();
if (!$users) {
abort(404);
}
return view('profile.index')
->with('users', $users);
}
}
userblock.blade.php
<div class="media">
<a class="pull-left" href="{{ route('profile/index', ['firstName' => $users->firstName]) }}">
<img class="media-object" alt="{{ $users-getNameOrLocation() }}" src="{{ $users->getAllAvatarsUrl() }}">
</a>
<div class="media-body">
<h4 class="media-heading"><a href="{{ route('profile/index', ['firstName' => $users->firstName]) }}">{{ $users->getNameOrLocation() }}</a></h4>
</div>
@if ($users->currentLocation)
<p>{{ $users->currentLocation }}</p>
@endif
results.blade.php
@extends('layouts.app')
@section('content')
<h3>Search Results for "{{ Request::input('query') }}"</h3>
@if (!$users->count())
<p>No Results Found</p>
@else
<div class="row">
<div class="col-lg-12">
@foreach ($users as $user)
@include('users/partials/userblock')
@endforeach
</div>
</div>
@endif
@endsection
最后我的两条路线,这个问题在这里连接起来我无法找到它出错的地方。
Route::get('/search', [
'uses' => '\App\Http\Controllers\SearchController@getResults',
'as' => 'search/results',
]);
Route::get('/users/{firstName}', [
'uses' => '\App\Http\Controllers\ProfileController@getProfile',
'as' => 'profile/index',
]);
链接:
@if (Auth::guest())
<li><a href="{{ url('/login') }}">Login</a></li>
<li><a href="{{ url('/register') }}">Register</a></li>
@else
<ul class="nav navbar-nav">
<form class="navbar-form navbar-left" role="search" action="{{ route('search/results') }}">
<input type="text" class="form-control" placeholder="Search" name="query">
</form>
<li><a href="{{ route('profile/index', ['firstName => Auth::user()->firstName']) }}">{{ Auth::user()->firstName }}</a></li>
<li><a href="{{ url('/home') }}">Timeline</a></li>
<li><a href="{{ url('/link') }}">Link</a></li>
<li><a href="{{ url('/journeys') }}">Journeys <span class="journey-num">{{ Auth::user()->journeys }}</span></a></li>
<li><a href="{{ url('/forum') }}">Forum</a></li>
</ul>
答案 0 :(得分:1)
Defo引用错误
....
<li><a href="{{ route('profile/index', ['firstName' =>Auth::user()->firstName]) }}">{{ Auth::user()->firstName }}</a></li>
....
注意在firstName数组键之后关闭'
。
至少应该修复链接