Home > Software design >  I can not change Controller locaiton in Laravel 8
I can not change Controller locaiton in Laravel 8

Time:10-05

I have a Laravel 8 project. I need the change Controllers file locations. But I can't change file locations. For example I moved the HomeController to App\Http\Controllers\Admin instead of App\Http\Controllers. Then I updated the Controllers that I defined with the use command in web.php in the route section. Below I leave both the HomeController and the relevant part of web.php. You can look there. After doing all this, when I refresh my page from localhost, the error in the screenshot appears. I don't understand what am I missing.

enter image description here

web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\{
    AdminController,
    AwsTranscoderController,
    AwsTranscribeController,
    CategoryController,
    ContentController,
    Controller,
    CourseController,
    CreatorApplicationController,
    CreatorController,
    DevController,
    HomeController,
    LearnBiteController,
    NotificationsController,
    NotionApiController,
    PayoutController,
    PromotionalCodeController,
    RecomendationEngineController,
    RequestPayoutController,
    SubscriberController,
    TagController,
    UserTagController,
};

use App\Http\Controllers\Tools\{
    AnalyticsToolController,
    PayoutAlgorithmController,
};

use App\Http\Controllers\DevelopmentTools\{
    EmailTestController,
    GeneralToolsController,
};

HomeController

<?php

namespace App\Http\Controllers;

use App\Course;
use App\CourseLecture;
use App\CreatorApplicationForm;
use App\CreatorProfile;
use App\RawListeningData;
use App\User;
use App\UserPayment;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class HomeController extends Controller 
{

    public $raw_listening;
    public $all_learnbite_id_list;
    public $all_learnbite_lecture_id_list;
    public $all_course_id_list;
    public $all_course_lecture_id_list;
    public $dashboard_data;
    public $creator_applications;
    public $submitted_content;


    public function __construct()
    {
        $this->middleware(['auth']);
    }



    public function index()
    {
        if(Auth::user()->role_id>2){
            $role = Auth::user()->role;
            auth()->logout();
            return redirect('/login');
        }
        $this->getRawListeningData();
        $this->getAllContent();
        $this->getDashboardData();
        $this->getCreatorApplications();
        $this->getSubmittedContent();

        $most_popular_5_course = $this->getMostPopular5Content(false);
        $most_popular_5_learnbite = $this->getMostPopular5Content(true);

        return view('admin.home',[
            'mostpopular_5_learnbite' => $most_popular_5_learnbite,
            'most_popular_5_course'=>$most_popular_5_course,
            'dashboard_data' =>$this->dashboard_data,
            'creator_applications' =>$this->creator_applications,
            'submitted_content' =>$this->submitted_content
            ]
        );
    }

    private function getRawListeningData($period="-1 week"){
        $this->raw_listening = RawListeningData::select('raw_listening_data.id','raw_listening_data.course_lecture_id','course_lectures.course_id','raw_listening_data.user_id','courses.user_id as creator_id')
            ->join('course_lectures', 'raw_listening_data.course_lecture_id', '=', 'course_lectures.id')
            ->join('courses', 'course_lectures.course_id', '=', 'courses.id')
            ->where([['raw_listening_data.created_at','>',date('Y-m-d',strtotime($period))]])
            ->get()
            ->groupBy('course_id')->collect()->sort()->reverse();
    }

    private function getSubmittedContent(){
        $this->submitted_content = Course::where('status_id','=',3)->get();
    }
    private function getCreatorApplications(){
        $this->creator_applications = CreatorApplicationForm::where('creator_id','=',null)->with('categoryCreator')->get();
    }
    private function getDashboardData(){
        $this->dashboard_data = collect([]);
        $courses = Course::select(['id','user_id','isLearnBite'])->where('status_id','=',1)->get();
        $listener = UserPayment::select(['id','user_id','price','period_type'])->where([['purchased_date','>=','2021-12-09']])->get();


        $creator_count = $courses->whereNotIn('user_id',[626,627])->groupBy('user_id')->count();
        $creator_profile = CreatorProfile::whereNotIn('user_id',[626,627])->count();
        $learnbite_count = $courses->whereNotIn('user_id',[626,627])->where('isLearnBite','=',true)->count();
        $course_count = $courses->whereNotIn('user_id',[626,627])->where('isLearnBite','=',false)->count();
        $listener_count = $listener->groupBy('user_id')->count();
        $trial_user_count = $listener->where('period_type','=',1)->groupBy('user_id')->count();
        $paid_user_count = $listener->where('period_type','=',3)->groupBy('user_id')->count();

        // $avarage_seconds = $this->getSessionDurationData();
        // TODO : Bu veri nasıl kullanılacak?

        $this->dashboard_data[] = collect(['title' => 'Creators with Profile','small' =>'','data' => $creator_profile]);
        $this->dashboard_data[] = collect(['title' => 'Creators with Content','small' =>'','data' => $creator_count]);
        $this->dashboard_data[] = collect(['title' => 'Published Courses','small' =>'','data' => $course_count]);
        $this->dashboard_data[] = collect(['title' => 'Published Learnbites','small' =>'','data' => $learnbite_count]);
        $this->dashboard_data[] = collect(['title' => 'Trial Listeners','small' =>'All Time','data' => $trial_user_count]);
        $this->dashboard_data[] = collect(['title' => 'Paid Listeners','small' =>'All Time','data' => $paid_user_count]);
    }


    private function getAllContent(){
      $this->all_learnbite_id_list =  DB::table('courses')->select('id')->where('isLearnBite','=',1)->get()->map(function ($content){
           return $content->id;
        });

      $this->all_course_id_list =  DB::table('courses')->select('id')->where('isLearnBite','=',0)->get()->map(function ($content){
           return $content->id;
        });

      $this->all_learnbite_lecture_id_list = DB::table('course_lectures')
          ->select('id')->whereIn('course_id',$this->all_learnbite_id_list)
          ->get()->map(function ($lecture){
          return $lecture->id;
      });

        $this->all_course_lecture_id_list = DB::table('course_lectures')
          ->select('id')->whereIn('course_id',$this->all_course_id_list)
          ->get()->map(function ($lecture){
          return $lecture->id;
      });
    }

    private function getMostPopular5Content($isLearnbite){
        $raw_data = collect($this->raw_listening)->filter(function ($data,$lecture_id) use ($isLearnbite){
                return  $isLearnbite ? $this->all_learnbite_lecture_id_list->contains($lecture_id) : $this->all_course_lecture_id_list->contains($lecture_id);
            });

        $result = $raw_data->map(function($value,$course_id){
            $course = Course::findorfail($course_id);
            $only_self =  $value->filter(function ($val) use($course){
               return  $val->user_id== $course->user_id;
            });
            $exclude_self =  $value->filter(function ($val) use($course){
               return  $val->user_id != $course->user_id;
            });
            $course->only_self = gmdate("H:i:s",  count($only_self));
            $course->listening_time = gmdate("H:i:s",  count($exclude_self));
            $course->different_user = count($exclude_self->groupBy('user_id'));
            
            return $course;
        });
        $result = $result->sortByDesc('listening_time');

        return $result->take(5);
    }
}

CodePudding user response:

You moved the file, but you didn't update the namespace.

namespace App\Http\Controllers;

needs to become:

namespace App\Http\Controllers\Admin;

(Presumably there's still a non-admin HomeController it's conflicting with.)

  • Related