好了,由於某種原因,我得到:本地主機/ new_laravel_social_site /公/用戶/%7Busername%7D爲什麼我收到%7Busername%7D在我的網址的結尾
我應該得到這樣的: 本地主機/ new_laravel_social_site/public/user /用戶名
有很多代碼,但我正在做codecoarse laravel社交媒體網站。我點擊時間線上的人,然後出現。但是,如果我點擊他們的個人資料,那麼它工作得很好我會將代碼發佈到哪裏。
它打破了我{{$ reply-> user-> getNameOrUsername()}}的地方。我有一切工作,這與我使用$回覆。
編輯:所有路線現在在底部。
@foreach ($statuses as $status)
<div class="media">
<a class="pull-left" href="{{ route('profile.index'), ['username' => $status->user->username] }}">
<img class="media-object" alt="{{ $status->user->getNameOrUsername() }}" src=" {{ $status->user->getAvatarUrl(40) }}">
</a>
<div class="media-body">
<h4 class="media-heading"><a href="{{ route('profile.index'), ['username' => $status->user->username] }}">{{ $status->user->getNameOrUsername() }}</a></h4>
<p>{{ $status->body }}</p>
<ul class="list-inline">
@if ($status->created_at->diffInMonths(\Carbon\Carbon::now()) >= 1)
<li>{{ $status->created_at->format('j M Y, g:ia') }}</li>
@else
<li>{{ $status->created_at->diffForHumans()}}</li>
@endif
<li><a href="#">Like</a></li>
<li>10 likes</li>
</ul>
@foreach ($status->replies as $reply)
<div class="media">
<a class="pull-left" href="{{ route('profile.index', ['username'=> $reply->user->username]) }}">
<img class="media-object" alt="{{ $reply->user->getNameOrUsername() }}" src="{{ $reply->user->getAvatarUrl(30) }}">
</a>
<div class="media-body">
<h5 class="media-heading"><a href="{{ route('profile.index', ['username' => $reply->user->username]) }}">{{ $reply->user->getNameOrUsername() }}</a></h5>
<p>{{ $reply->body }}</p>
<ul class="list-inline">
@if ($reply->created_at->diffInMonths(\Carbon\Carbon::now()) >= 1)
<li>{{ $reply->created_at->format('j M Y, g:ia') }}</li>
@else
<li>{{ $reply->created_at->diffForHumans()}}</li>
@endif
<li><a href="#">Like</a></li>
<li>4 likes</li>
</ul>
</div>
</div>
@endforeach
<form role="form" action="{{ route('status.reply', ['statusId' => $status->id]) }}" method="post">
<div class="form-group{{ $errors->has("reply-{$status->id}") ? ' has-error': '' }}">
<textarea name="reply-{{ $status->id }}" class="form-control" rows="2" placeholder="Reply to this status"></textarea>
@if ($errors->has("reply-{$status->id}"))
<span class="help-block">{{ $errors->first("reply-{$status->id}")}}</span>
@endif
</div>
<input type="submit" value="Reply" class="btn btn-default btn-sm">
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
</div>
@endforeach
{!! $statuses->render() !!}
@foreach ($status->replies as $reply)
<div class="media">
<a class="pull-left" href="{{ route('profile.index', ['username' => $user->username]) }}">
<img class="media-object" alt="{{ $reply->user->getNameOrUsername() }}" src="{{ $reply->user->getAvatarUrl(30) }}">
</a>
<div class="media-body">
<h5 class="media-heading"><a href="{{ route('profile.index', ['username' => $reply->user->username]) }}">{{ $reply->user->username }}</a></h5>
<p>{{ $reply->body }}</p>
<ul class="list-inline">
@if ($reply->created_at->diffInMonths(\Carbon\Carbon::now()) >= 1)
<li>{{ $reply->created_at->format('j M Y, g:ia') }}</li>
@else
<li>{{ $reply->created_at->diffForHumans()}}</li>
@endif
<li><a href="#">Like</a></li>
<li>4 likes</li>
</ul>
</div>
</div>
@endforeach
模型
Stat.php:
<?php
namespace Chatty\Models;
use Illuminate\Database\Eloquent\Model;
class Status extends Model
{
protected $table = 'statuses';
protected $fillable = [
'body'
];
public function user()
{
return $this->belongsTo('Chatty\Models\User', 'user_id');
}
public function scopeNotReply($query)
{
return $query->whereNull('parent_id');
}
public function replies()
{
return $this->hasMany('Chatty\Models\Status', 'parent_id');
}
}
user.php的
<?php
namespace Chatty\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
class User extends Model implements AuthenticatableContract
{
use Authenticatable;
protected $table = 'users';
protected $fillable = [
'username',
'email',
'password',
'confirm_password',
'first_name',
'last_name',
'location',
];
protected $hidden = [
'password',
'confirm_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 getNameOrUsername()
{
return $this->getName() ?: $this->username;
}
public function getFirstNameOrUsername()
{
return $this->first_name ?: $this->username;
}
public function getAvatarUrl($size)
{
return "https://www.gravatar.com/avatar/{{ md($this->email) }}?d=mm&s=$size";
}
public function statuses()
{
return $this->hasMany('Chatty\Models\Status', 'user_id');
}
public function friendsOfMine()
{
return $this->belongsToMany('Chatty\Models\User', 'friends', 'user_id', 'friend_id');
}
public function friendOf()
{
return $this->belongsToMany('Chatty\Models\User', 'friends', 'friend_id', 'user_id');
}
public function friends()
{
return $this->friendsOfMine()->wherePivot('accepted', true)->get()->merge($this->friendOf()->wherePivot('accepted', true)->get());
}
public function friendRequest()
{
return $this->friendsOfMine()->wherePivot('accepted', false)->get();
}
public function friendRequestPending()
{
return $this->friendOf()->wherePivot('accepted', false)->get();
}
public function hasFriendRequestPending(User $user)
{
return (bool) $this->friendRequestPending()->where('id', $user->id)->count();
}
public function hasFriendRequestRecieved(User $user)
{
return (bool) $this->friendRequest()->where('id', $user->id)->count();
}
public function addFriend(User $user)
{
$this->friendOf()->attach($user->id);
}
public function acceptFriendRequest(User $user)
{
$this->friendRequest()->where('id', $user->id)->first()->pivot->
update([
'accepted' => true,
]);
}
相關途徑:
Route::post('/status/{statusId}/reply', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'status.reply',
'middleware' => ['auth'],
]);
Route::get('/user/{username}', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'profile.index',
]);
這是我StatusController裏面的方法,我用:
public function postReply(Request $request, $statusId)
{
$this->validate($request, [
"reply-{$statusId}" => 'required|max:1000',
], [
'required' => 'You must have content to reply',
]);
$status = Status::notReply()->find($statusId);
if(!$status) {
return redirect()->route('home');
}
if(!Auth::user()->isFriendsWith($status->user) && Auth::user()->id !== $status->user->id) {
return redirect()
->route('home')
->with('info', 'Must be a friend to reply to this status, or be signed in');
}
$reply = Status::create([
'body' => $request->input("reply-{$statusId}"),
])->user()->associate(Auth::user());
$status->replies()->save($reply);
return redirect()->back()->with('info',"Reply has been comented!");
}
所有路線:
<?php
/**
* Home
**/
Route::get('/', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'home',
]);
/*
* Authentication
*/
Route::get('/signup', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'auth.signup',
'middleware' => ['guest'],
]);
Route::post('/signup', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'middleware' => ['guest'],
]);
Route::get('/signin', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'auth.signin',
'middleware' => ['guest'],
]);
Route::post('/signin', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'middleware' => ['guest'],
]);
Route::get('/signout', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'auth.signout',
]);
/*
* search
*/
Route::get('/search', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'search.results',
]);
/*
* Profiles
*/
Route::get('/user/{username}', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'profile.index',
]);
Route::get('/profile/edit', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'profile.edit',
'middleware' => ['auth'],
]);
Route::post('/profile/edit', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'profile.edit',
'middleware' => ['auth'],
]);
/*
* friends
*/
Route::get('/friends', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'friends.index',
'middleware' => ['auth'],
]);
Route::get('/friends/add/{username}', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'friends.add',
'middleware' => ['auth'],
]);
Route::get('/friends/accept/{username}', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'friends.accept',
'middleware' => ['auth'],
]);
/*
* Statuses
*/
Route::post('/status', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'status.post',
'middleware' => ['auth'],
]);
Route::post('/status/{statusId}/reply', [
'uses' => '\Chatty\Http\Controllers\[email protected]',
'as' => 'status.reply',
'middleware' => ['auth'],
]);
如果您需要更多的代碼,請讓我知道你需要什麼=] – NewbieCoder