Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

Laravel upload image

/* Route */
Route::resource('/admin/projects', ProjectsController::class);

/* Migration */
Schema::create('projects', function (Blueprint $table) {
            $table->id();
            $table->string('project_image')->nullable();
            $table->timestamps();
        });

/* Upload image */
<input type="file" name="project_image">

/* Contoller */
public function index()
    {
        $projects = Projects::all();
        return view('admin.projects.index', ['projects' => $projects]);
    }

public function create()
    {
        return view('admin.projects.create');
    }

public function store(Request $request)
    {
        $file = $request->file("project_image");
        $extenstion = $file->getClientOriginalExtension();
        $filename = time().'.'.$extenstion;
        $file->move('images/projects/', $filename);

        Projects::create([
            'project_image' => $filename,
        ]);
                
        return redirect()->route('projects.index')->with('success','Project created successfully.');
    }
    
/* Show image */
<img src="{{ asset('/images/projects/'.$project->project_image) }}" alt="..."/>
Source by laravel.com #
 
PREVIOUS NEXT
Tagged: #Laravel #upload #image
ADD COMMENT
Topic
Name
4+2 =