Home > Net >  How can i reduce the following Laravel code?
How can i reduce the following Laravel code?

Time:10-22

My application is working perfectly, but I'm trying to keep my code as simple and clean as possible. I have a Controller that is receiving most of the data automatically, but I have 3 inputs that gonna be added to the request, more specifically, if the course is active(optional), is highlighted(optional), and the user id. The first and second are being treated with isset(), and the third is defined by the authenticated user. I would like to know if there is a better way(probably there is) to improve my code and simplify and remove useless lines of code. I have been reading about merge() in the Laravel requests section of the documentation, but I couldn't apply it for some reason. My requests are located in the $data variable, and the course is created at the final by passing this variable to the model Course with method create. The question is: is possible to remove the $data declaration for the other cases and keep it only for $data['is_active'], $data['is_highlighted'] and $data['user_id'], without missing the other parameters?

public function store(Request $request){
    $this->validate($request, []);
    
    try{
        $data["user_id"] = Auth::user()->id;
        $data["name"] = $request->name;
        $data["objectives"] = $request->objectives;
        $data["requirements"] = $request->requirements;
        $data["description"] = $request->description;
        $data["content"] = $request->content;
        $data["duration"] = $request->duration;
        $data["video_link"] = $request->video_link;
        $data["is_active"] = isset($request->is_active) ? 1 : 0;
        $data["is_highlighted"] = isset($request->is_is_highlighted) ? 1 : 0;

        $imageName = time().'.'.$request->image->extension();

        $request->image->move(public_path('uploads/images/courses/'), $imageName);

        $image = Image::create([
            'path' => ('uploads/images/courses/'.$imageName),
            'alt_text' => $request->alt ? $request->alt : $request->title,
        ]);

        $data["image_id"] = $image->id;

        if(isset($request->is_published))
            $data["published_at"] = Carbon::now()->toDateTimeString();
        
        $course = Course::create($data);
       

        return redirect()->route('courses.index')->withMessage(trans('crud.record_created'));

CodePudding user response:

You can reduce the number of lines like this

public function store(Request $request){ $this->validate($request, []);

    try {
        $data = $request->all();
        $data["user_id"] = Auth::user()->id;

        $imageName = time() . '.' . $request->image->extension();
        $request->image->move(public_path('uploads/images/courses/'), $imageName);
        $image = Image::create([
            'path' => ('uploads/images/courses/' . $imageName),
            'alt_text' => $request->alt ? $request->alt : $request->title,
        ]);

        $data["image_id"] = $image->id;
        $data["published_at"] = isset($request->is_published) ? Carbon::now()->toDateTimeString() : null;

        $course = Course::create($data);


        return redirect()->route('courses.index')->withMessage(trans('crud.record_created'));
    }

CodePudding user response:

Finally, I found out how to solve the problem:

public function store(Request $request){
    $this->validate($request, []);
    
    try{
        $request->merge(["user_id" => Auth::user()->id]);
        $request->merge(["is_active" => isset($request->is_active) ? 1 : 0]);
        $request->merge(["is_highlighted" => isset($request->is_highlighted) ? 1 : 0]);

        $imageName = time().'.'.$request->image->extension();

        $request->image->move(public_path('uploads/images/courses/'), $imageName);

        $image = Image::create([
            'path' => ('uploads/images/courses/'.$imageName),
            'alt_text' => $request->alt ? $request->alt : $request->title,
        ]);

        $request["image_id"] = $image->id;

        $this->courses->create($request->all());
        return redirect()->route('courses.index')->withMessage(trans('crud.record_created'));

    } catch (Exception $ex) {
        return redirect()->back()->withErrors($ex->getMessage())->withInput();
    }
}

By using this, your code gets more objective and clear, which allows other people understand better what you're doing.

  • Related