我想从数据库中获取一个表,我从雄辩的laravel中获得了这些文件,但是我不知道为什么我收到$ user undefined变量。我非常需要您的帮助,因为我不知道该怎么做才能完成我的项目。我需要role_users表,user_id和role_id列。
编辑:这是我的表,名为“事件” https://imgur.com/a/gcAzv4F
“ if”功能应该像这样...
@if(...role_id == 1)
<i class="icon-user"></i>
@else
<i class="icon-trophy"></i>
Role.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
// protected $fillable = [
// 'name', 'display_name', 'description',
// ];
protected $table = "roles";
public function user()
{
return $this->belongsToMany('App\User','role_users','role_id','user_id');
}
}
我的观点
<ul class="list-inline">
<li style="font-style: normal !important">
@if( in_array("Organizations", $event->user->roles->pluck('role') )
<i class="fa fa-building-o"></i>
@else
<i class="icon-user"></i>
@endif
<a style="margin-left: 2px" href="{{ url('') }}/{{ $event->user->username }}">{{ $event->user->username }}</a></li>
<li style="font-style: normal !important"><i style="margin-right: 3px" class="icon-hotel-restaurant-183 u-line-icon-pro fa-"></i> <a href="{{ url('view-all-event') }}">Event</li>
<li style="font-style: normal !important"><i style="margin-right: 3px" class="icon-notebook fa-"></i> <a href="{{ url('topic') }}/{{ $event->category->category_url }}">{{ $event->category->category }}</a></li>
</ul>
@endforeach
In this case I receive this error:Undefined variable: user (View: /var/www/clients/client1/web2/web/resources/views/event/view_all_event.blade.php)
Event.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
protected $table = 'events';
public $timestamps = false;
protected $fillable = [
'user_id', 'subject','information','event_type_id','country','public','category_id','date','added','views','address','city','starts','ends','organizer','telephone','website','email','image_path'
];
public function category()
{
return $this->belongsTo('App\Category','category_id','id');
}
public function user()
{
// return $this->hasOne('App\Country','state_country_id','id');
return $this->belongsTo('App\User','user_id','id');
}
public function role()
{
return $this->belongsToMany('App\Role','role_users','user_id','role_id');
}
public function event_like()
{
return $this->hasMany('App\EventLike');
}
public function event_comment()
{
return $this->hasMany('App\EventComment');
}
public function event_type()
{
// return $this->hasOne('App\Country','state_country_id','id');
return $this->belongsTo('App\EventType','event_type_id','id');
}
public function country()
{
// return $this->hasOne('App\Country','state_country_id','id');
return $this->belongsTo('App\Country','country','id');
}
public function user_participant()
{
return $this->belongsToMany('App\User','events_participants','event_id','user_id');
}
public function participant()
{
return $this->belongsToMany('App\ParticipantType','events_participants','event_id','participant_type');
}
}
User.php
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
class User extends Authenticatable
{
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array
*/
// protected $fillable = [
// 'name', 'email', 'password',
// ];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
// protected $hidden = [
// 'password', 'remember_token',
// ];
public function comment()
{
return $this->hasMany('App\Comment');
}
public function country()
{
// return $this->hasOne('App\Country','state_country_id','id');
return $this->belongsTo('App\Country','country_id','id');
}
public function organization_type()
{
// return $this->hasOne('App\Country','state_country_id','id');
return $this->belongsTo('App\OrganizationType');
}
public function industry()
{
// return $this->hasOne('App\Country','state_country_id','id');
return $this->belongsTo('App\Industry');
}
public function career_path()
{
return $this->hasMany('App\CareerPath');
}
public function education()
{
return $this->hasMany('App\Education');
}
public function about()
{
return $this->hasOne('App\About');
}
public function portfolio()
{
return $this->hasOne('App\Portfolio');
}
public function language_skills_selected()
{
return $this->belongsToMany('App\LanguageSkill','language_skills_selected','user_id','language_skills');
}
public function offices_branch()
{
return $this->hasMany('App\OfficesBranch');
}
public function my_alert()
{
return $this->hasOne('App\MyAlert');
}
public function privancy_setting()
{
return $this->hasOne('App\PrivancySetting');
}
public function event()
{
return $this->hasMany('App\Event');
}
public function news()
{
return $this->hasMany('App\News');
}
public function opinion()
{
return $this->hasMany('App\Opinion');
}
public function career_solution()
{
return $this->hasMany('App\CareerSolution');
}
public function contact()
{
return $this->belongsToMany('App\User','contacts','contact_id','user_id');
}
public function user()
{
return $this->belongsToMany('App\User','contacts','user_id','contact_id');
}
public function invitation()
{
return $this->belongsToMany('App\User','invitations','inviter_id','target_id')->withPivot('accepted');
}
public function target()
{
return $this->belongsToMany('App\User','invitations','target_id','inviter_id','accepted')->withPivot('accepted');
}
public function mailbox()
{
return $this->belongsToMany('App\User','mailbox','sender_id','target_id')->withPivot('read','subject','message','date','id');
}
public function inbox()
{
return $this->belongsToMany('App\User','mailbox','target_id','sender_id')->withPivot('read','subject','message','date','id');
}
public function role()
{
return $this->belongsToMany('App\Role','role_users','user_id','role_id');
}
public function evnet_like()
{
return $this->hasMany('App\EventLike');
}
public function evnet_comment()
{
return $this->hasMany('App\EventComment');
}
public function news_like()
{
return $this->hasMany('App\NewsLike');
}
public function news_comment()
{
return $this->hasMany('App\NewsComment');
}
public function opinion_like()
{
return $this->hasMany('App\OpinionLike');
}
public function opinion_comment()
{
return $this->hasMany('App\OpinionComment');
}
public function event_participant()
{
return $this->belongsToMany('App\Event','events_participants','user_id','event_id');
}
public function participant()
{
return $this->belongsToMany('App\ParticipantType','events_participants','user_id','participant_type');
}
public function feed()
{
return $this->hasMany('App\Feed');
}
public function rss_article()
{
return $this->hasMany('App\RssArticle');
}
public static function customPaginate($currentPage,$items,$perPage)
{
//Get current page form url e.g. &page=6
// $currentPage = LengthAwarePaginator::resolveCurrentPage();
//Create a new Laravel collection from the array data
$collection = new Collection($items);
//Define how many items we want to be visible in each page
$perPage = $perPage;
//Slice the collection to get the items to display in current page
$currentPageSearchResults = $collection->slice(($currentPage * $perPage)- $perPage, $perPage)->all();
//Create our paginator and pass it to the view
$paginatedSearchResults = new LengthAwarePaginator($currentPageSearchResults, count($collection), $perPage);
return $paginatedSearchResults;
}
}
我猜Event.php用于我的页面(view-all-event)。
答案 0 :(得分:2)
使用
<li style="font-style: normal !important">
@if( in_array("Organizations", $event->user->roles->pluck('role') )
<i class="fa fa-building-o"></i>
@else
<i class="icon-user"></i>
@endif
</li>
要检查用户是否具有Organizations
角色,如果您已创建roles
关系来关联用户和角色。