Search code examples
phplaraveleloquentlaravel-8

Laravel returning query exception with no error message


I am doing a simple create operation. Below is the code on controller.

$this->validate($request, [
        'title' => 'required',
        'description' => 'required',
        'content' => 'required',
        'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg',
        'categories' => 'required',
        'sections' => 'required',
    ]);
    // try {
        $imagePath = $request->file('image')->storeAs('images/post', Carbon::now() . substr(str_replace(['.','?','/'], '-', $request->title),0,50) . '.' . $request->file('image')->getClientOriginalExtension(), 'public');
        $post = new Post;
        $post->title = $request->title;
        $post->description = $request->description;
        $post->content = $request->content;
        $post->tags = $request->tags ?? '';
        $post->slug = substr(str_replace(['.','?','/'],'-',$request->description),0,500);
        $post->image = $imagePath;

        if ($post->save()) {
            $post->getCategories()->sync($request->categories);
            $post->getSections()->sync($request->sections);
            UtilityFunctions::createHistory('Created Post with Id ' . $post->id . ' and title ' . $post->title, 1);
            return redirect()->route('admin.posts.index')->with(['successMessage' => 'Success!! Post created']);
        } else {
            return redirect()->back()->with(['errorMessage' => 'Error!! Post not created']);
        }
    // } catch (Exception $e) {
    //     return redirect()->back()->with(['errorMessage' => $e->getMessage()]);
    // }

It throws Illuminate\Database\QueryException but the exception has no message. It does not tell what went wrong.

Screenshot


Solution

  • I removed the title part inheriting from post title while saving the imagepath. The new image now only inherits current date and time as filename and path.

    $imagePath = $request->file('image')->storeAs('images/post', Carbon::now()  . '.' . $request->file('image')->getClientOriginalExtension(), 'public');
    

    This project uses devanagari characters and seems like it had trouble saving the image path containing special characters from devanagari.