Skip to main content
added 1 character in body; edited title
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Laravel, function Method on Laravel model or controller?to find courses related to a course

I have a Laravel project that has courses. I want to take the related courses from one.

Should I create the method in the model (the php class) or in the controller?

ActuallyCurrently I have this code in the model:

class Course extends Model {

    public function relatedCourses() {

        $all_courses = new Collection;

        // Similar name
        $words = explode($this->name, " ");
        foreach ($words as $word) {
            
            $courses = Course::where(function($query) use ($word) {
                $query->where('id', '<>', $this->id)->where('name_es', 'LIKE', "%$word%")->orWhere('name_cat', 'LIKE', "%$word%");
            })->get();

            $all_courses = $all_courses->merge($courses);
        }

        return $all_courses; 
    }
    
}

Should I move it to the controller?

Laravel, function on model or controller?

I have a Laravel project that has courses. I want to take the related courses from one.

Should I create the method in the model (the php class) or in the controller?

Actually I have this code in the model:

class Course extends Model {

    public function relatedCourses() {

        $all_courses = new Collection;

        // Similar name
        $words = explode($this->name, " ");
        foreach ($words as $word) {
            
            $courses = Course::where(function($query) use ($word) {
                $query->where('id', '<>', $this->id)->where('name_es', 'LIKE', "%$word%")->orWhere('name_cat', 'LIKE', "%$word%");
            })->get();

            $all_courses = $all_courses->merge($courses);
        }

        return $all_courses; 
    }
    
}

Should I move it to the controller?

Method on Laravel model to find courses related to a course

I have a Laravel project that has courses. I want to take the related courses from one.

Should I create the method in the model (the php class) or in the controller?

Currently I have this code in the model:

class Course extends Model {

    public function relatedCourses() {

        $all_courses = new Collection;

        // Similar name
        $words = explode($this->name, " ");
        foreach ($words as $word) {
            
            $courses = Course::where(function($query) use ($word) {
                $query->where('id', '<>', $this->id)->where('name_es', 'LIKE', "%$word%")->orWhere('name_cat', 'LIKE', "%$word%");
            })->get();

            $all_courses = $all_courses->merge($courses);
        }

        return $all_courses; 
    }
    
}

Should I move it to the controller?

fixed blatant pasting error in the code-block
Source Link
Vogel612
  • 25.3k
  • 7
  • 58
  • 141

I have a Laravel project that has courses. I want to take the related courses from one.

Should I create the method in the model (the php class) or in the controller?

Actually I have this code in the model:

class Course extends Model {

    public function relatedCourses() {

        $all_courses = new Collection;

        // Similar name
        $words = explode($this->name, " ");
        foreach ($words as $word) {
            
            $courses = Course::where(function($query) use ($word) {
                $query->where('id', '<>', $this->id)->where('name_es', 'LIKE', "%$word%")->orWhere('name_cat', 'LIKE', "%$word%");
            })->get();

            $all_courses = $all_courses->merge($courses);
        }

        return $all_courses; 
    }
    
}

}

Should I move it to the controller?

I have a Laravel project that has courses. I want to take the related courses from one.

Should I create the method in the model (the php class) or in the controller?

Actually I have this code in the model:

class Course extends Model {

public function relatedCourses() {

    $all_courses = new Collection;

    // Similar name
    $words = explode($this->name, " ");
    foreach ($words as $word) {
        
        $courses = Course::where(function($query) use ($word) {
            $query->where('id', '<>', $this->id)->where('name_es', 'LIKE', "%$word%")->orWhere('name_cat', 'LIKE', "%$word%");
        })->get();

        $all_courses = $all_courses->merge($courses);
    }

    return $all_courses; 
}

}

Should I move it to the controller?

I have a Laravel project that has courses. I want to take the related courses from one.

Should I create the method in the model (the php class) or in the controller?

Actually I have this code in the model:

class Course extends Model {

    public function relatedCourses() {

        $all_courses = new Collection;

        // Similar name
        $words = explode($this->name, " ");
        foreach ($words as $word) {
            
            $courses = Course::where(function($query) use ($word) {
                $query->where('id', '<>', $this->id)->where('name_es', 'LIKE', "%$word%")->orWhere('name_cat', 'LIKE', "%$word%");
            })->get();

            $all_courses = $all_courses->merge($courses);
        }

        return $all_courses; 
    }
    
}

Should I move it to the controller?

deleted 11 characters in body
Source Link
BCdotWEB
  • 11.3k
  • 2
  • 27
  • 45

I have a Laravel project that has courses. I want to take the related courses from one.

Should I create the method in the model (the php class) or in the controller?

Actually I have this code in the model:

class Course extends Model {

public function relatedCourses() {

    $all_courses = new Collection;

    // Similar name
    $words = explode($this->name, " ");
    foreach ($words as $word) {
        
        $courses = Course::where(function($query) use ($word) {
            $query->where('id', '<>', $this->id)->where('name_es', 'LIKE', "%$word%")->orWhere('name_cat', 'LIKE', "%$word%");
        })->get();

        $all_courses = $all_courses->merge($courses);
    }

    return $all_courses; 
}

}

Should I move it to the controller?

Thanks!

I have a Laravel project that has courses. I want to take the related courses from one.

Should I create the method in the model (the php class) or in the controller?

Actually I have this code in the model:

class Course extends Model {

public function relatedCourses() {

    $all_courses = new Collection;

    // Similar name
    $words = explode($this->name, " ");
    foreach ($words as $word) {
        
        $courses = Course::where(function($query) use ($word) {
            $query->where('id', '<>', $this->id)->where('name_es', 'LIKE', "%$word%")->orWhere('name_cat', 'LIKE', "%$word%");
        })->get();

        $all_courses = $all_courses->merge($courses);
    }

    return $all_courses; 
}

}

Should I move it to the controller?

Thanks!

I have a Laravel project that has courses. I want to take the related courses from one.

Should I create the method in the model (the php class) or in the controller?

Actually I have this code in the model:

class Course extends Model {

public function relatedCourses() {

    $all_courses = new Collection;

    // Similar name
    $words = explode($this->name, " ");
    foreach ($words as $word) {
        
        $courses = Course::where(function($query) use ($word) {
            $query->where('id', '<>', $this->id)->where('name_es', 'LIKE', "%$word%")->orWhere('name_cat', 'LIKE', "%$word%");
        })->get();

        $all_courses = $all_courses->merge($courses);
    }

    return $all_courses; 
}

}

Should I move it to the controller?

Source Link
Loading