Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel-如何将外键值显示为视图的标题_Laravel_Laravel 5.8 - Fatal编程技术网

Laravel-如何将外键值显示为视图的标题

Laravel-如何将外键值显示为视图的标题,laravel,laravel-5.8,Laravel,Laravel 5.8,我正在使用Laravel-5.8作为Web门户。我有以下模型课程: class AppraisalIdentity extends Model { protected $table = 'appraisal_identity'; protected $fillable = [ 'appraisal_name', 'is_current', ]; } class AppraisalGoal extends Model { prote

我正在使用Laravel-5.8作为Web门户。我有以下模型课程:

class AppraisalIdentity extends Model
{
  protected $table = 'appraisal_identity';
  protected $fillable = [
          'appraisal_name',
          'is_current',
      ];
}

class AppraisalGoal extends Model
{
  protected $table = 'appraisal_goals';
  protected $fillable = [
          'goal_type_id',
          'appraisal_identity_id',
          'employee_id',
          'company_id',
          'is_published',
          'is_approved',
          'weighted_score',
          'employee_comment',
          'line_manager_comment',
          'goal_title',
          'start_date',
          'end_date',
          'is_active'
      ];

   public function goaltype()
   {
      return $this->belongsTo('App\Models\Appraisal\AppraisalGoalType');
   }

   public function appraisalidentity()
   {
      return $this->belongsTo('App\Models\Appraisal\AppraisalIdentity','appraisal_identity_id');
   }

   public function appraisalgoaldetail(){
     return $this->hasMany('App\Models\Appraisal\AppraisalGoalDetail');
   }   
}
该控制器:

class AppraisalGoalsController extends Controller
{
  public function create()
  {
   $userCompany = Auth::user()->company_id;

   $goaltypes   =       AppraisalGoalType::where('company_id', $userCompany)->get(); 
   $categories = AppraisalGoalType::with('children')->where('company_id', $userCompany)->whereNull('parent_id')->get();
   $identities = DB::table('appraisal_identity')->select('appraisal_name')->where('company_id', $userCompany)->where('is_current', 1)->first();

    return view('appraisal.appraisal_goals.create')
        ->with('goaltypes', $goaltypes)
        ->with('categories', $categories)
        ->with('identities', $identities);
 }
 public function store(StoreAppraisalGoalRequest $request)
 {
  $startDate = Carbon::parse($request->start_date);
  $endDate = Carbon::parse($request->end_date);
   try {
    $goal = new AppraisalGoal();
    $goal->goal_type_id             = $request->goal_type_id;
  //  $goal->appraisal_identity_id    = $request->appraisal_identity_id;
    $goal->employee_id              = $request->employee_id;
    $goal->weighted_score           = $request->weighted_score;
    $goal->goal_description         = $request->goal_description;
    $goal->start_date               = $startDate;
    $goal->end_date                 = $endDate;
    $goal->save();

    foreach ( $request->activity as $key => $activity){
        $goaldetail = new AppraisalGoalDetail();
        $goaldetail->kpi_description            = $request->qty[$key];
        $goaldetail->appraisal_doc              = $request->price[$key];
        $goaldetail->activity                   = $request->activity[$key];
        $goaldetail->appraisal_goal_id          = $goal->id;
        $goaldetail->save();
     }
          Session::flash('success', 'Appraisal Goal is created successfully');
          return redirect()->route('appraisal.appraisal_goals.index');
       } catch (Exception $exception) {
          Session::flash('danger', 'Appraisal Goal creation failed!');
          return redirect()->route('appraisal.appraisal_goals.index');
       }
     }
}
评估标识是评估标识中的外键

我想:

  • 从评估标识(评估标识)中获取评估名称,其中当前值为1,并将其显示为视图中的标题:

    当前评估:{{$identies}

  • 从评估工具中获取与评估身份相关的评估身份的值,其中当前值为1,并将其保存在评估工具中

  • 第一个错误是:

    htmlspecialchars()要求参数1为字符串,对象为给定对象(视图:C:\xampp\htdocs\project\resources\views\evaluation\u goals\create.blade.php)

    但当我申请时:

    模具(var_dump($identies))

    在控制器中,我得到:

    对象(stdClass)#2266(1){[“评估名称”]=>string(12)“评估2018”}

    我如何解决这两个问题


    谢谢。

    我认为你展示了obj的价值观 所以在我看来应该是 {{$identies->evaluation_name}

    从evaluational中获取与evaluationidentity相关的evaluation_identity_id的值,其中当前值为1,并将其保存在evaluational中


    嗨,你能添加你的数据库模式吗?
    $appraisal_identity_id = AppraisalIdentity::where('is_current',1)->value('id');
    
    $goal = new AppraisalGoal();
    $goal->goal_type_id             = $request->goal_type_id;
    // $goal->appraisal_identity_id    = $request->appraisal_identity_id;
    $goal->appraisal_identity_id    = $appraisal_identity_id;
    $goal->employee_id              = $request->employee_id;
    $goal->weighted_score           = $request->weighted_score;
    $goal->goal_description         = $request->goal_description;
    $goal->start_date               = $startDate;
    $goal->end_date                 = $endDate;
    $goal->save();