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

Laravel - How to display foreign key value as the title of a view blade

我正在使用 Laravel-5.8 作为门户网站。我有这些型号 类:

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

appraisal_identity_id 是来自 appraisal_identities 的外键。

我想:

  1. 从appraisal_identity(AppraisalIdenty)中获取appraisal_name,其中is_currrent为1,并将其显示为视图blade中的标题:

    当前评估:{{ $identities }}

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

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

htmlspecialchars() expects parameter 1 to be string, object given (View: C:\xampp\htdocs\project\resources\views\appraisal_goals\create.blade.php)

但是当我申请的时候:

die(var_dump($identities));

在控制器中,我得到:

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

如何解决这两个问题?

谢谢。

我认为你显示的对象不是视图中的值 所以在视图中应该是 {{$identities->appraisal_name}}

从 AppraisalGoal 获取与 AppraisalIdentity 相关的 appraisal_identity_id 的值,其中 is_current 为 1,并将其保存在 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();