Requested URL not found on the server in XAMPP using laravel - php

I tried to edit and updated the employee info, unfortunately, it doesn't work
I fetch the employee id but when I sent the updated data it's not working.
it shows Requested URL not found on the server
this is my controller
public function edit_function($id){
$user = User::find($id);
return view('employee.empedit')->with('user',$user);
}
public function update(Request $request,$id){
$user = User::find($id);
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->phonenumber = $request->input('phonenumber');
$user->profession = $request->input('profession');
if($request->hasfile('images')){
$file= $request->file('images');
$extension = $file->getClientOriginalExtension();
$filename = time() . '.' . $extension;
$file->move('uploads/user/', $filename);
$user->images= $filename;
}
$user->save();
return redirect('empprofile')->with('success', 'Data Updated.');
}
this is my view
<form method="post" action="/updateimages/{{ $user->id }}" enctype="multipart/form-data">
<div class="container">
<div class="jumbotron">
<h2>Update The Information Of Employee</h2>
{{ csrf_field() }}
{{ method_field('PUT') }}
<div class="form-group">
<label >Name:</label>
<input type="text" class="form-control" id="name" placeholder="Enter Name" name="name" value="{{ $user->name }} ">
</div>
<div class="form-group">
<label >Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter email" name="email" value="{{ $user->email }} ">
</div>
<div class="form-group">
<label >Phone Number:</label>
<input type="text" class="form-control" id="phonenumber" placeholder="Enter Phone Number" name="phonenumber" value="{{ $user->phonenumber }} ">
</div>
<div class="form-group">
<label >Profession :</label>
<input type="text" class="form-control" id="profession" placeholder="Enter Profession" name="profession" value="{{ $user->profession }} ">
</div>
<div class="form-group">
<label >Image :</label>
<input type="file" class="form-control" id="images" placeholder="" name="images" value="{{ $user->images }}">
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit" name="submit" style="width:50%;">Update Data</button>
</div>
</div>
</div>
</form>
this is my route
Route::get('edit_profile/{id}' , "empController#edit_function");
Route::put('/updateimages/{id}', "empController#update");
it shows Requested URL not found on the server

Since I am not a Big fan of Url and id So i will go with
name based routing and Route Model Binding
Step 1: Refactor Routes
Route::get('edit_profile/{user}' , "empController#edit_function")
->name('user.editProfile');
Route::put('/updateimages/{user}', "empController#update")
->name('user.updateProfile');
Step 2: Refactor Controller Method
public function edit_function(User $user)
{
$user = $user;
return view('employee.empedit')->with('user',$user);
}
public function update(Request $request,User $user)
{
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->phonenumber = $request->input('phonenumber');
$user->profession = $request->input('profession');
if($request->hasfile('images')){
$file= $request->file('images');
$extension = $file->getClientOriginalExtension();
$filename = time() . '.' . $extension;
$file->move('uploads/user/', $filename);
$user->images= $filename;
}
$user->save();
return redirect('empprofile')->with('success', 'Data Updated.');
}
Step 3: Edit Html and Switch to route helper
<form method="POST" action="{{route('user.updateProfile',['user' => $user])}}" enctype="multipart/form-data">
Kindly Comment Below if you are facing any issues

Its because some other route replce your existing route. You can solve it by debugging. it will cost your time. I had a better solution,
You name your route. and call the route by route() function.
From your above information,
It may be,
in route ->
Route::put('/updateimages/{id}', "empController#update")->name('updateImage');
in view (form action) ->
<form method="post" action="{{ route('updateImage', $user->id ) }}" enctype="multipart/form-data">

Related

When I'm trying to insert the image in the database, it just shows me the path only, e.g. 'http://127.0.0.1:8000/storage/images'

I'm new to Laravel 8 and I'm trying to insert the data with an image, so when I try to insert the data it just shows me the inserted data and image path only. How can I solve this?
File RestaurantController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Restaurant;
class RestaurantController extends Controller
{
function add(Request $request)
{
$resto = new Restaurant;
$resto->name = $request->input('name');
$resto->email = $request->input('email');
$resto->address = $request->input('address');
$resto->create_at = $request->input('create_at');
$resto->uploaded_at = $request->input('uploaded_at');
$resto->image = $request->input('image');
// $resto->image = $request->file('image')->store('images');
if($request->hasFile('image'))
{
$image = $request->file('image');
$image_name = $image->getClientOriginalName();
$image->move(public_path('/images'), $image_name);
$image_path = "/images/" . $image_name;
}
$resto->save();
$request->session()->flash('status', 'Client entered successfully');
return redirect('list');
}
}
File add.blade.php
#extends('layout')
#section('content')
<div class="col-sm-6">
<form method="POST" action="" enctype="multipart/form-data">
#csrf
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" placeholder="Enter Your Name">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" class="form-control" id="email"
placeholder="name#example.com">
</div>
<div class="form-group">
<label for="address">Address</label>
<textarea class="form-control" name="address" id="address" rows="3"></textarea>
</div>
<div class="form-group">
<label for="create_at">Create At Date</label>
<input type="date" name="create_at" class="form-control" id="create_at">
</div>
<div class="form-group">
<label for="uploaded_at">Uploaded At Date</label>
<input type="date" name="uploaded_at" class="form-control" id="uploaded_at">
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="file" name="image" class="form-control" id="image">
</div>
<button type="Submit" clas="btn btn-primary">Submit</button>
</form>
</div>
#stop
File web.php
Route::get('/', [RestaurantController::class, 'index']);
Route::view('/add', 'add');
Route::post('/add', [RestaurantController::class, 'add'])->name('add');
I got <img src=http://127.0.0.1:8000/storage/images alt="image" height="50" width="50" > and what I want is http://127.0.0.1:8000/storage/images/`my_image_name`
When I perform return $request->all();, it shows me
{"_token":"ExiC1hv4sX3qrz6ZcQJJNfIL6bjblw938hfRkG8J","name":"test","email":"test#gmail.com","address":"testing address","create_at":"2022-05-30","uploaded_at":"2022-06-01","image":{}}
as a output.
You can use this code:
if($file = $request->hasFile('image')) {
$file = $request->file('image');
$fileName = $file->getClientOriginalName();
$destinationPath = public_path() . '/images';
$file->move($destinationPath, $fileName);
}
You can simply use the store() method.
...
if ($request->hasFile('image')) {
$image = $request->image->store('image'); // See store()
}
$restro->image = $image;
$restro->save();
Inside the blade template, you can retrieve the stored file like this:
<img src="{{ asset('storage/' . $restro->image) }}" />
Make sure you have created the symbolic link of your storage folder in your public folder. If you haven't, you can create it by this artisan command:
php artisan storage:link

Can't update user in laravel

