Ad

Upload Image With Laravel 6

- 1 answer

I'm trying to upload an image but when I submit the form nothing happens at all. I end up with an empty image folder and a null image field in the database. I already tried many potential solutions online up until this point.

Controller

class ReportsController extends Controller
{
    /**
     * Only logged in users can access reports.
     */

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $reports = Report::with(['victim', 'lost_vehicle'])->get();
        return view('reports.index')->with('reports', $reports);
    }

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

    public function store(ReportRequest $request)
    {
        $userid = Auth::user()->id;
        request()->validate([
            // 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg',
        ]);
        $victim = Victim::create([
            'vic_name' => $request->input('name'),
            'vic_statment' => $request->input('statment'),
            'case_id' => uniqid(),
            'report_status' => $request->input('status'),
            'user_id' => $userid,
        ]);

        if ($files = $request->file('image')) {
            $carImage = time().'.'.$files->getClientOriginalExtension();
            $request->image->move(public_path('images'), $carImage);
            $victim->lost_vehicles()->create([
                'image' => $carImage,
            ]);
        }
        $victim->lost_vehicles()->create([
            'type' => $request->input('type'),
            'model' => $request->input('model'),
            'production_year' => $request->input('year'),
            'color' => $request->input('color'),
            'value' => $request->input('value'),
            'marks' => $request->input('marks'),
            'plate_number' => $request->input('plate'),
            'chassis_number' => $request->input('chassis'),
            'last_location' => $request->input('location'),
            'last_time_spotted' => $request->input('last_time_spotted'),
            'victim_id' => $victim->id,
        ]);

        return dd($victim);
    }
}

I set the form to accept files {!! Form::open(['url'=>'reports'], array('files'=> true)) !!}.

Ad

Answer

You've got an issue with this part of your code:

if ($files = $request->file('image')) {
    ...
    $victim->lost_vehicles()->create([
        'image' => $carImage,
    ]);
}
$victim->lost_vehicles()->create([
    ...
]);

You're correctly checking for the existence of $request->file('image'), but you're calling $victim->lost_vehicles()->create() twice. This is fine if lost_vehicles is a hasMany() relationship, but you'll end up with 2 records in the database, one with an image and no information, and another will all the information and no image. Consider refactoring:

$image = null;
if ($files = $request->file('image')) {
    $carImage = time().'.'.$files->getClientOriginalExtension(); 
    $request->image->move(public_path('images'), $carImage);

    $image = $carImage;
}

$victim->lost_vehicles()->create([
    'type' => $request->input('type'),
    'model' => $request->input('model'),
    'production_year' => $request->input('year'),
    'color' => $request->input('color'),
    'value' => $request->input('value'),
    'marks' => $request->input('marks'),
    'plate_number' => $request->input('plate'),
    'chassis_number' => $request->input('chassis'),
    'last_location' => $request->input('location'),
    'last_time_spotted' => $request->input('last_time_spotted'),
    'victim_id' => $victim->id,
    'image' => $image
]);

What this does is sets $image to null initially, and then if your check passes, sets it to the string representing the uploaded image. Then, it is included in the create() method below, and you should have all data associated.

Aside from that, I haven't seen that method for uploading the image. I use:

\Storage::disk("local")->putFileAs("images", $files, $carImage);
// Modified to fit your variable names

And also make sure you've called php artisan storage:link to correctly link storage/app to public.

Ad
source: stackoverflow.com
Ad