我想从 career_solutions
表中为每个用户获取角色(role_users
表)。我正在尝试为用户添加一些图标,但我遇到了一个大问题.我刚刚为过滤器 events
、opinion
和 news
制作了图标,但对于 Career solution
不起作用。
对于 events
、opinion
和 news
我使用了:
$temp['role'] = $events->user->role;
$temp['role'] = $opinion->user->role;
$temp['role'] = $news->user->role;
现在我正在尝试为 $career_solution
获取相同的值,但是 $career_solution->user->role
似乎不再有效...
这是我的 Career Solution.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
class CareerSolution extends Model
{
protected $table = 'career_solutions';
public $timestamps = false;
protected $fillable = [
'user_id', 'subject','topic_category_id','topic_subcategory_id','quantity','expires_at','public','location','date','added','views','on_offer','optional','city','employment_type','estimated_salary','url','type','job_id','company','modified_date','city','indeedApply'
];
public function user()
{
return $this->belongsTo('App\User','user_id','id');
}
public function country()
{
return $this->belongsTo('App\Country','location','id');
}
public function category()
{
return $this->belongsTo('App\Category','topic_category_id','id');
}
public function sub_category()
{
return $this->belongsTo('App\CareerSolutionCategory','topic_subcategory_id','id');
}
}
这是我的 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');
}
}
我的 Controller
if($filter == 'all' || $filter == 'events')
{
$events = \App\Event::with('user','category')->whereHas('user', function($query) {
$query->where('deleted_at', '=', null);
})->whereIn('category_id',$categoryID)->where(function($query1) use($id) {
$query1->where('public','=', 1)->orWhereHas('user.contact', function ($query2) use ($id) {
$query2->where('user_id', '=', $id);
})->orWhere('user_id','=',$id);
});
// $events = $events->;
$events_data = $events->orderBy('date', 'desc')->get();
foreach ($events_data as $event)
{
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $event->information, $image);
if(isset($image['src']))
{
$type_picture = $image['src'];
}else{
$type_picture = "";
}
$temp = array();
$temp['type'] = 'Events';
$temp['typee'] = 'hotel-restaurant-183 u-line-icon-pro fa-';
$temp['subject'] = $event->subject;
$temp['all_url'] = 'view-all-event';
$temp['type_url'] = 'view-event';
$temp['id'] = $event->id;
$temp['information'] = $event->information;
$temp['category'] = $event->category->category;
$temp['category_url'] = $event->category->category_url;
$temp['color'] = $event->category->color;
$temp['date'] = $event->date;
$temp['public'] = $event->public;
$temp['username'] = $event->user->username;
$temp['role'] = $event->user->role;
$temp['profile_picture'] = $event->user->profile_picture;
$temp['type_picture'] = $type_picture;
$news_events_opinions[] = $temp;
}
}
foreach ($career_solutions_data as $career_solution)
{
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $career_solution->optional, $image);
if(isset($image['src']))
{
$type_picture = $image['src'];
}
else{
$type_picture = "";
}
$temp_soluation = array();
$temp_soluation['type'] = 'Career Solution';
$temp_soluation['typee'] = 'briefcase';
$temp_soluation['subject'] = $career_solution->subject;
$temp_soluation['information'] = $career_solution->optional;
$temp_soluation['category'] = $career_solution->category;
$temp_soluation['category_url'] = $career_solution->category_url;
$temp_soluation['color'] = $career_solution->color;
$temp_soluation['all_url'] = 'search-career-solutions';
$temp_soluation['type_url'] = 'view-career-solutions';
$temp_soluation['id'] = $career_solution->id;
$temp_soluation['date'] = $career_solution->date;
$temp_soluation['public'] = $career_solution->public;
$temp_soluation['sub_category'] = $career_solution->sub_category;
$temp_soluation['on_offer'] = $career_solution->on_offer;
$temp_soluation['username'] = $career_solution->username;
$temp_soluation['roleMe'] = $career_solution->optional;
$temp_soluation['role'] = $user->user_id;
$temp_soluation['profile_picture'] = $career_solution->profile_picture;
$temp_soluation['type_picture'] = $type_picture;
// $news_events_opinions[] = $temp_soluation;
$my_career_solution[] = $temp_soluation;
}
}
最佳答案
你能尝试在你的 Controller 中添加这个吗?
$temp_soluation['role'] = \App\User::select('id')->where('id', '=', $career_solution->user_id)->first()->role;
代替
$temp_soluation['role'] = $user->user_id;
在你看来使用这个:
@if($carer_solution_data['role'][0]['pivot']['role_id'] == 1 )
// code
@else
// code
@endif
关于php - 如何从职业解决方案表中的用户那里获取角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57081955/