I want some help with my code.
I try to update user data but it's not update anything.
User Name, Email, Posisson, Image.
Any help please.
My Route :
I used URL because route didn't work.
Route::get('editusers/{id}','UsersController#update');
My Controller:
public function edit($id)
{
$editusers=User::findOrFail($id);
return view('admin.users.EditUser', compact('editusers'));
}
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'posission' => 'required',
]);
$useredit = User::find($id);
$useredit->name = $request->input('name');
$useredit->email = $request->input('email');
$useredit->posission = $request->input('posission');
if($request->hasFile('file'))
{
$file = $request->file('file');
$filename = time().'.'.$file->getClientOriginalExtension();
Image::make($file)->resize(150, 150)->save(public_path('/admin/images/'.$filename));
$useredit->UserImg = $filename;
}
$useredit->save();
return redirect()->back();
}
HTML :
<form class="" action="{{url('editusers',Auth::user()->id)}}" role="form" enctype="multipart/form-data">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="PUT">
<label>Edit Your Profile :</label>
<div class="form-group">
<label>Name :</label>
<input class="form-control" value="{{$editusers->name}}" name="Name">
</div>
<div class="form-group">
<label>Email :</label>
<input class="form-control" value="{{$editusers->email}}" name="email">
</div>
<div class="form-group">
<label>Posisson :</label>
<input class="form-control" value="{{$editusers->posission}}" name="posission">
</div>
<div class="form-group">
<label>Image :</label>
<img src="{{ asset('admin') }}/images/{{$editusers->UserImg}}" alt="avatar" class="img-circle" style="max-height: 100px;">
<input type="file" id="file" name="file"/>
</div>
<input class="btn btn-success btn-mini deleteRecord" type="submit" name="submit" value="Update">
What I expect is that it updates my database.
As your form has
<form class="" action="{{url('editusers',Auth::user()->id)}}" role="form" enctype="multipart/form-data" method="POST">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="PUT">
so your route must have put(),
so it should be Route::put('editusers/{id}','UsersController#update');
also you can use #method('PUT') instead of <input type="hidden" name="_method" value="PUT"> and #csrf instead of {!! csrf_field() !!}
either change $useredit->name = $request->input('name'); to $useredit->name = $request->input('Name'); or in form
<input class="form-control" value="{{$editusers->name}}" name="Name"> to
<input class="form-control" value="{{$editusers->name}}" name="name">
Your route is wrong
Route::get('editusers/{id}','UsersController#update');
it supposed to be PUT
Route::put('editusers/{id}','UsersController#update');
The problem because you put wrong method at your route. Change it
// From
Route::get('editusers/{id}', 'UsersController#update')
// To
Route::put('editusers/{id}', 'UsersController#update')
Anyways, you should change your route to be standard. It should be:
//To show data you should use:
Route::get('users/edit/{id}', 'UsersController#show');
//To update user data.
Route::put('users/edit', 'UsersController#update');
i think you should use resources route to solve this:
Route::resource('editusers','UserController');
but first you need to run this command
php artisan make:controller UserController --resource

$file->move() function not showing moved file in the new folder?

I am trying to take a file that is uploaded by the user and move it to a folder named "Images" inside of my Laravel Public file, I have managed to get it to execute successfully with no errors, my only issue is that the uploaded file wont show inside of the /Images folder.
My function is as follows:
public function contactPost(Request $request)
{
if($request->hasFile('attachment'))
{
$file = $request->file('attachment');
$file->move('Users/jvb/laravel/public/Images', $file->getClientOriginalName());
echo 'File name '.$file->getClientOriginalName();
echo "<br>";
} else {
return "No";
}
}
The blade.php file is as follows:
#extends('layouts.master')
#section('content')
<form method="POST" enctype="multipart/form-data" action="{{ route('contactPost') }}">
#csrf
<div class="form-group">
<input class="form-control" name='name' type="text" placeholder="Name">
</div>
<div class="form-group">
<input class="form-control" name='email' type="email" placeholder="Email">
</div>
<div class="form-group">
<textarea class="form-control" name="message" id="" cols="30" rows="10" placeholder="Message"></textarea>
</div>
<div class="form-group">
<input class="form-control" name="attachment" type="file">
</div>
<div class="form-group">
<button type="submit" class="form-control">Send Message</button>
</div>
</form>
#endsection
Location of image folder within my laravel project is:
What am I doing wrong and how fix this?
Use public_path() instead of absolute path
$file->move(public_path()."/Images/", $file->getClientOriginalName());
UPDATE
In order to store the image into your database
$user = new User; //model
$filename = $file->getClientOriginalName();
$file->move(public_path("Images"), $filename);
$path = '/Images/' . $filename;
$user->avatar = $path;
$user->save();

I have the following error: "Type error: Too few arguments to function AlbumController::postEdit(), 1 passed and exactly 2 expected"

