Laravel $request->all() is empty - laravel

I start study laravel.
The route to contacts page
Route::match(['get', 'post'], '/contacts', [ 'uses' => 'Admin\ContactController#show', 'as' => 'contacts' ] );
class ContactController extends Controller {
public function show( Request $request ) {
print_r( $request->all() );
return view( 'default.contacts', [ 'title' => 'Contacts' ] );
}
}
Form
<form method="post" action="{{ route('contacts') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="inputEmail4">Name</label>
<input type="text" class="form-control" id="inputEmail4" placeholder="Name">
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
When i submit the form, i get an array with token.
Array
(
[_token] => JMTxTwh5Cb4sPeDjGcVetgTt2yGy6mDsFs6jW3Tx
)
What can be a problem?
Thanks for answer.

You are missing the name in your inputs. Please add to them.
<form method="post" action="{{ route('contacts') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="inputEmail4">Name</label>
<input type="text" class="form-control" name="name" id="inputEmail4" placeholder="Name">
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" name="address" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>

Related

Once the password has been reset, I cannot log into my application

I'm following the documentation on this link about resetting the password:
Reset Password
So first I create the view containing a form just to request the email and once the email has been received I click on the button to reset the password.
So far everything ok! Once I reset the password I try to log into my app with the new password but I cannot log in with this new password.
Can anyone kindly tell me where the problem lies? Thank you all
Route:
Route::get('/forgot-password', [Controller::class,'passwordRequest'])->middleware('guest')->name('password.request');
Route::post('/forgot-password', [Controller::class,'passwordEmail'])->middleware('guest')->name('password.email');
Route::get('/reset-password/{token}', [Controller::class,'passwordReset'])->middleware('guest')->name('password.reset');
Route::post('/reset-password', [Controller::class,'passwordUpdate'])->middleware('guest')->name('password.update');
Controller:
public function passwordRequest() {
return view('auth.forgot-password');
}
public function passwordEmail(Request $request) {
$request->validate(['email' => 'required|email']);
$status = Password::sendResetLink(
$request->only('email')
);
return $status === Password::RESET_LINK_SENT
? back()->with(['status' => __($status)])
: back()->withErrors(['email' => __($status)]);
}
public function passwordReset($token) {
return view('auth.reset-password', ['token' => $token]);
}
public function passwordUpdate(Request $request) {
$request->validate([
'token' => 'required',
'email' => 'required|email',
'password' => 'required|min:8|confirmed',
]);
$status = Password::reset(
$request->only('email', 'password', 'password_confirmation', 'token'),
function ($user, $password) {
$user->forceFill([
'password' => Hash::make($password)
])->setRememberToken(Str::random(60));
$user->save();
event(new PasswordReset($user));
}
);
return $status === Password::PASSWORD_RESET
? redirect()->route('login')->with('status', __($status))
: back()->withErrors(['email' => [__($status)]]);
}
View:
ForgotPassword
<div class="row mt-3 mx-5">
<div class="col-12 col-md-6 namelabel">
<form action="{{route('password.email')}}" method="post">
#csrf
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" name="email">
</div>
<button type="submit" class="btn btn-primary mb-5">Invia</button>
</form>
</div>
</div>
ResetPassword
<div class="row mt-3 mx-5">
<div class="col-12 col-md-6 namelabel">
<form action="{{route('password.email')}}" method="post">
#csrf
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" name="email">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" name="password">
</div>
<div class="mb-3">
<label for="password_confirmation" class="form-label">Conferma password</label>
<input type="password" class="form-control" name="password_confirmation">
</div>
<div class="mb-3">
<input type="hidden" class="form-control" name="token" value="{{$token}}" >
</div>
<button type="submit" class="btn btn-primary mb-5">Invia</button>
</form>
</div>
</div>
You're almost done! In your auth.reset-password view, you must send the request to the password.update route, not the password.email route.
The password.update route will run the passwordUpdate method to update the User's password.
https://laravel.com/docs/9.x/passwords#password-reset-handling-the-form-submission
<div class="row mt-3 mx-5">
<div class="col-12 col-md-6 namelabel">
<form action="{{ route('password.update') }}" method="post">
#csrf
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" name="email">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" name="password">
</div>
<div class="mb-3">
<label for="password_confirmation" class="form-label">Conferma password</label>
<input type="password" class="form-control" name="password_confirmation">
</div>
<div class="mb-3">
<input type="hidden" class="form-control" name="token" value="{{$token}}" >
</div>
<button type="submit" class="btn btn-primary mb-5">Invia</button>
</form>
</div>
</div>

