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

Lara_Love's avatar

validation.required

form send

<form action="{{ route('housebuild.store') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <input type="text" name="title">
    <input type="text" name="wall">
    <input type="text" name="sailing">
    <input type="file" name="images[]" id="inputImage" multiple
           class="form-control @error('images') is-invalid @enderror">
    @error('images')
    <span class="text-danger">{{ $message }}</span>
    @enderror
    <input type="text" name="adres" >
    <textarea name="desc"></textarea>
    <x-BtnSend/>
</form>

controller

 public function store(Request $request)
    {
        dd($request);
        $request->validate([
            'title'=> 'required',
            'desc'=> 'required',
            'wall'=> 'required',
            'sailing'=> 'required',
            'adres'=> 'required',
        
        ]);


        $input = $request->all();
        $images = [];
        if ($request->images){
            foreach($request->images as $key => $image)
            {
                $destinationPath = 'image/house/build/';
                $profileImage = time().rand(1,99).'.'.$image->extension();
                $resize = \Intervention\Image\ImageManagerStatic::make($image);
                $resize->resize(500, 300);
                $resize->save($destinationPath . $profileImage);
                $images[]['images'] = "$profileImage";
            }
        }
      $input['user_id'] = Auth::id();
        $builder = Build::create($input);
        foreach ($images as $path) {
            $builder->buildimages()->create(['path' => $path] );
        }
        return redirect()->back()

    }

model Build

 protected $fillable = [
        'user_id',

        'title',
        'desc',
        'wall',
        'sailing',
        'adres',
    ];
    public function user()
    {
        return $this->belongsTo(User::class);
    }

   public function buildimages()
    {
        return $this->hasMany(Buildimages::class);
    }

model Buildimages

protected $fillable = [
       'build_id',
      'image'
    ];

    public function build()
    {
       return $this->belongsTo(Build::class);
    }

tables

  Schema::create('builds', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->string('title');
            $table->text('desc');
            $table->string('wall');
            $table->string('sailing');
            $table->string('adres');

            $table->foreign('user_id')->references('id')->on('users')
                ->onDelete('cascade');
            $table->timestamps();
        });
    }

and

 Schema::create('buildimages', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('build_id');
            $table->text('image');
            $table->foreign('build_id')->references('id')->on('builds')->onDelete('cascade');
            $table->timestamps();
        });

dd

