0

我将 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');
       }
     }
}

评估标识 ID 是评估标识的外键。

我想要:

  1. 从 is_currrent 为 1 的评估标识 (AppraisalIdenty) 中获取评估名称,并将其显示为视图刀片的标题:

    当前评估:{{ $identities }}

  2. 从 AppraisalGoal 中获取与 is_current 为 1 的 AppraisalIdentity 相关的appraisal_identity_id 的值,并将其保存在 AppraisalGoal 中

对于第一个,我得到了这个错误:

htmlspecialchars() 期望参数 1 是字符串,给定对象(视图:C:\xampp\htdocs\project\resources\views\appraisal_goals\create.blade.php)

但是当我申请时:

死(var_dump($identities));

在控制器中,我得到:

object(stdClass)#2266 (1) { ["appraisal_name"]=> string(12) "appraise 2018" }

我该如何解决这两个问题?

谢谢你。

4

2 回答 2

0

我认为您在视图中显示 obj 而不是值,因此在视图中应该是 {{$identities->appraisal_name}}

于 2020-01-29T08:44:54.823 回答
0

从 AppraisalGoal 中获取与 is_current 为 1 的 AppraisalIdentity 相关的appraisal_identity_id 的值,并将其保存在 AppraisalGoal 中

$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();
于 2020-01-29T09:00:20.070 回答