samedi 23 avril 2016

Laravel:5.0 How to delete elements in array

I am an absolute beginner of Laravel and even php.

I would like to delete elements of array below.

English is not my first language, so if this question does not make sense to you, please leave you comments. Any advice would be appreciated! Thanks in advance!

MyController

public function deleteLog(Course $course, CreateCourseRequest $request){

    $courseWeeks = $course->weeks;
    $courseWeeksCount = intval($courseWeeks);

    $inputWeeks = $request->input('weeks');
    $inputWeeksCount = intval($inputWeeks);

    $inputUsers = $request->input('first_name_list');

    for ($i = $courseWeeksCount; $i >= $inputWeeksCount; $i--) {
        foreach($inputUsers as $user) {
             /* I need to write code that deletes certain numbers of elements of array. 
                For example, when the admin changes the weeks of the course 'intro to php' 
                from 5 to 3, logs whose number of weeks are 4 and 5 will be deleted   */
        }
    }
}

Basically, I would like to do the opposite of addLog function below.

 public function addLog(Course $course, CreateCourseRequest $request){

    $courseWeeks = $course->weeks;
    $courseWeeksCount = intval($courseWeeks);

    $inputWeeks = $request->input('weeks');
    $inputWeeksCount = intval($inputWeeks);

    $inputUsers = $request->input('first_name_list');

    $logs = array();

    for ($i = $courseWeeksCount; $i <= $inputWeeksCount; $i++) {
        foreach($inputUsers as $id) {
            array_push($logs, array(
                'user_id' => $id,
                'course_id' => $course->id,
                'weeks' => $i,
                'work_description' => '',
                'instructor_comments' => '',
                'status' => 'accepted',
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now(),
            ));
        }
    }
    Journal::insert($journals);
}

In MyController, there are functions below.

if($this->AreInputWeeksLess($course, $request)){
        //$this->deleteLog($course, $request);
}

public function AreInputWeeksLess(Course $course, CreateCourseRequest $request){
    $courseWeeks = $course->weeks;
    $courseWeeksCount = intval($courseWeeks);

    $inputWeeks = $request->input('weeks');
    $inputWeeksCount = intval($inputWeeks);

    if($inputWeeksCount - $courseWeeksCount < 0){
        return true;
    } else{
        return false;
    }
}

Course_Edit.blade.php

<div class = "form-group">
    {!! Form::label('weeks', 'Weeks:') !!}
    {!! Form::selectRange('weeks', 1, 17) !!}
</div>

<div class = "form-group">
    {!! Form::label('first_name_list', 'Students:') !!}
    {!! Form::select('first_name_list[]', $students, null, ['id' => 'first_name_list' ,'class' => 'form-control', 'multiple']) !!}
</div>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire