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)
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
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.