lesstenperfect
10 months ago

Code Review: Is this okay, or should I be doing something...

Posted 10 months ago by lesstenperfect

Here is my code for attaching files to client notes. Is this okay or should I be doing something different?

public function upload(Request $request)
    {
        $attachment = new Attachment();

        $request->file('file')->storeAs('/public/', preg_replace('/[, ]+/', '-', $request->file->getClientOriginalName()));

        $attachment->user_id        = $request->user()->id;
        $attachment->file_name      = preg_replace('/[, ]+/', '-', $request->file->getClientOriginalName());
        $attachment->original_name  = $request->file->getClientOriginalName();
        $attachment->file_type      = $request->file->getClientMimeType();
        $attachment->file_ext       = $request->file->getClientOriginalExtension();
        $attachment->file_path      = Storage::url($attachment->file_name);
        $attachment->web_path       = asset('/storage/' . $attachment->file_name);
        $attachment->custom_name    = $attachment->file_name;

        $attachment->save();

        $attachment->notes()->attach($attachment->id, ['client_notes_id' => 1]);

        return redirect()->back();
    }

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