Undefined variable: image How to upload image?
public function store(Request $request)
{
if($request->has('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$image->move(public_path('images/blogs'), $filename);
$image = $request->file('image')->getClientOriginalName();
}
$category = Category::create(array_merge($request->all(), ['image' => $image]));
$category->save();
return redirect(route('categories.index'));
}
First of all you need to set the image variables default in case no file is uploaded. Also check for hasFile. And if it still does not work, please post the form code
public function store(Request $request)
{
$image = '';
if($request->hasFile('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$image->move(public_path('images/blogs'), $filename);
$image = $request->file('image')->getClientOriginalName();
}
$category = Category::create(array_merge($request->all(), ['image' => $image]));
$category->save();
return redirect(route('categories.index'));
}
Try this.
public function store(Request $request)
{
if($request->hasFile('image')){
$image = $request->file('image')->storeAs(
'images/blogs',
$image->getClientOriginalName()
);
$request['image'] = $image;
}
$category = Category::create($request->all());
$category->save();
return redirect(route('categories.index'));
}
@sinnbeck
I updated your code but It did not save in database.
@davidifranco
your code has error $image in phpstroem
Like I said.. Please post the form code
@oxbir Yeah I missed that...
public function store(Request $request)
{
if($request->hasFile('image')){
$request['image'] = $request->file('image')->storeAs(
'images/blogs',
$request->file('image')->getClientOriginalName()
);
}
$category = Category::create($request->all());
return redirect(route('categories.index'));
}
blade
@extends('Admin.master')
@section('content')
<h3><i class="fas fa-plus-square ml-2"></i>افزودن دسته جدید</h3><hr>
<form action="{{ route('categories.store') }}" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label for="name">نام</label>
<input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}">
</div>
<div class="form-group">
<label for="href">آدرس</label>
<input type="text" class="form-control" id="href" name="href" value="{{ old('href') }}">
</div>
<div class="form-group">
<label for="lang">زبان</label>
<select id="lang" name="lang" class="form-control">
<option value="fa">فارسی</option>
<option value="en">انگلیسی</option>
</select>
</div>
<div class="form-group">
<label for="parent_id">زیر دسته</label>
<select name="parent_id" id="parent_id" class="form-control">
@foreach($categories as $value => $key)
<option value="{{ $value }}">{{ $key }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="icon">آیکن</label>
<input type="text" class="form-control" id="icon" name="icon" value="{{ old('icon') }}">
</div>
<div class="form-group">
<label for="background">پس زمینه</label>
<input type="file" class="form-control" id="background" name="background" value="{{ old('background') }}">
</div>
<div class="form-group">
<button class="btn btn-primary btn-lg btn-block"><i class="fas fa-check ml-2"></i>دخیره</button>
</div>
</form>
@endsection
The name you are using is background not image (the name must match)
public function store(Request $request)
{
$image = '';
if($request->hasFile('background')) {
$image = $request->file('background');
$filename = $image->getClientOriginalName();
$image->move(public_path('images/blogs'), $filename);
$image = $request->file('background')->getClientOriginalName();
}
$category = Category::create(array_merge($request->all(), ['image' => $image]));
$category->save();
return redirect(route('categories.index'));
}
@sinnbeck
I updated your code but It did not save in database.
Ok time to debug. Use dd() to check your variable content
For instance
public function store(Request $request)
{
$image = '';
dd($request->file('background'));
if($request->hasFile('background')) {
$image = $request->file('background');
$filename = $image->getClientOriginalName();
$image->move(public_path('images/blogs'), $filename);
$image = $request->file('background')->getClientOriginalName();
}
$category = Category::create(array_merge($request->all(), ['image' => $image]));
$category->save();
return redirect(route('categories.index'));
}
dd
Illuminate\Http\UploadedFile {#331 ▼
-test: false
-originalName: "3.jpg"
-mimeType: "image/jpeg"
-error: 0
#hashName: null
path: "C:\xampp\tmp"
filename: "php8E04.tmp"
basename: "php8E04.tmp"
pathname: "C:\xampp\tmp\php8E04.tmp"
extension: "tmp"
realPath: "C:\xampp\tmp\php8E04.tmp"
aTime: 2020-08-16 11:28:14
mTime: 2020-08-16 11:28:14
cTime: 2020-08-16 11:28:14
inode: 0
size: 82092
perms: 0100666
owner: 0
group: 0
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
linkTarget: "C:\xampp\tmp\php8E04.tmp"
}
Yes try it yourself to see if you can find an error
Like
$image = $request->file('background')->getClientOriginalName();
dd($image);
Ok if you check the database for the newest created row, what is in the image column?
You need to check your naming. The array keys should match the column names
$category = Category::create(array_merge($request->all(), ['background' => $image]));
@sinnbeck
I updated your code but It did not save in database.
Remove this line. And it should throw an error if it does not save.
$category->save()
I replaced in codes but my problem did not solve yet.
public function store(Request $request)
{
$background = '';
if($request->hasFile('background')) {
$background = $request->file('background');
$filename = $background->getClientOriginalName();
$background->move(public_path('images/backgrounds'), $filename);
$background = $request->file('background')->getClientOriginalName();
}
$category = Category::create(array_merge($request->all(), ['background' => $background]));
return redirect(route('categories.index'));
}
Do you have debug disabled in the env file? If create fails, there should be an error. Are you looking at the categories table?
No, Everything all right.
You can try this but I doubt it will help
$data = $request->all();
$data['background'] = $background;
Category::create($data);
I replaced in codes but my problem did not solve yet.
public function store(Request $request)
{
$background = '';
if($request->hasFile('background')) {
$background = $request->file('background');
$filename = $background->getClientOriginalName();
$background->move(public_path('images/backgrounds'), $filename);
$background = $request->file('background')->getClientOriginalName();
}
$data = $request->all();
$data['background'] = $background;
Category::create($data);
//$category = Category::create(array_merge($request->all(), ['background' => $background]));
return redirect(route('categories.index'));
}
The categories table is still completely empty?
No, All information is filling. The background field just does not save.
Show last row in table please
And it would have been nice to know that it does save in the database (just not that column). This would make it seem that it does not save at all
I updated your code but It did not save in database.
Wouldn't that trigger a mass assignment exception?
Please sign in or create an account to participate in this conversation.