Soo's avatar
Level 2

Save Image in database

Hello friends, i have form with image upload i need to save the image in table database when i create new offer .Any help! this is the code:

<div class="grid grid-cols-1 mt-5 mx-7">
            <label for="file" class="uppercase md:text-sm text-xs text-gray-500 text-light font-semibold">Image</label>
               <div class="max-w-md mx-auto rounded-lg overflow-hidden md:max-w-xl">
                   <div class="md:flex">
                       <div class="w-full p-3">
                           <div class="relative border-dotted h-48 rounded-lg  border-2 border-cyan-700 bg-gray-100 flex justify-center items-center">
                               <div class="absolute">
                                   <div class="flex flex-col items-center "> <i class="fa fa-folder-open fa-4x text-blue-700 "></i> <span class="block text-gray-400 font-normal">Attach you image here</span> </div>
                               </div> <input type="file" class="h-full w-full opacity-0" name="">
                           </div>
                       </div>
                   </div>
               </div>
           </div>

and this is the controller function:

public function store()
    {
        request()->validate([
        'name' => 'required|string|max:40',
        'description' => 'required|string|max:255',
        'job_photo_path' => 'required'
        ]);

        $jobOffer = jobOffer::create([
        'name' => request('name'),
        'description' => request('description'),
        'job_photo_path' => request('job_photo_path'),
        ]);
        return redirect('/jobOffer');
                          
      }
0 likes
10 replies
Soo's avatar
Level 2

@frankielee show me this error

Error Call to a member function store() on null.

SilenceBringer's avatar

@soo that's because you file input do not have name.

<input type="file" class="h-full w-full opacity-0" name="">

replcae it with

<input type="file" class="h-full w-full opacity-0" name="job_photo_path">
3 likes
Soo's avatar
Level 2

@silencebringer

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\jobOffer;

class JobOfferController extends Controller
{
    public function index(jobOffer $jobOffers)
    {
        $jobOffers = jobOffer::all();
        return view('jobOffer.index',[ 'jobOffers'=>$jobOffers ]);   
    }

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

    public function store(Request $request)
    {
        request()->validate([
        'name' => 'required|string|max:40',
        'description' => 'required|string|max:10000',
        'job_photo_path' => 'required|'
        ]);
         
        $path = $request->file('job_photo_path')->store('image_name');
        $jobOffer = jobOffer::create([
        'name' => request('name'),
        'description' => request('description'),
        'job_photo_path' => $path
        ]);
        return redirect('/jobOffer')->with('success', 'Your Job Offer has been created successfully!');
                          
      }

}
frankielee's avatar

add dd() before the codes


public function store()
{
		dd($request()->all()); <---this
}
1 like
Soo's avatar
Level 2

@frankielee Thank's a lot it working now . i just put the encrypt line:

<form method="POST" action="/joboffer" enctype="multipart/form-data"> 

Please or to participate in this conversation.