noblemfd
2 weeks ago
127
4
Laravel

DateTime::__construct() expects parameter 1 to be string, array given

Posted 2 weeks ago by noblemfd

I am working on dynamic form input. I have two(2) models:

 class Goal extends Model
 {
   protected $table = 'goals';
   protected $fillable = [
          'goal_type_id',
          'employee_id',
          'weighted_score',
          'employee_comment',
          'goal_title'
      ];

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

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

  public function goaldetail(){
    return $this->hasMany('App\Models\GoalDetail');
  }
}


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

  public function goal()
  {
    return $this->belongsTo('App\Models\Goal');
  }
}

From the model, start_date and end_date are date datatype.

I also have Request Rules:

class StoreGoalRequest extends FormRequest
{    
  public function rules()
  {
    return [
    'goal_type_id' => [
        'required',              
        Rule::unique('goals', 'goal_type_id', 'employee_id')
    ],  
    'goal_title' => [
        'required', 
        'string',
        'min:5',
        'max:100',                
        Rule::unique('goals', 'goal_title', 'employee_id')
    ],  
    'weighted_score'            => 'required|numeric|min:0|max:500',           

    'kpi_description'           => 'required|array',
    'kpi_description.*'         => 'required',

    'activity'           => 'required|array',
    'activity.*'         => 'required',               

    'start_date.*' => [
        'required', 
        'date',
        'date_format:Y-m-d',
        'array'
    ], 
    'start_date.*' => [
        'required', 
        'date',
        'date_format:Y-m-d',
    ],
    'end_date.*' => [
        'required', 
        'date',
        'date_format:Y-m-d',
        'after:start_date.*',
        'array'
    ],           
    'end_date.*' => [
        'required', 
        'date',
        'date_format:Y-m-d',
        'after:start_date.*'
    ],        

    ];
  }
}

Goal is the main Controller and everything for GoalDetail is done from there.

 public function create()
 {
     $categories = GoalType::with('children')->whereNull('parent_id')->get();
        ->with('categories', $categories);
 }

public function store(StoreGoalRequest $request)
{
  $startDate = Carbon::parse($request->start_date);
  $endDate = Carbon::parse($request->end_date);      
  $employeeId = Auth::user()->employee_id;
  try {
    $goal = new Goal();
    $goal->goal_type_id             = $request->goal_type_id;
    $goal->employee_id              = $employeeId;
    $goal->weighted_score           = $request->weighted_score;
    $goal->goal_title               = $request->goal_title;
    $goal->goal_description         = $request->goal_description;
    $goal->save();

    foreach ( $request->activity as $key => $activity){
        $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[$key];
        $goaldetail->end_date                   = $endDate[$key];                
        $goaldetail->goal_id          = $goal->id;
        $goaldetail->save();
     }
        Session::flash('success', 'Goal is created successfully');
        return redirect()->route('goals.index');
    } catch (Exception $exception) {
        Session::flash('danger', 'Goal creation failed!');
        return redirect()->route('goals.index');
  }
}

create.blade

   <form  action="{{route('goals.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
       {{csrf_field()}}

       <div class="card-body">
        <div class="form-body">
        <div class="row">  
          <div class="col-12 col-sm-6">
            <div class="form-group">
              <label class="control-label"> Goal Title:<span style="color:red;">*</span></label>
              <input  type="text" name="goal_title" placeholder="Enter goal title here" class="form-control">
            </div>
          </div>

     <div class="col-sm-12">
            <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th scope="col">Activity<span style="color:red;">*</span></th>
                            <th scope="col">KPI<span style="color:red;">*</span></th>
                            <th scope="col">Start Date<span style="color:red;">*</span></th>
                            <th scope="col">End Date<span style="color:red;">*</span></th>
                            <th scope="col"><a class="btn btn-info addRow"><i class="fa fa-plus"></i></a></th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td><input type="text" name="activity[]" class="form-control activity" ></td>
                            <td><input type="text" name="kpi_description[]" class="form-control kpi_description" ></td>
                            <td><input type="date" class="form-control start_date" placeholder="dd/mm/yyyy" name="start_date[]"  min="{{Carbon\Carbon::now()->firstOfYear()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}"></td>
                            <td><input type="date" class="form-control end_date" placeholder="dd/mm/yyyy" name="end_date[]"  min="{{Carbon\Carbon::now()->firstOfYear()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}"></td>
                            <td><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>
                         </tr>
                        </tbody>


            </table>
       </div>
          <div class="col-12 col-sm-6">
            <div class="form-group">
              <label class="control-label"> Weight:</label>
              <input  type="number" name="weighted_score" placeholder="Enter weighted score here" class="form-control">
            </div>
          </div>  

       </div>
     </div>
    </div>          
    <!-- /.card-body -->
    <div class="card-footer">
      <button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
      <button type="button" onclick="window.location.href='{{route('goals.index')}}'" class="btn btn-default">Cancel</button>
    </div>           
       
    </form>

I am using Laravel-5.8 for dynamic form input. I have two(2) models:

class Goal extends Model
{
  protected $table = 'goals';
  protected $fillable = [
          'goal_type_id',
          'employee_id',
          'weighted_score',
          'employee_comment',
          'goal_title'
      ];

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

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

  public function goaldetail(){
    return $this->hasMany('App\Models\GoalDetail');
  }
}

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

  public function goal()
  {
    return $this->belongsTo('App\Models\Goal');
  }
}

From the model, start_date and end_date are date datatype.

I also have Request Rules:

class StoreGoalRequest extends FormRequest
{    
  public function rules()
  {
    return [
    'goal_type_id' => [
        'required',              
        Rule::unique('goals', 'goal_type_id', 'employee_id')
    ],  
    'goal_title' => [
        'required', 
        'string',
        'min:5',
        'max:100',                
        Rule::unique('goals', 'goal_title', 'employee_id')
    ],  
    'weighted_score'            => 'required|numeric|min:0|max:500',           

    'kpi_description'           => 'required|array',
    'kpi_description.*'         => 'required',

    'activity'           => 'required|array',
    'activity.*'         => 'required',               

    'start_date.*' => [
        'required', 
        'date',
        'date_format:Y-m-d',
        'array'
    ], 
    'start_date.*' => [
        'required', 
        'date',
        'date_format:Y-m-d',
    ],
    'end_date.*' => [
        'required', 
        'date',
        'date_format:Y-m-d',
        'after:start_date.*',
        'array'
    ],           
    'end_date.*' => [
        'required', 
        'date',
        'date_format:Y-m-d',
        'after:start_date.*'
    ],        

    ];
  }
}

Goal is the main Controller and everything for GoalDetail is done from there.

 public function create()
 {
   $categories = GoalType::with('children')->whereNull('parent_id')->get();
        ->with('categories', $categories);
 }

public function store(StoreGoalRequest $request)
{
  $startDate = Carbon::parse($request->start_date);
  $endDate = Carbon::parse($request->end_date);      
  $employeeId = Auth::user()->employee_id;
    try {
      $goal = new Goal();
      $goal->goal_type_id             = $request->goal_type_id;
      $goal->employee_id              = $employeeId;
      $goal->weighted_score           = $request->weighted_score;
      $goal->goal_title               = $request->goal_title;
      $goal->goal_description         = $request->goal_description;
      $goal->save();

    foreach ( $request->activity as $key => $activity){
        $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[$key];
        $goaldetail->end_date                   = $endDate[$key];                
        $goaldetail->goal_id          = $goal->id;
        $goaldetail->save();
     }
        Session::flash('success', 'Goal is created successfully');
        return redirect()->route('goals.index');
    } catch (Exception $exception) {
        Session::flash('danger', 'Goal creation failed!');
        return redirect()->route('goals.index');
 }
}

create.blade

   <form  action="{{route('goals.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
       {{csrf_field()}}

       <div class="card-body">
        <div class="form-body">
        <div class="row">  
          <div class="col-12 col-sm-6">
            <div class="form-group">
              <label class="control-label"> Goal Title:<span style="color:red;">*</span></label>
              <input  type="text" name="goal_title" placeholder="Enter goal title here" class="form-control">
            </div>
          </div>

     <div class="col-sm-12">
            <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th scope="col">Activity<span style="color:red;">*</span></th>
                            <th scope="col">KPI<span style="color:red;">*</span></th>
                            <th scope="col">Start Date<span style="color:red;">*</span></th>
                            <th scope="col">End Date<span style="color:red;">*</span></th>
                            <th scope="col"><a class="btn btn-info addRow"><i class="fa fa-plus"></i></a></th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td><input type="text" name="activity[]" class="form-control activity" ></td>
                            <td><input type="text" name="kpi_description[]" class="form-control kpi_description" ></td>
                            <td><input type="date" class="form-control start_date" placeholder="dd/mm/yyyy" name="start_date[]"  min="{{Carbon\Carbon::now()->firstOfYear()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}"></td>
                            <td><input type="date" class="form-control end_date" placeholder="dd/mm/yyyy" name="end_date[]"  min="{{Carbon\Carbon::now()->firstOfYear()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}"></td>
                            <td><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>
                         </tr>
                        </tbody>


            </table>
       </div>
          <div class="col-12 col-sm-6">
            <div class="form-group">
              <label class="control-label"> Weight:</label>
              <input  type="number" name="weighted_score" placeholder="Enter weighted score here" class="form-control">
            </div>
          </div>  

       </div>
     </div>
    </div>          
    <!-- /.card-body -->
    <div class="card-footer">
      <button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
      <button type="button" onclick="window.location.href='{{route('goals.index')}}'" class="btn btn-default">Cancel</button>
    </div>           
       
    </form>

Javascript

<script type="text/javascript">
    $(document).ready(function(){
        $('.addRow').on('click', function () {
   var isHod = {{ Auth::user()->is_hod == 0 ? 0 : 1 }};
    var numRows = $('.activity').length

    if (isHod || (!isHod && numRows<3)) {
        addRow();
    }
        });

        function addRow() {
            var addRow = '<tr>\n' +
'                                <td><input type="text" name="activity[]" class="form-control activity" ></td>\n' +
'                                <td><input type="text" name="kpi_description[]" class="form-control kpi_description" ></td>\n' +
'                                <td><input type="date" class="form-control start_date" placeholder="dd/mm/yyyy" name="start_date[]"  min="{{Carbon\Carbon::now()->firstOfYear()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}"></td>\n' +
'                                <td><input type="date" class="form-control end_date" placeholder="dd/mm/yyyy" name="end_date[]"  min="{{Carbon\Carbon::now()->firstOfYear()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}"></td>\n' +
'                                <td><a   class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>\n' +
'                             </tr>';
            $('tbody').append(addRow);
            addRemoveListener();
        };
     addRemoveListener();
  });

</script>

When I clicked on submit button, I expected the application to save data into goals and goal_details table. But I got the error shown below:

DateTime::__construct() expects parameter 1 to be string, array given

Please sign in or create an account to participate in this conversation.