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
setimage
= ?,slide_shows
.updated_at
= 2021-01-22 15:53:49 whereid
= 1)
'image' => $fileName ?? null,
if it can't be null, then don't set it to be null – aynber