У меня небольшая проблема. Я создал миграцию, модель и т. Д. Для друга, когда я добавляю друга, все в порядке, но я не знаю, как это сделать для конкретного пользователя в друзьях, тогда я не могу добавить его снова. Тогда просто не вижу кнопки.
Это моя кнопка:
@if($user->off_friends != 1)
@if(!\Auth::guest() && $user->id != \Auth::id())
@if($user->hasFriend($user->id))
{!! Form::open(array('method'=>'post', 'route' => array('store.friend', $user->id))) !!}
{!! Form::hidden('friend_id', $user->id) !!}
{!! Form::hidden('friend_name', $user->name) !!}
{!! Form::hidden('friend_avatar', $user->avatar) !!}
{!! Form::submit('Dodaj znajomego', ['class' => 'btn btn-primary btn-xs']) !!}
{!! Form::close() !!}<BR />
@endif
@endif
@endif
И это моя функция:
public function hasFriend($id)
{
foreach($this->friend as $friend)
{
if(\Auth::id() == $friend->user_id && $friend->friend_id == $id)
{
return true;
}
}
}
Я жду ответа, это важно, спасибо!
РЕДАКТИРОВАТЬ::
Модель пользователя:
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable =
[
'name',
'email',
'password',
'avatar',
'age',
'sex',
'signature',
'skype',
'facebook',
'twitter',
'xfire',
'steam',
'origin',
'duty',
'off_messages',
'off_comments',
'off_friends',
'off_online'
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
public function player()
{
return $this->hasMany('App\Player');
}
public function group()
{
return $this->belongsToMany('App\Group');
}
public function change()
{
return $this->belongsToMany('App\Change');
}
public function isAdmin()
{
foreach($this->group as $group)
{
if ($group->id == 1)
{
return true;
}
}
return false;
}
public function isMod()
{
foreach($this->group as $group)
{
if ($group->id == 2)
{
return true;
}
}
return false;
}
public function isGm()
{
foreach($this->group as $group)
{
if ($group->id == 3)
{
return true;
}
}
return false;
}
public function isBanned()
{
foreach($this->group as $group)
{
if($group->id == 4)
{
return true;
}
}
return false;
}
public function hasFriend($id)
{
foreach($this->friend as $friend)
{
if(\Auth::id() == $friend->user_id && $friend->friend_id == $id)
{
return true;
}
}
}
/*With group user*/
public function linkName($id, $name)
{
foreach($this->group as $group)
{
if($group->id == 1)
{
$nick = "<td class='info'><a style='color:".$group->color_name."; font-weight:bold' href=".'/profile/'.$id.">".$name."</a></td><td class='info'>".$group->name."</td>";
return $nick;
}
if($group->id == 2)
{
$nick = "<td class='info'><a style='color:".$group->color_name."; font-weight:bold' href=".'/profile/'.$id.">".$name."</a></td><td class='info'>".$group->name."</td>";
return $nick;
}
if($group->id == 3)
{
$nick = "<td class='info'><a style='color:#eac91d; font-weight:bold' href=".'/profile/'.$id.">".$name."</a></td><td class='info'>".$group->name."</td>";
return $nick;
}
if($group->id == 4)
{
$nick = "<td class='info'><a style='color:".$group->color_name."; font-weight:bold' href=".'/profile/'.$id."><s>".$name."</s></a></td><td class='info'>".$group->name."</td>";
return $nick;
}
if($group->id == 5)
{
$nick = "<td class='info'><a style='color:".$group->color_name."; font-weight:bold' href=".'/profile/'.$id.">".$name."</a></td><td class='info'>".$group->name."</td>";
return $nick;
}
}
}
/*Without group user*/
public function formatLinkName($id, $name)
{
foreach($this->group as $group)
{
if($group->id == 1)
{
$nick = "<a style='color:".$group->color_name."; font-weight:bold' href=".'/profile/'.$id.">".$name."</a>";
return $nick;
}
if($group->id == 2)
{
$nick = "<a style='color:".$group->color_name."; font-weight:bold' href=".'/profile/'.$id.">".$name."</a>";
return $nick;
}
if($group->id == 3)
{
$nick = "<a style='color:#eac91d; font-weight:bold' href=".'/profile/'.$id.">".$name."</a>";
return $nick;
}
if($group->id == 4)
{
$nick = "<a style='color:".$group->color_name."; font-weight:bold' href=".'/profile/'.$id."><s>".$name."</s></a>";
return $nick;
}
if($group->id == 5)
{
$nick = "<a style='color:".$group->color_name."; font-weight:bold' href=".'/profile/'.$id.">".$name."</a>";
return $nick;
}
}
}
public function showName($name)
{
foreach($this->group as $group)
{
if($group->id == 1)
{
$nick = "<strong><font color=".$group->color_name.">".$name."</font></strong>";
return $nick;
}
if($group->id == 2)
{
$nick = "<strong><font color=".$group->color_name.">".$name."</font></strong>";
return $nick;
}
if($group->id == 3)
{
$nick = "<font color='#eac91d'>".$name."</font></strong>";
return $nick;
}
if($group->id == 4)
{
$nick = "<font color=".$group->color_name."><s>".$name."</s></font></strong>";
return $nick;
}
if($group->id == 5)
{
$nick = "<font color=".$group->color_name."><b>".$name."</b></font></strong>";
return $nick;
}
}
}
public function showACP()
{
foreach($this->group as $group)
{
if($group->id == 1)
{
$show = '<li><a href="/admin">ACP</a></li>';
return $show;
}
}
}
public function info()
{
return $this->belongsToMany('App\Info');
}
public function formatSex($sex)
{
if($this->sex == 0)
{
$showsex = 'Mężczyzna';
return $showsex;
}
else
{
$showsex = 'Kobieta';
return $showsex;
}
}
public function note()
{
return $this->belongsToMany('App\Note');
}
public function contact()
{
return $this->belongsToMany('App\Contact');
}
public function checkGroup($id)
{
foreach($this->group as $group)
{
if ($group->id == 1)
{
return true;
}
}
return false;
}
public function comment()
{
return $this->hasMany('App\Comment');
}
public function queryComment($query, $id)
{
return $query->where('to_user', $id);
}
public function queryCommit($query, $id)
{
return $query->where('to_comment', $id);
}
public function message()
{
return $this->hasMany('App\Message');
}
public function article()
{
return $this->hasMany('App\Article');
}
public function friend()
{
return $this->belongsToMany('App\Friend');
}
public function queryFriend($query, $id)
{
return $query->where('friend_id', $id);
}
}
Модель друга:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Friend extends Model
{
protected $fillable = ['friend_id', 'friend_name', 'added_at', 'friend_avatar'];
public function user()
{
return $this->belongsToMany('App\User')->withTimestamps();
}
}
ДОБАВЛЕНИЕ ОТВЕТА:
Я сделал это так:
@if(!\Auth::guest() && $user->id != \Auth::id() && !\Auth::user()->hasFriend($user->id))
BUTTON
@endif
Задача ещё не решена.
Других решений пока нет …