Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Tenkai188's avatar

CRUD create null values

Hello all I made a CRUD for my Image model to be able to upload image using a form and register the data of those images (name, path,etc..) in the following table in my MySql database:

CREATE TABLE IF NOT EXISTS `images` (
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `path` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `project_id` bigint(20) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `images_project_id_foreign` (`project_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;

The problem is than when I upload a image the name and the path have NULL value and I dont understand why because I checked and the $imageName and $pathImg in the controler seems to have the correct values.

Model:

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
 
class Image extends Model
{
    use HasFactory;
    protected $fillable = [
        'name', 'path','project_id'
    ];
}

Controler:

public function store(Request $request)
{
    $projects = Project::all();
    $images = Image::all();
 
 
    $imageName = time().'.'.$request->image->extension();
 
    $request->image->move(public_path('image'), $imageName);
    $pathImg="/image/$imageName/";
 
    $request->validate([
        'name' => $imageName,
        'path' => $pathImg,
    ]);
 
    Image::create($request->all());
 
    return view('gallery')
        ->with('success','You have successfully upload image.')
        ->with('image',$pathImg)
        ->with('images',$images)
        ->with('projects',$projects);
 
}

my form:

<form method="POST" action="{{ route('image.store') }}" enctype="multipart/form-data">
                    @csrf
                    <input type="file" class="form-control" name="image" />
                    <input type="hidden" id="version" name="project_id" value="{{ $project->id }}" />

                    <button type="submit" class="btn btn-sm">Upload</button>
</form>
``
0 likes
6 replies
jlrdw's avatar

I have no idea what this is doing:

    $projects = Project::all();
    $images = Image::all();

You want to select all from table prior to storing a new record?

1 like
Tenkai188's avatar

@jlrdw It's because after uploading the image I want to be redirected to a page where all the projects and images are displayed and so I needed all the projects and images but now I think I misunderstood how to do that (sorry) so I made index function like this:

public function index()
    {
        $projects = Project::all();
        $images = Image::all();
        return view('gallery')->with('projects',$projects)->with('images',$images);
    }

and I use it in the create function:

public function store(Request $request)
    {

        $imageName = time().'.'.$request->image->extension();
        $request->image->move(public_path('img'), $imageName);
        $pathImg="/img/$imageName/";

        $request->validate([
            'name' => $imageName,
            'path' => $pathImg,
        ]);

        Image::create($request->all());

        return redirect()->route('projects.index');
    }
Tray2's avatar

Your code looks very strange to me.

public function store(Request $request)
{
    $projects = Project::all(); //Why are you fetching all projects in your store method?
    $images = Image::all();  //Why are you fetching all images in your store method?
 
 
    $imageName = time().'.'.$request->image->extension();
 
    $request->image->move(public_path('image'), $imageName);
    $pathImg="/image/$imageName/";
 

  //Validation should come before you store the image not after like it does here

    $request->validate([
        'name' => $imageName,
        'path' => $pathImg,
    ]); //This validation is off as well, you don't have any validation rules.
 
  
    Image::create($request->all()); //This is a really bad idea, you should store the validated array instead of the entire request. The reason for the name not being inserted is that you never use it when you create the Image row. 
 
//Here you should do a redirect with a status, and not return a view.
    return view('gallery')
        ->with('success','You have successfully upload image.')
        ->with('image',$pathImg)
        ->with('images',$images)
        ->with('projects',$projects);
 
}

Here is an example of a store method from a controller. The code below uses a FormRequest class to handle the validation.

public function store(GameFormRequest $request)
    {
        Game::create($request->validated());
        return redirect(route('games.index'))->with('status', 'Game added!');
    }
Tenkai188's avatar

@Tray2 Sorry I think I misunderstood what the validate function do What I'm trying to do is : put a value for 'path' and 'name' and after that registered the image data's in my database

Snapey's avatar
Snapey
Best Answer
Level 122

rearrange your file, you have multiple things confused;

    public function store(Request $request)
    {
        if($request->hasFile('image') {

            $request->validate([
				'image' => 'image',
			]);

            $imageName = time().'.'.$request->image->extension();
            $request->image->move(public_path('img'), $imageName);
            $pathImg="/img/$imageName/";

            Image::create([
                'name' => $imageName,
                'path' =< $pathImg
            ]);

        }

        return redirect()->route('projects.index');
    }

Please or to participate in this conversation.