lilo
1 week ago
112
4
Laravel

Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileDoesNotExist File does not exist

Posted 1 week ago by lilo

Hi,

I have a panel which I constructt using quick admin panel. In this panel, there is a crud named "FeaturedBox" and in this crud I have a field named "icon". But this icon is not photo type, it's text. Nevertheless I get this error (I wrote "icon-object-group1" on text area in panel ):

Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileDoesNotExist File C:\Users\....\storage\tmp/uploads/icon-object-group1 does not exist

The controller of the featuredBox crud is given below:

public function index(Request $request)
{
    abort_if(Gate::denies('featured_box_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

    if ($request->ajax()) {
        $query = FeaturedBox::query()->select(sprintf('%s.*', (new FeaturedBox)->table));
        $table = Datatables::of($query);

        $table->addColumn('placeholder', ' ');
        $table->addColumn('actions', ' ');

        $table->editColumn('actions', function ($row) {
            $viewGate      = 'featured_box_show';
            $editGate      = 'featured_box_edit';
            $deleteGate    = 'featured_box_delete';
            $crudRoutePart = 'featured-boxes';

            return view('partials.datatablesActions', compact(
                'viewGate',
                'editGate',
                'deleteGate',
                'crudRoutePart',
                'row'
            ));
        });

        $table->editColumn('id', function ($row) {
            return $row->id ? $row->id : "";
        });
        $table->editColumn('icon', function ($row) {
            if ($photo = $row->icon) {
                return sprintf(
                    '<a href="%s" target="_blank"><img src="%s" width="50px" height="50px"></a>',
                    $photo->url,
                    $photo->thumbnail
                );
            }

            return '';
        });
        $table->editColumn('title', function ($row) {
            return $row->title ? $row->title : "";
        });
        $table->editColumn('link', function ($row) {
            return $row->link ? $row->link : "";
        });

        $table->rawColumns(['actions', 'placeholder', 'icon']);

        return $table->make(true);
    }

    return view('admin.featuredBoxes.index');
}

public function create()
{
    abort_if(Gate::denies('featured_box_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');

    return view('admin.featuredBoxes.create');
}

public function store(StoreFeaturedBoxRequest $request)
{
    $featuredBox = FeaturedBox::create($request->all());

    if ($request->input('icon', false)) {
        $featuredBox->addMedia(storage_path('tmp/uploads/' . $request->input('icon')))->toMediaCollection('icon');
    }

    if ($media = $request->input('ck-media', false)) {
        Media::whereIn('id', $media)->update(['model_id' => $featuredBox->id]);
    }

    return redirect()->route('admin.featured-boxes.index');
}

public function edit(FeaturedBox $featuredBox)
{
    abort_if(Gate::denies('featured_box_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');

    return view('admin.featuredBoxes.edit', compact('featuredBox'));
}

public function update(UpdateFeaturedBoxRequest $request, FeaturedBox $featuredBox)
{
    $featuredBox->update($request->all());

    if ($request->input('icon', false)) {
        if (!$featuredBox->icon || $request->input('icon') !== $featuredBox->icon->file_name) {
            if ($featuredBox->icon) {
                $featuredBox->icon->delete();
            }

            $featuredBox->addMedia(storage_path('tmp/uploads/' . $request->input('icon')))->toMediaCollection('icon');
        }
    } elseif ($featuredBox->icon) {
        $featuredBox->icon->delete();
    }

    return redirect()->route('admin.featured-boxes.index');
}

public function show(FeaturedBox $featuredBox)
{
    abort_if(Gate::denies('featured_box_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');

    return view('admin.featuredBoxes.show', compact('featuredBox'));
}

public function destroy(FeaturedBox $featuredBox)
{
    abort_if(Gate::denies('featured_box_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

    $featuredBox->delete();

    return back();
}

public function massDestroy(MassDestroyFeaturedBoxRequest $request)
{
    FeaturedBox::whereIn('id', request('ids'))->delete();

    return response(null, Response::HTTP_NO_CONTENT);
}

public function storeCKEditorImages(Request $request)
{
    abort_if(Gate::denies('featured_box_create') && Gate::denies('featured_box_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');

    $model         = new FeaturedBox();
    $model->id     = $request->input('crud_id', 0);
    $model->exists = true;
    $media         = $model->addMediaFromRequest('upload')->toMediaCollection('ck-media');

    return response()->json(['id' => $media->id, 'url' => $media->getUrl()], Response::HTTP_CREATED);
}

I'm stuck with it. How can I fix this?

Please sign in or create an account to participate in this conversation.