Laravel : no errors and no update on database happens after saving the edit

I have two tables, user and technicien, with a one to one relation. After editing technicien information through edit form and saving, no update happens on database and no errors as well.
Here is my code:
controllers
public function edit($id)
{
$technicien=technicien::find($id);
$user = $technicien->user;
return view('technicien.edit',['technicien'=>$technicien])->with('user',$user);
}
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
$technicien->user->update($request->get('user'));
$user->nom = $request->update('nom');
return redirect('/technicien');
}
View
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Technicien</h1>
<form action="{{ route('technicien.update', $technicien->technicien ) }}" method="update">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="nom">Nom</label>
<input id="nom" type="text" class="form-control" name="user[nom]" value="{{$user->nom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control" name="user[prenom]" value="{{$user->prenom}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form-control" name="user[email]" value="{{$user->email}}" >
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name="moyenne_avis" class="form-control" value ="{{$technicien->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name="actif" class="form-control" value ="{{$technicien->actif}}" >
</div>
<div class="form-group">
<input type="submit" value="enregistrer" class="form-control btn btn-primary">
</div>
</div>
</form>
</div>
</div>
#endsection
route.php
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::patch('/technicien/{id}', 'TechnicienController#update')-
>name('technicien.update');
You just need to pass parameters to update function.
Read docs
public function update(Request $request, $id)
{
$technicien=technicien::find($id);
$technicien->update($request->all());
$technicien->user->update([
'nom' => $request->nom,
'premon' => $request->premon,
'email' => $request->email
]);
return redirect('/technicien');
}
Also from the docs
You should define which model attributes you want
to make mass assignable. You may do this using the $fillable property
on the model.

Laravel :error when updating the database after editing data

I have two tables table "technician" and table "user" with a connection one to one and I have a form through which I edit the technician information. When I save the changes I get an error and the update doesn't work.Here is my form my code and a screenshot of the error Thank you in advance.
route.php
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::patch('/technicien/{id}', 'TechnicienController#update')-
>name('technicien.update');
edit.php
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Technicien</h1>
<form action="{{ route('technicien.update', $moyenne_avis->id , $actif->id , $user->nom , $user->prenom ,$user->email ) }}" method="update">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="nom">Nom</label>
<input id="nom" type="text" class="form-control" name="user[nom]" value="{{$user->nom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control" name="user[prenom]" value="{{$user->prenom}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form-control" name="user[email]" value="{{$user->email}}" >
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name="moyenne_avis" class="form-control" value ="{{$moyenne_avis->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name="actif" class="form-control" value ="{{$moyenne_avis->actif}}" >
</div>
<div class="form-group">
<input type="submit" value="enregistrer" class="form-control btn btn-primary">
</div>
</div>
</form>
</div>
</div>
#endsection
controller1.php
public function edit($id)
{
$technicien=technicien::find($id);
$user = $technicien->user;
return view('technicien.edit',['moyenne_avis'=>$technicien],
['actif'=>$technicien],['user_id'=>$technicien])->with('user',$user);
}
controller2.php
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
$technicien->user->update($request->get('user'));
return redirect('technicien');
}
You are passing too many param and in your route you only take one param!
I think you only need one param in your view which is technician ID and which will find the record in your controller query!
You can try follow code with removing unusable params from form:
<form action="{{ route('technicien.update', $moyenne_avis->id ) }}" method="POST">
And your route looks like:
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::post('/technicien/{id}', 'TechnicienController#update')->name('technicien.update');
Hope this helps you!

Hidden type input value not being passed in Laravel gallery Project

I have created an album. I am trying to upload photos to the album and storing the album_id through the 'hidden' type input. When i check the source code, album_id is shown in 'value' attribute but unfortunately the value is not being passed to the query during form submission.
My create method of PhotoController which shows the form
public function create($id)
{
$albums = Album::where('id',$id)->first();
return view('admin.pages.photos',compact('albums', 'id'));
}
Here is the form.
<div class="container">
<div class="row">
<a class="btn btn-success" href="/gallery/{{$albums->slug}}">Back to Gallery</a>
<h4>Upload Photos to <strong>{{$albums-> name}}</strong> Gallery</h4>
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
<img class="thumbnail" src="/images/gallery/{{$albums->cover_pic}}" alt="{{$albums->name}}">
</div>
<div class="col-md-8">
<form class="form-horizontal" action="/photo" method="POST" enctype="multipart/form-data" >
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-8">Photo Title:</label>
<input type="text" name="photo_name" class="form-control" placeholder="Name of the Photo" value="{{ old('photo_name') }}" >
</div>
<div class="form-group">
<label class="col-md-8">Description</label>
<input type="text" name="desc" class="form-control" placeholder="Write Description" value="{{ old('desc') }}">
</div>
<div class="form-group">
<label class="col-md-8">Upload Pic</label>
<input type="file" name="photo" class="form-control" value="{{old('photo')}}" >
</div>
<input type="hidden" name="album_id" value="{{$albums->id}}">
<button type="submit" name="submit" class="btn btn-success waves-effect waves-light m-r-10">Submit</button>
</form>
and the store method
public function store(Request $request)
{
$this->validate($request, [
'photo_name'=>'required|min:3',
'desc'=>'required',
'photo'=>'required'
]);
$photo = new Photo;
$photo->album_id = $request->album_id;
$photo->photo_name = $request->photo_name;
$str = strtolower($request->photo_name);
$photo->slug = preg_replace('/\s+/', '-', $str);
if($file=$request->file('photo')){
$name = time().'.'.$file->getClientOriginalName();
$file->move('images/gallery', $name);
$photo['photo'] = $name;
}
$photo->desc = $request->desc;
$photo->save();
return redirect()->back()->with('status', 'Photo Successfully Added!');
}

dd($request->country_flag); returns null in laravel

This is my form to upload a file:
<div class="col-lg-6 col-lg-offset-3">
<form method="post" action="{{ route('admin.country.store') }}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="country_id">Country ID</label>
<input type="number" class="form-control" id="country_id" name="country_id">
</div>
<div class="form-group">
<label for="country_name">Country name</label>
<input class="form-control" type="text" id="country_name" name="country_name">
<p class="danger">{{ $errors->first('country_name') }}</p>
</div>
<div class="form-group">
<label for="alternate_title">Alternate Title</label>
<input class="form-control" type="text" id="alternate_title" name="alternate_title">
<p class="danger">{{ $errors->first('alternate_title') }}</p>
</div>
<div class="form-group">
<label for="country_flag">Country Flag</label>
<input class="" type="file" id="country_flag" name="country_flag">
<p class="danger">{{ $errors->first('country_flag') }}</p>
</div>
<div class="btn-group" role="group">
<button class="btn btn-default" type="reset">Reset</button>
<button class="btn btn-success" type="submit">Upload</button>
</div>
</form>
This is a function in my controller to handle form request.
public function store(Request $request)
{
$new_country = new SelectCountry();
$message = [
'required' => "This field can not be empty",
];
$this->validate($request, [
'country_name' => 'required',
'alternate_title' => 'required',
'country_flag' => 'required',
], $message);
dd($request->country_flag);
}
When I do dd($request->country_flag);, it returns null. It seems like file is not uploaded by the form.
What am I doing wrong?
I'm not sure if you can or not access a file like you are doing. Try this:
$file = $request->file('country_flas');
Try this
$input = $request->all();
$file = $input['country_flag'];
dd($file);

Resources