Uchchhas's avatar

Image Uploading Problem

Code in my controller function is given below...

public function create(Request $request) { $imgPath = ''; if($request->hasFile('teacherImg')){ $rootDir = 'Dashboard/images/teachers/'; $file = $request->file('teacherImg'); $fileName = imgNameFormatting($file->getClientOriginalName()); $fileName = explode('.',$fileName); $newFileName = $fileName[0].'-'.time().'.'.$fileName[1]; $request->file('teacherImg')->move($rootDir, $newFileName); $imgPath = '/'.$rootDir.$newFileName; }

   $teachers = new Teacher;
   if(!empty($request->file('teacherImg'))){
     $blog->img = $imgPath;
   }
   $teachers->name = $request->name;
   $teachers->phone = $request->phone;
   $teachers->email = $request->email;
   $teachers->facebook = $request->facebook;
   $teachers->nid = $request->nid;
   $teachers->gender = $request->gender;
   $teachers->presentAddress = $request->presentAddress;
   $teachers->permanentAddress = $request->permanentAddress;
   $teachers->department = $request->department;
   $teachers->designation = $request->designation;
   $teachers->employeeId = $request->employeeId;
   $teachers->salary = $request->salary;
   $teachers->dateOfJoinin = date ("Y-m-d", strtotime($request->dateOfJoinin));
   $teachers->save();

   return redirect('/teacher-list');
}

After running this I got the error is "Creating default object from empty value"

How can I solve this ? Please help...................

0 likes
1 reply
jlrdw's avatar

I don't know about all that, but I know this works for an upload:

    public function add(Request $request)
    {
        if (isset($_POST['submit'])) {
            //echo "made it here";
            $lid = DB::table('dc_dogs')->count();
            $lid = $lid + 1;
            $file = $request->file('ufile');
            $file_name = $file->getClientOriginalName();
            $file_ext = $file->getClientOriginalExtension();

            $fileInfo = pathinfo($file_name);
            $filename = $fileInfo['filename'];
            $newname = $filename . $lid . "." . $file_ext;
            $destinationPath = ASSET . 'upload/imgdogs';
            echo $destinationPath;
            $file->move($destinationPath, $newname);
            $dogpic = Cln::fixValue($newname);
            $dogname = ucfirst(Cln::fixValue($_POST['dogname']));
            $sex = ucfirst($_POST['sex']);
            $comments = Cln::fixValue($_POST['comments']);
            $adopted = (isset($_POST['adopted']) == '1' ? '1' : '0'); ///added
            $lastedit = date("Y-m-d H:i:s");
            //echo "adpt=" . $adopted . "aaa";

            if (!isset($error)) {
                $postdata = array(
                    'dogpic' => $dogpic,
                    'dogname' => $dogname,
                    'sex' => $sex,
                    'comments' => $comments,
                    'adopted' => $adopted,
                    'lastedit' => $lastedit
                );

                DB::table('dc_dogs')->insert($postdata);
            }
        }
        $title = 'Dog add';
        $view = 'dog/add';
        $layout = ViewLayout::getLayout('admin/addtp');
        $content = View::make($view);


        return view($layout)->with('content', $content)->with('title', $title);
    }

Please or to participate in this conversation.