I have the following problem when trying to edit an "album", hopefully they can help me, I'm a little frustrated haha.
The Form
<form name="editalbum" action="{{ action('AlbumController#postEdit', $album->id) }}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<fieldset>
<h2>Editar <strong>{{$album->name}}</strong></h2>
<br></br>
<div class="form-group">
<label for="name">Nombre del proyecto</label>
<input name="name" type="text" class="form-control" value="{{ $album->name }}" required>
</div>
<div class="form-group">
<label for="description">Descripción del proyecto</label>
<textarea name="description" rows="10" cols="50" type="text" class="form-control" value="{{ $album->description }}" required></textarea>
</div>
<div class="form-group">
<label for="location">Locación:</label>
<input name="location" type="text" class="form-control" value="{{ $album->location }}" required>
</div>
<div class="form-group">
<label for="year">Año:</label>
<input name="year" type="text" class="form-control" value="{{ $album->year }}" required>
</div>
<button type="submit" class="btn btn-primary">Editar</button>
</fieldset>
</form>
So far I think everything is going well because I try to post in the ID of the model.
The function:
public function postEdit(Request $request, $id)
{
$album = Album::find($id);
$album = Album::all();
if(count($album) > 0){
$album->name = Input::get('name');
$album->description = Input::get('description');
$album->year = Input::get('year');
$album->location = Input::get('location');
$album->save();
Alert::success('Successfully Updated', 'Congratulations');
return view('admin.dashboard');
} else {
Alert::error('Facilities not found', 'Error');
return view('galeries');
}
I think you made error in routes.php
It should look like this:
Route::post('albums/update/{id}', ['uses' => 'AlbumController#postEdit']);
One solution will be to remove the DI Request object
public function postEdit($id)
{
//rest of code
}
note: the param has to be passed as a array
action="{{ action('AlbumController#postEdit', ['id' => $album->id]) }}"

Laravel form not submitting data to database

For some reason, my form isn't submitting any data to the database, but it looks fine from where I'm standing and the database can call the information to the form fine.
Since there's a lot of information for people to submit, I'm making the profile details not part of the login process. Still unfamiliar with how Laravel does these but I roughly get the process now that I've been fiddling.
One thing I'm wondering, is there a specific syntax for forms to write to the database, should I be naming the database's respective table names in the form? Or is that part of the Controller?
Should I be using Form Model Binding? It's a little hard to find information on that that's for the current version of Laravel though.
What am I missing?
//Route::get('NewUser', 'UserEntryController#create');
Route::post('NewUser', 'UserEntryController#UserForm');
//Route::get('NewUser', 'UserEntryController#create')->name('NewUser');
Route::post('NewUser', 'UserEntryController#UserForm')->name('submit');
Controller:
<?php
namespace App\Http\Controllers;
use App\UserEdit;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
class UserEntryController extends Controller
{
protected function create()
{
$id = UserEdit::find(715)->toArray();
return view('NewUser', compact('id'));
//return $array;
}
public function UserForm(Request $request) {
$email = $request['email'];
$first_name = $request['first_name'];
$password_hint = $request['password_hint'];
$last_name = $request['last_name'];
$user = UserEdit::find(715)->first();
$user->email = $email;
$user->First_Name = $first_name;
$user->Last_Name = $last_name;
$user->Password_Hint = $password_hint;
$user->save();
$id = UserEdit::find(715)->toArray();
return view('NewUser', compact('id'));
}
}
Blade:
#extends('layout')
#section('content')
<h1> Add Your Information {{ $id['name'] }}</h1>
<div class="row">
<div class="col-md-6">
<h3>Edit</h3>
<form action="{{ route('submit') }}" method="post">
<div class="form-group">
{{ csrf_field() }}
<label for="email">Your E-Mail</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
{{ csrf_field() }}
<label for="first_name">Your First Name</label>
<input class="form-control" type="text" name="first_name" id="first_name">
</div>
<div class="form-group">
{{ csrf_field() }}
<label for="last_name">Your Last Name</label>
<input class="form-control" type="text" name="last_name" id="last_name">
</div>
<div class="form-group">
{{ csrf_field() }}
<label for="password_hint">Your Password Hint</label>
<input class="form-control" type="text" name="password_hint" id="password_hint">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
</div>
#foreach ($id as $key=>$value)
{{ $value }}<br>
#endforeach
#stop
You should use $request->get('email') instead of $request['email'] and the same for everything else you want from the request, and I don't think you have to use ->first() when using ->find
There could be something wrong with the _token field, since there are five csrf fields in the form.
Try to remove this line
<input type="hidden" name="_token" value="{{ Session::token() }}">
and just leave one {{ csrf_field() }} in the form.

Categories