1
votes

I am struggling with my working on a upload a file that was made out of Laravel. I had followed some tutorials on youtube but I can seem to expand the toggle button when the screen size is reduced to the smallest. Since I'm starting a new project, I applied all of this with Laravel and opened the edit.blade.php file in my local directory. Please help me! I have been stuck for hours.

migration

public function up()
{
    Schema::create('slide_shows', function (Blueprint $table) {
        $table->id();
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->string('image');
        $table->string('lang');
        $table->timestamps();
    });
}

SlideShow.php

protected $fillable = [
    'user_id',
    'lang',
    'image'
];

web.php

Route::resource('slideShows', 'SlideShowController')

public function update(SlideShowRequest $request, SlideShow $slideShow)
{
    if ($request->hasFile('image'))
    {
        $file = $request->file('image');
        $name = time();
        $extension = $file->getClientOriginalExtension();
        $fileName = $name . '.' . $extension;
        $file->move(public_path('images/slideShows'), $fileName);
    }
    $data = [
        'user_id' => 1, //auth()->id(),
        'image' => $fileName ?? null,
        'lang' => $request->lang
    ];
    $slideShow->update($data);
    return redirect()->route('groups.index');
}

edit.blade.php

    <form action="{{ route('slideShows.update', $slideShow->id) }}" method="POST" enctype="multipart/form-data">
        @csrf
        @method('PUT')
        <div lang="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label for="lang">lang</label>
                    <select id="lang" name="lang" class="form-control">
                        <option value="fa" {{ $slideShow->lang == 'fa' ? 'selected' : '' }}>fa</option>
                        <option value="ar" {{ $slideShow->lang == 'ar' ? 'selected' : '' }}>ar</option>
                        <option value="en" {{ $slideShow->lang == 'en' ? 'selected' : '' }}>en</option>
                    </select>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label for="image">image</label>
                    <input type="hidden" name="image" value="{{ $slideShow->image == $slideShow->image ? $slideShow->image : '' }}">
                    <input id="image" type="file" name="image" class="form-control">
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="form-group">
                <button type="submit" class="btn btn-success">save</button>
            </div>
        </div>
    </form>

I see this error

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image' cannot be null (SQL: update slide_shows set image = ?, slide_shows.updated_at = 2021-01-22 15:53:49 where id = 1)

1
Your answer is in your error.Robin Singh
'image' => $fileName ?? null, if it can't be null, then don't set it to be nullaynber
What is the solution?Mahmoud Khosravi
@MahmoudKhosravi allow null on the image column in your database..Gert B.
@RobinSingh he should use migrations for that in laravel.. otherwise when you move the application he will get the same error again.Gert B.

1 Answers

1
votes

When you want to allow a column to be null you can set it as nullable in your migration:

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

You need to create a new migration for that to update the database table. and run the migrations:

https://laravel.com/docs/8.x/migrations#updating-column-attributes