Controller
public function store_room_detail(Request $request)
{
$request->validate([
'room_image' => 'required|mimes:png,jpg,jpeg|max:2048'
]);
return redirect()->back();
}
View:
<form method="POST" action="{{ route('admin.store-room-detail') }}" enctype="multipart/form-data">
#csrf
<div class="card-body">
<div class="form-group">
<label for="room_image">Room Image</label>
<input type="file" class="col-md-6" id="room_image" name="room_image[]">
<a href="javascript:void(0)" class="btn btn-primary add_more mb-3">
Add More
</a>
<div class="wrap"></div>
#if ($errors->has('room_image'))
<span class="text-danger">{{ $errors->first('room_image') }}</span>
#endif
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
In the above code. Validation working fine but when I upload jpg, png image then it will again show that The room image must be a file of type: png, jpg, jpeg. I don't why? Please help me.
Thank You
you should validate like this because you send an array in request:
$request->validate([
'room_image' => 'required',
'room_image.*' => 'required|mimes:png,jpg,jpeg|max:2048'
]);
Related
I am creating a database for books. When I was testing CRUD without image upload(normal CRUD), it was working. But when I added book cover image file as part of data input I get error "
Argument #1 ($rules) must be of type array, Illuminate\Http\Request given, called in
". I didn't understand what caused it, is it because validation or path image folder will be stored in?
Here it is image of error.
Model/Buku.php
class Buku extends Model
{
use HasFactory;
protected $fillable = [
'cover',
'nama_buku',
'author',
'terbitan',
'barcode',
'ketersediaan',
];
}
I suspect the error is in controllers code but didn't know why normal CRUD succeed but image upload CRUD fails.
Controllers/BukuController.php
protected function store(Request $request)
{
$request->validate($request, [
'cover' => 'required|file|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'nama_buku' => 'required',
'author' => 'required',
'terbitan' => 'required',
'barcode' => 'required',
'ketersediaan' => 'required',
]);
$input = $request->all();
if ($cover = $request->file('cover')) {
$destinationPath = 'cover/';
$profileImage = date('YmdHis') . "." . $cover->getClientOriginalExtension();
$cover->move($destinationPath, $profileImage);
$input['cover'] = "$profileImage";
}
Buku::create($input);
return redirect()->route('buku.index')->with('success','Buku has been created successfully.');
}
I don't know if the errors is actually from input so just in case here is views create blade
Views/create.blade.php
<form action="{{ route('buku.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Cover:</strong>
<input type="file" name="cover" placeholder="Cover Buku"
class="block w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100" />
#error('cover')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
#enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Nama Buku:</strong>
<input type="text" name="nama_buku" class="form-control" placeholder="Nama Buku">
#error('nama_buku')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
#enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Author:</strong>
<input type="text" name="author" class="form-control" placeholder="Author Buku">
#error('author')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
#enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Terbitan:</strong>
<input type="text" name="terbitan" class="form-control" placeholder="Terbitan Buku">
#error('terbitan')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
#enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Barcode:</strong>
<input type="text" name="barcode" placeholder="Barcode Buku" id="scanner" />
#error('barcode')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
#enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Ketersediaan:</strong>
<input type="number" name="ketersediaan" class="form-control" placeholder="Ketersediaan Buku">
#error('ketersediaan')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
#enderror
</div>
</div>
<button type="submit" class="btn btn-primary ml-3">Submit</button>
</div>
</form>
I also tried image upload code when I was still using Laravel 4, still get the same error. Did anyone know how to fix this?
The error states it pretty clearly. You pass a Request object where an array is expected.
Instead of using the object directly, you should use the all() method, which creates an array with all the posted fields in your request. Or in this case, you can leave the request->all() out, because the method is already called on the request object.
$request->validate($request,...)
$request->validate($request->all(),...)
$request->validate(['cover' => ....])
#########EDIT
It is also not recommended to insert all request data. You should only insert validated data.
$input = $request->all();
Buku::create($input);
You should use one of the two following approaches to only insert validated data: https://laravel.com/docs/10.x/validation#working-with-validated-input
I am trying to redirect logged in users to the main dashboard view, that I've titled as "xwelcome.blade.php" temporarily.
I've attached my routes:
Route::get('xwelcome', [CustomAuthController::class, 'xwelcome']);
Route::get('login', [CustomAuthController::class, 'index'])->name('login');
Route::post('custom-login', [CustomAuthController::class, 'customLogin'])->name('login.custom');
Route::get('registration', [CustomAuthController::class, 'registration'])->name('register-user');
Route::post('custom-registration', [CustomAuthController::class, 'customRegistration'])->name('register.custom');
Route::get('logout', [CustomAuthController::class, 'logOut'])->name('logout');
This is my CustomAuthController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;
use Session;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
class CustomAuthController extends Controller
{
public function index()
{
return view('auth.login');
}
public function customLogin(Request $request)
{
$request->validate([
'email' => 'required',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
return redirect()->intended('xwelcome')
->withSuccess('Signed in');
}
return redirect("login")->withSuccess('Login details are not valid');
}
public function registration()
{
return view('auth.registration');
}
public function customRegistration(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
$data = $request->all();
$check = $this->create($data);
return redirect("xwelcome")->withSuccess('You have signed-in');
}
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password'])
]);
}
public function dashboard()
{
if(Auth::check()){
return view('xwelcome');
}
return redirect("login")->withSuccess('You are not allowed to access');
}
public function logOut() {
Session::flush();
Auth::logout();
return Redirect('login');
}
}
Login button:
<form role="form" method="post" action="{{ route('login.custom') }}">
#csrf
<div class="mt-4">
<div class="form-group">
<label class="form-control-label">Email address</label>
<div class="input-group input-group-merge">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-user"></i></span>
</div>
<input type="email" class="form-control" id="input-email" placeholder="name#example.com">
</div>
</div>
<div class="form-group mb-4">
<div class="d-flex align-items-center justify-content-between">
<div>
<label class="form-control-label">Password</label>
</div>
<div class="mb-2">
Lost password?
</div>
</div>
<div class="input-group input-group-merge">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-key"></i></span>
</div>
<input type="password" class="form-control" id="input-password" placeholder="Password">
<div class="input-group-append">
<span class="input-group-text">
<a href="#" data-toggle="password-text" data-target="#input-password">
<i class="fas fa-eye"></i>
</a>
</span>
</div>
</div>
</div>
<button type="submit" class="btn btn-sm btn-primary btn-icon rounded-pill">
<span class="btn-inner--icon"><i class="fas fa-long-arrow-alt-right"></i></span>
Sign In
</button></div>
</form>
After I fill in my test account details, and press the button, I see this in my address bar
https://example.com/login?_token=0GddRaD453thhRFL7KWgEQ5DnZ3q7B4rL5gaajuM
Any help would be appreciated. Cheers
I can't see the input fields in your form!.
Have you tried to display the errors after validating the form?.
I think that you are redirected back to :
https://example.com/login?_token=0GddRaD453thhRFL7KWgEQ5DnZ3q7B4rL5gaajuM
because you are missing the input:email and input:password, what your are seeing is only the csrf token that laravel generates.
i Think issue is on button . try this out
<form role="form" method="post" action="{{ route('login.custom') }}">
#csrf
<div class="mt-4">
<button type="submit" class="btn btn-sm btn-primary btn-icon rounded-pill">
<span class="btn-inner--icon"><i class="fas fa-long-arrow-alt-right"></i></span>
Sign In
</button></div>
</form>
you are missing name attribute in your input field. the code should look like
<form role="form" method="post" action="{{ route('login.custom') }}">
#csrf
<div class="mt-4">
<div class="form-group">
<label class="form-control-label">Email address</label>
<div class="input-group input-group-merge">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-user"></i></span>
</div>
<input name="email" type="email" class="form-control" id="input-email" placeholder="name#example.com">
</div>
</div>
<div class="form-group mb-4">
<div class="d-flex align-items-center justify-content-between">
<div>
<label class="form-control-label">Password</label>
</div>
<div class="mb-2">
Lost password?
</div>
</div>
<div class="input-group input-group-merge">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-key"></i></span>
</div>
<input name="password" type="password" class="form-control" id="input-password" placeholder="Password">
<div class="input-group-append">
<span class="input-group-text">
<a href="#" data-toggle="password-text" data-target="#input-password">
<i class="fas fa-eye"></i>
</a>
</span>
</div>
</div>
</div>
<button type="submit" class="btn btn-sm btn-primary btn-icon rounded-pill">
<span class="btn-inner--icon"><i class="fas fa-long-arrow-alt-right"></i></span>
Sign In
</button></div>
</form>
I am trying to update multiple images but unfortunately I am getting error please help me how can i resolved that ? thank u.
please check error
Invalid argument supplied for foreach()
controller
public function update(Request $request, $roomId)
{
if($request->has('images')){
foreach($request->file('images') as $value){
$extension = $value->getClientOriginalExtension();
Storage::disk('wfh')->put($value->getFilename() . '.' . $extension,
File::get($value));
$roomDetail->image = $value->getFilename() . '.' . $extension;
$roomDetail = RoomDetail::where('room_id',$roomId)->first();
$roomDetail->update([
'room_id' => $roomId,
'image' => $roomDetail->image,
]);
}
}
Html view
<form action="{{route('room.update',$editRooms->id)}}" method="POST" class="needs-validation"
novalidate enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="row">
<div class="col-md-8">
<div class="input-field">
<label class="active">Images Upload</label>
<div class="input-images" style="padding-top: .5rem;" ></div>
#foreach ($roomDetails as $value)
<img height="100px" class="mt-4 m-4" width="100px" src="{{
Config('wfh.file')
.$value->image}}" alt="">
<input type="hidden" name="images[]" value="{{$value->image}}" >
#endforeach
</div>
</div>
</div>
<div class="col-md-3">
<div class="modal-footer">
<button type="submit" id="btnSubmit" class="btn btn-primary btn-lg w-100">Add
Room</button>
</div>
</div>
</form>
I have a website I am currently editing tht was built with laravel. I have have a page that displays a "details of shipped package"
I added a form to page to update the current location of the shipped package on the details page.
<div class="row mb-30">
<div class="col-lg-12 mt-2">
<div class="card border--dark">
<h5 class="card-header bg--dark">#lang('Courier Location')</h5>
<div class="card-body">
<form action="{{route('....')}}" method="POST">
#csrf
<div class="modal-body">
<div class="form-group">
<label for="current_location" class="form-control-label font-weight-bold">#lang('Current Location')</label>
<input type="text" class="form-control form-control-lg" name="current_location" value="{{__($courierInfo->current_location)}}" required="">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn--primary"><i class="fa fa-fw fa-paper-plane"></i>#lang('Update')</button>
</div>
</form>
</div>
</div>
</div>
I have also added the update function in the controller
public function courierUpdate(Request $request, $id)
{
$request->validate([
'current_location' => 'required',
]);
$courierInfoUpdate =CourierInfo::findOrFail($id);
$courierInfoUpdate->current_location = $request->current_location;
$courierInfoUpdate->save();
$notify[] = ['success', 'Courier location info has been updated'];
return back()->withNotify($notify);
}
I am having problem with the laravel route to call that should be added as form action.
Declare a route on the web.php
Route::post('/courier-Update/{id}','App\Http\Controllers\YourControllerName#courierUpdate')->name('courier.Update');
and now just call this route in your form and also pass the id of that courier
like this:
route('courier.Update',$courier->id)
You can add a new route in routes/web.php
//import your controller at Beginning of the file
use App\Http\Controllers\YourController;
Route::post('update_location/{id}', [YourController::class, 'courierUpdate'])->name('updateLocation');
//or
Route::post('update_location/{id}', 'YourController#courierUpdate')->name('updateLocation');
And then in your blade view
<form action="{{ route('updateLocation', [ 'id' => $id]) }}" method="POST">
#csrf
</form>
trying to upload an image through a post Form:
this is my controller:
public function store(Request $request)
{
$request->validate([
'title'=>'required',
'content'=>'required',
'image'=>'required',
'description'=>'required'
]);
$image = $request->image->store('images');
Post::create([
'title' => $request->input('title'),
'content' => $request->input('content'),
'description' => $request->input('description'),
'image' => $image
]);
return redirect(route('posts.index'))->with('success', 'Posts Submitted successfully');
}
I used : php artisan storage:link which generated a folder named images in public directory: Absolute path is:
C:\xampp\htdocs\blogPholio\public\storage\images\KWDKRrty2ijXjrgrismvyiV4k6UAQNrFogJl9W2f.jpeg
in my blade I am trying to show image through/:
<img src="{{Storage::url($post->image)}}">
My form in the create blade looks like:
<form action="{{ route('posts.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Image:</strong>
<input type="file" name="image" class="form-control" placeholder="upload:">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</div>
</form>
Thanksa lot
Try this
<img src="{{asset(Storage::disk('local')->url($post->image))}}"/>