Illuminate\Http\Request {#46 ▼ // app\Http\Controllers\BuildController.php:54
  +attributes: Symfony\Component\HttpFoundation\ParameterBag {#48 ▶}
  +request: Symfony\Component\HttpFoundation\InputBag {#47 ▼
    #parameters: array:6 [▼
      "_token" => "pdOmqRC34TVjoc2l4JFQNCROycOGtVrUfv9wABUW"
      "title" => "title"
      "wall" => "wall"
      "sailing" => "sailng"
      "adres" => "address"
      "desc" => "<p>describtion describtion describtion describtion describtion describtion describtion describtion</p>"
    ]
  }
  +query: Symfony\Component\HttpFoundation\InputBag {#54 ▶}
  +server: Symfony\Component\HttpFoundation\ServerBag {#51 ▶}
  +files: Symfony\Component\HttpFoundation\FileBag {#50 ▼
    #parameters: array:1 [▼
      "images" => array:3 [▼
        0 => Symfony\Component\HttpFoundation\File\UploadedFile {#34 ▶}
        1 => Symfony\Component\HttpFoundation\File\UploadedFile {#35 ▶}
        2 => Symfony\Component\HttpFoundation\File\UploadedFile {#36 ▶}
      ]
    ]
  }

Error is :

SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default value

insert into
`buildimages` (`build_id`, `updated_at`, `created_at`)
values
(2, 2023 -01 -08 10: 44: 31, 2023 -01 -08 10: 44: 31)

A post should sometimes have one, sometimes five, and sometimes several images. The problem is inside the controller. How should I solve it?

0 likes
38 replies
Lara_Love's avatar

and i use package

composer require intervention/image

1 like
vincent15000's avatar

@LoverToHelp Not possible to help you => you are saying that there is an validation error, but you don't show the error.

Ben Taylor's avatar

I don't see an input for user_id but you are requiring it. Maybe that's the issue. Otherwise possibly related to this in your code:

'images' => 'required',
            'images.*' => 'required|image|mimes:jpeg,png,jpg,gif,rar,winrar,zip,svg|max:2048',

But hard to help when you don't provide much relevant information. Try dding the request and showing us the result

public function store(Request $request)
    {
dd($request);

1 like
vincent15000's avatar

@Ben Taylor I didn't notice that, well done ;). And it's the same with desc.

@lovertohelp You probably have to add the user_id value inside the controller.

Take attention to add all fields in the form.

Ben Taylor's avatar

Remove this line from the validator

'image' => 'required',
Lara_Love's avatar

Hello Mr @Ben Taylor

i remove it and new error.

SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default value

insert into
  `builds` (
    `title`,
    `wall`,
    `sailing`,
    `adres`,
    `desc`,
    `user_id`,
    `updated_at`,
    `created_at`
  )
Ben Taylor's avatar

@LoverToHelp ok so either follow @sinnbeck 's advise and fix your database structure so that image isn't a field on that table, or else add image to the $input array

Sinnbeck's avatar

Where did my answers go? Did someone delete theirs? Wtf

1 like
Lara_Love's avatar

@Sinnbeck Hello dear engineer I did. There is a problem if I put the picture

$table->text('image')->nullable();

Nothing is saved :

If I write the image in the database like this $table->text('image'); , this error

SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default value

gives

Sinnbeck's avatar

@LoverToHelp why do you want that column? I gave you the complete answer in the now deleted answer. Check your mail where the text should be.

Short answer. Add a one to many relationship to a builder_images table

1 like
Lara_Love's avatar

@Sinnbeck According to your speech and help, the image column is in buildimages table

Schema::create('buildimages', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('build_id');
            $table->text('image');
            $table->foreign('build_id')->references('id')->on('builds')->onDelete('cascade');
            $table->timestamps();

which is connected to the build table

 Schema::create('builds', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->string('title');
            $table->text('desc');
            $table->string('wall');
            $table->string('sailing');
            $table->string('adres');
            $table->foreign('user_id')->references('id')->on('users')
                ->onDelete('cascade');
            $table->timestamps();

with one to many relation. Now the error is related to that image table. (The image is saved, but if it is

$table->text('image');

in table buildimages

it gives an error

SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default value

, and if it is

$table->text('image')->nullable();

, the name of the null image is saved. like blew picture https://s2.uupload.ir/files/null_bk41.png

Sinnbeck's avatar

I would also recommend you name the table build_images instead of buildimages. This will make the model be named BuildImage instead of Buildimage

Lara_Love's avatar

@Sinnbeck

The controller

    public function store(Request $request)
    {
        $request->validate([
            'title'=> 'required',
            'desc'=> 'required',
            'wall'=> 'required',
            'sailing'=> 'required',
            'adres'=> 'required',
        ]);


        $input = $request->all();
        $images = [];
        if ($request->images){
            foreach($request->images as $key => $image)
            {
                $destinationPath = 'image/house/build/';
                $profileImage = time().rand(1,99).'.'.$image->extension();
                $resize = \Intervention\Image\ImageManagerStatic::make($image);
                $resize->resize(500, 300);
                $resize->save($destinationPath . $profileImage);
                $images[]['images'] = "$profileImage";
            }
        }
        $input['user_id'] = Auth::id();
        $builder = Build::create($input);
        foreach ($images as $path) {
            $builder->buildimages()->create(['path' => $path] );
        }
        return redirect()->back();
Sinnbeck's avatar

@LoverToHelp you are nesting it for some reason? And wrapping it in a string?

$images[]= $profileImage;
Sinnbeck's avatar

And the column needs to match the database. I just assumed that you would name it "path". You named it "image"

$builder->buildimages()->create(['image' => $path] );
Lara_Love's avatar

@Sinnbeck Sorry, I'm a little confused. If I define the name here and change it to

$images[]= $profileImage;

, it cannot recognize it The image is saved as the path. I showed in the image and code below https://s2.uupload.ir/files/und_e6.png

 public function store(Request $request)
    {
        $request->validate([
            'title'=> 'required',
            'desc'=> 'required',
            'wall'=> 'required',
            'sailing'=> 'required',
            'adres'=> 'required',
        ]);
        $input = $request->all();
        $images[]= $profileImage;
        if ($request->images){
            foreach($request->images as $key => $image){
                $destinationPath = 'image/house/build/';
                $profileImage = time().rand(1,99).'.'.$image->extension();
                $resize = \Intervention\Image\ImageManagerStatic::make($image);
                $resize->resize(500, 300);
                $resize->save($destinationPath . $profileImage);
                $images[]['images'] = "$profileImage";}}
        $input['user_id'] = Auth::id();
        $builder = Build::create($input);
        foreach ($images as $path) {
            $builder->buildimages()->create(['path' => $path] );}
        return view('house.build.index')
Lara_Love's avatar

@Sinnbeck Unfortunately, it gives an error

SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default value

insert into
  `buildimages` (`build_id`, `updated_at`, `created_at`)
values
  (6, 2023 -01 -08 11: 41: 52, 2023 -01 -08 11: 41: 52)
Lara_Love's avatar

@Sinnbeck

In the meantime, Happy New Year to you and your family. I hope you have beautiful moments with your family and friends

Sinnbeck's avatar

@LoverToHelp thanks. Same to you.

Can you show the code after the updates? Also make sure that image is fillable on the model :)

protected $guarded = ['id'];
Lara_Love's avatar

@Sinnbeck

thank you The beginning of our new year is on the solar date and it is called Nowruz in date 03-21-2023 ad

class Buildimages extends Model
{
    use HasFactory;

    protected $fillable = [
       'build_id',
      'image'
    ];

    public function build()
    {
       return $this->belongsTo(Build::class);
    }
```
and


```
class Build extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'title',
        'desc',
        'wall',
        'sailing',
        'adres',
    ];
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function buildimages()
    {
        return $this->hasMany(Buildimages::class);
    }

```

and 

```
 public function store(Request $request)
    {
        $request->validate([
            'title'=> 'required',
            'desc'=> 'required',
            'wall'=> 'required',
            'sailing'=> 'required',
            'adres'=> 'required',
        ]);
        $input = $request->all();
        $images = [];
        if ($request->images){
            foreach($request->images as $key => $image){
                $destinationPath = 'image/house/build/';
                $profileImage = time().rand(1,99).'.'.$image->extension();
                $resize = \Intervention\Image\ImageManagerStatic::make($image);
                $resize->resize(500, 300);
                $resize->save($destinationPath . $profileImage);
                $images[]= $profileImage;}}
        $input['user_id'] = Auth::id();
        $builder = Build::create($input);
        foreach ($images as $path) {
            $builder->buildimages()->create(['path' => $path] );
        }
        return view('house.build.index')
```
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@LoverToHelp you didn't change this?

$builder->buildimages()->create(['path' => $path] );
//change to 
$builder->buildimages()->create(['image' => $path] );
1 like
Lara_Love's avatar

@Sinnbeck You are one of the best. We hope to celebrate together next year in a beautiful world

Sinnbeck's avatar

@LoverToHelp you are also returning a view. A post, put or delete request should always return a redirect (unless it's ajax)

1 like
Sinnbeck's avatar

@LoverToHelp happy to help. Glad to hear it works. Yeah I hope the world does not turn any worse the next year :)

1 like
Lara_Love's avatar

thank you @Sinnbeck I got into trouble in edit . I will try a little. If I can't solve it, I will get help. thanks

Lara_Love's avatar

Hello Mr @Sinnbeck

You know, I save data with this image controller, but I have a problem to edit and display the image / the image is not displayed. And should I create another foreach for images? controller

 public function store(Request $request)
    {
        $request->validate([
            'title'=> 'required',
            'desc'=> 'required',
            'wall'=> 'required',
            'sailing'=> 'required',
            'adres'=> 'required',
        ]);
        $input = $request->all();
        $images = [];
        if ($request->images){
            foreach($request->images as $key => $image)
            {
                $destinationPath = 'image/house/build/';
                $profileImage = time().rand(1,99).'.'.$image->extension();
                $resize = \Intervention\Image\ImageManagerStatic::make($image);
                $resize->resize(500, 300);
                $resize->save($destinationPath . $profileImage);
                $images[]= $profileImage;
            }}
        $input['user_id'] = Auth::id();
        $builder = Build::create($input);
        foreach ($images as $path) {
            $builder->buildimages()->create(['image' => $path] );
        }
        return redirect('housebuild')

models

class Build extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'title',
        'desc',
        'wall',
        'sailing',
        'adres',
    ];
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function buildimages()
    {
        return $this->hasMany(Buildimages::class);
    }
}

and

class Buildimages extends Model
{
    use HasFactory;

    protected $fillable = [
       'build_id',
      'image'
    ];

    public function build()
    {
       return $this->belongsTo(Build::class);
    }
}

```

view for show


```
@foreach($build as $item)
   <h3>{{ $item->title }}</h3>
         @foreach(???????)
                 <img class="img-fluid" src="{{ asset('image/house/build/'.$item->image) }}" >
        @endforeach
   <p>{{ $item->desc }}</p>
@endforeach            
```

Please or to participate in this conversation.