Laravel - Обновление пакета Dynami c Ввод, добавление комментария и статуса - PullRequest
0 голосов
/ 17 февраля 2020

Я использовал Laravel -5,8 для веб-приложения. В приложении я использовал динамический ввод c.

У меня есть эти таблицы:

class AppraisalGoal extends Model
{
    protected $table = 'appraisal_goals';
    protected $primaryKey = 'id';

    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',
                  'appraisal_doc',
                  'appraisal_start_date',
                  'appraisal_end_date',
              ];

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

    public function employee()
    {
        return $this->belongsTo('App\Models\Hr\HrEmployee','employee_id');
    }

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

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

class AppraisalGoalDetail extends Model
{
    protected $table = 'appraisal_goal_details';
    protected $fillable = [
                  'name',
                  'company_id',
                  'appraisal_goal_id',
                  'kpi_description',
                  'appraisal_doc',
                  'activity',
                  'start_date',
                  'end_date',
              ];

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

Я успешно сохранил его в базе данных, используя этот контроллер:

AppisalGoalController

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

    $identities = DB::table('appraisal_identity')->select('id','appraisal_name')->where('company_id', $userCompany)->where('is_current', 1)->first();

    $goaltypes   =       AppraisalGoalType::where('company_id', $userCompany)->get(); 
     $categories = AppraisalGoalType::with('children')->where('company_id', $userCompany)->whereNull('parent_id')->get();

    return view('appraisal.appraisal_goals.create')
            ->with('goaltypes', $goaltypes)
            ->with('categories', $categories)
            ->with('identities', $identities);
}

public function store(StoreAppraisalGoalRequest $request)
{
    $validated = $request->validated();     
    $appraisalStartDate = Carbon::parse($request->appraisal_start_date);
    $appraisalEndDate = Carbon::parse($request->appraisal_end_date);        
    $userCompany = Auth::user()->company_id;
    $employeeId = Auth::user()->employee_id;
      $identities = DB::table('appraisal_identity')->select('id','appraisal_name')->where('company_id', $userCompany)->where('is_current', 1)->first();
    try {
        $goal = new AppraisalGoal();
        $goal->goal_type_id             = $request->goal_type_id;
        $goal->appraisal_identity_id    = $request->appraisal_identity_id;
        $goal->employee_id              = $employeeId;
        $goal->weighted_score           = $request->weighted_score;
        $goal->goal_title               = $request->goal_title;
        $goal->goal_description         = $request->goal_description;

             if ($request->appraisal_doc != "") {
                 $appraisal_doc = $request->file('appraisal_doc');
                 $new_name = rand() . '.' . $appraisal_doc->getClientOriginalExtension();
                 $appraisal_doc->move(public_path('storage/documents/appraisal_goal'), $new_name);
                 $goal->appraisal_doc = $new_name;
            }            

        foreach ( $request->activity as $key => $activity){
            $startDate = Carbon::parse($request->start_date[$key]);
            $endDate = Carbon::parse($request->end_date[$key]);

            $goaldetail = new AppraisalGoalDetail();

            $goaldetail->kpi_description            = $request->kpi_description[$key];
            $goaldetail->appraisal_doc              = $request->application_doc[$key];
            $goaldetail->activity                   = $request->activity[$key];  
            $goaldetail->start_date                 = $startDate ->toDateTimeString();
            $goaldetail->end_date                   = $endDate->toDateTimeString();                  
            $goaldetail->appraisal_goal_id          = $goal->id;
            $goaldetail->save();
         }
        $min_date = AppraisalGoalDetail::select('start_date')->where('appraisal_goal_id', $goal->id)->min('start_date');
        $max_date = AppraisalGoalDetail::select('end_date')->where('appraisal_goal_id', $goal->id)->max('end_date');             

         $goal->update([
             'appraisal_start_date' => $min_date,
             'appraisal_end_date'   => $max_date
                 ]);
            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 goal

Это успешно сохранено в базе данных.

Теперь у меня проблема в том, что я хочу обновить сохраненные записи, просто добавив комментарий и статус, как показано на диаграмме ниже:

appraisal update

Когда Я нажимаю на кнопку отправки, она должна обновить запись с комментарием и одобрить / отклонить

Это мой indexController

public function index()
{
    $userEmployee = Auth::user()->employee_id;
    $goals = AppraisalGoal::where('employee_id', $userEmployee)->get();
    return view('appraisal.appraisal_goals.index')->with('goals', $goals);
}

Как мне написать контроллер и просмотреть для достижения этой цели?

Спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...