如何在职业解决方案表中从用户那里获得角色? [英] How to get roles from users in career solution table?

查看:77
本文介绍了如何在职业解决方案表中从用户那里获得角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 career_solutions 表中为每个用户获取角色( role_users 表)。为用户添加一些图标,但是我遇到了一个大问题。我只是为过滤器事件观点新闻,但对于职业解决方案不起作用。

I want to get roles(role_users table) for every user from career_solutions table.I'm trying to add some icons for users, but I have a big problem.I just made icons for filter events,opinion and news, but for Career solution doesn't works.

对于事件观点新闻,我使用了

$temp['role'] = $events->user->role;
$temp['role'] = $opinion->user->role;
$temp['role'] = $news->user->role;

现在我想为 $ career_solution ,但 $ career_solution-> user->角色似乎不再起作用了……

Now I'm trying to get same values for $career_solution but $career_solution->user->role doesn't seems that it works anymore...

这是我的职业解决方案.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');
    }




}

我的控制器

 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;
                }
            }


推荐答案

您可以尝试在控制器中添加它吗?

Can you try to add this in your controller?

$temp_soluation['role'] = \App\User::select('id')->where('id', '=', $career_solution->user_id)->first()->role;

而不是

$temp_soluation['role'] = $user->user_id;

在您看来,使用以下命令:

And in your view use this:

@if($carer_solution_data['role'][0]['pivot']['role_id'] == 1 )
    // code
@else 
    // code
@endif

这篇关于如何在职业解决方案表中从用户那里获得角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