Laravel form no data submitted - php

My form in my-profile.blade.php looks like this:
<form id="profile-form" role="form" method="POST" action="{{ route('myprofile.store') }}">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<div class="col-md-6">
<label for="first_name">First Name</label>
<input type="text" class="form-control" id="first_name" placeholder="First Name" value="{{$currentUser->first_name}}" required>
</div>
...
<button type="submit" class="btn btn-primary">Save</button>
</form>
web.php file:
Route::resource('myprofile', 'MyProfileController');
MyProfileController controller:
public function store(Request $request)
{
Log::info("request:");
Log::info($request);
Log::info("input:");
Log::info(Input::all());
}
After logging the request and input:
local.INFO: request:
[local.INFO: array (
'_token' => 'S0u7OzktqMS5zVLr9WHwIq52EhGfZKoQWRD6XlCR',
)
local.INFO: input:
local.INFO: array (
'_token' => 'S0u7OzktqMS5zVLr9WHwIq52EhGfZKoQWRD6XlCR',
)
This is what I get.
I also tried the {{csrf_token()}}, the output is the same.
The controller's store function runs, so the action is set up okay.
What could be the problem?

I think there's no name attribute inside your input.

Try this:
<form id="profile-form" role="form" method="POST" action="{{ route('myprofile.store') }}">
{{csrf_field() }}
<div class="col-md-6">
<label for="first_name">First Name</label>
<input type="text" class="form-control" id="first_name" name="first_name" placeholder="First Name" value="{{$currentUser->first_name}}" required>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
Controller:
public function store(Request $request)
{
dd($request->get('first_name'));
}

Related

Laravel 8 - following system

I want to do following system without any laravel library, but when I submit form not saving anything. Where is the problem I don't know.
My route is:
Route::post('/follow' , [HomeController::class, 'follow'])->name('follow');
My blade is:
<form class="form-horizontal" action="{{route('follow')}}" method="POST"></form>
#csrf
<div class="form-group">
<input class="form-control" type="hidden" name="follower_id">
<input type="submit" class="site-btn" name="following_id" value="Follow" >
</div>
</form>
My controller is:
public function follow(Request $request){
$request->validate([
'follower_id'=>['required'],
'following_id'=>['required'],
]);
$follower_id = $request->follower_id;
$following_id = $request->following_id;
$save = Follow::create([
'following_id' => Auth::user()->id,
'follower_id' => $follower_id,
]);
if($save){
return back();
}else{
return back();
}
}
And my User model contains the following relationship
public function follows(){
return $this->hasMany('App\Models\Follow');
}
remove </form> in first line
<form class="form-horizontal" action="{{route('follow')}}" method="POST"></form>
#csrf
<div class="form-group">
<input class="form-control" type="hidden" name="follower_id">
<input type="submit" class="site-btn" name="following_id" value="Follow" >
</div>
</form>
it should be like this
<form class="form-horizontal" action="{{route('follow')}}" method="POST">
#csrf
<div class="form-group">
<input class="form-control" type="hidden" name="follower_id">
<input type="submit" class="site-btn" name="following_id" value="Follow" >
</div>
</form>

Laravel: how to get data with post

I've created a function in my routes that take some data from a view and send to another view
Route::post('/trans', function(){
$j = Input::get('r');
return view('movs.create')->with($j);
});
this route take the data from this form
<form action="/trans" method="POST">
#csrf
<div class="input-group">
<input type="hidden" class="form-control" name="r" value={{$cooperado->id}}>
<button type="submit" class="btn btn-primary">
<span>+</span>
</button>
</span>
</div>
</form>
but cant set the data in this other form on 'movs.create'
<form method="post" action="{{ route('movs.store') }}">
<div class="form-group">
#csrf
<label for="name">ID COOP:</label>
<input type="number" class="form-control" name="id_coop" readonly/> <-- data must be setted here
</div>
<div class="form-group">
<label for="price">VALOR MOVIMENTACAO:</label>
<input type="number" step=0.01 class="form-control" name="valor"/>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
when i try to set the data in id_coop input, laravel says that the variable doesnt exists
To set data in the create form, you may need to add a value attribute to the id_coop input:
<input type="number" class="form-control" name="id_coop" value="{{ $j }} readonly/>
Also, ->with() needs to be a key (variable name) and value:
Route::post('/trans', function(){
$j = Input::get('r');
return view('movs.create')->with('id_coop', $j);
});
This would mean you use {{ $id_coop }} instead.
with works with key value pair
Route::post('/trans', function(){
$j = Input::get('r');
return view('movs.create')->with('j',$j);
// or return view('movs.create', compact('j')); // it will extract in
//blade as $j
// or return view('movs.create', ['j' => $j]);
});
// you can fetch that data in blade as {{$j}}
<input type="number" class="form-control" name="id_coop" value="{{$j ?? ''}}" readonly/>
Example of with,
return view('greeting')->with('name', 'Victoria'); // name as key and Victorial as value.
{{$j ?? ''}} if data is not set then '' value.
//controller
public function Postdata(Request $request){
$data['j'] = Input::get('r');
return view('movs.create',$data);
}
//route
Route::post('/trans','yourController#Postdata');
//your view
<form method="post" action="{{ url('/store') }}">
<div class="form-group">
#csrf
<label for="name">ID COOP:</label>
<input type="number" class="form-control" name="id_coop" value="{{ $j }}" readonly/> <-- data must be setted here
</div>
<div class="form-group">
<label for="price">VALOR MOVIMENTACAO:</label>
<input type="number" step=0.01 class="form-control" name="valor"/>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
//store route
Route::post('/store','yourController#Savedata');
hope this helps

How to fix edit interface getting only first word of a column in table in Laravel 5.7?

My edit interface edit.blade.php only gets the first word of the name from my database, this is what the index.blade.php looks like
and when i click on the edit icon of the 3rd line it leads me to edit.blade.php which gives me this
"Nom d'établissement" textfield only gets the first word from the database
Everything looks fine in the database:
this is my edit.blade.php form:
<form method="post" action="{{ route('etablissements.update', $etablissement->id) }}">
#method('PATCH')
#csrf
<div class="col-5">
<div class="form-group">
<label for="nom">Nom Etablissement :</label>
<input type="text" class="form-control" name="nom" value={{ $etablissement->nom }} />
</div>
<div class="form-group">
<label for="price">E-Mail :</label>
<input type="text" class="form-control" name="email" value={{ $etablissement->email }} />
</div>
<div class="form-group">
<label for="quantity">Telephone:</label>
<input type="text" class="form-control" name="telephone" value={{ $etablissement->telephone }} />
</div>
</div>
<button type="submit" class="btn btn-primary">Confirmer</button>
</form>
this is edit function in the controller:
public function edit($id)
{
$etablissement = Etablissement::find($id);
return view('etablissements.edit', compact('etablissement'));
}
and this is update function in the controller:
public function update(Request $request, $id)
{
$request->validate([
'nom'=>'required',
'email'=> 'required',
'telephone' => 'required|numeric'
]);
$etablissement = Etablissement::find($id);
$etablissement->nom = $request->get('nom');
$etablissement->email = $request->get('email');
$etablissement->telephone = $request->get('telephone');
$etablissement->save();
return redirect('/etablissements')->with('success', 'Utilisateur édité');
}
Quote the value attribute.
<input type="text" class="form-control" name="nom" value="{{ $etablissement->nom }}" />
Without quotes, the second word in$etablissement->nom is interpreted as another attribute rather than part of the value of the value attribute.
The email and telephone values are showing up correctly because there are no spaces, but you should quote those as well just in case.

error in update file - Trying to get property of non-object, when there is no session

I have a form for the user edit his profile account. So it appears for each field the value if there is a value for each field with: "value="{{$user->name}}". But sometimes is appearing this error:
Trying to get property of non-object
Do you know how to correct the issue?
<form method="post" action="{{route('user.update')}}">
{{csrf_field()}}
<div>
<label for="name">Name</label>
<input type="text" value="{{$user->name}}" name="name" class="form-control" id="name">
</div>
<div>
<label for="surname">Surname</label>
<input type="text" value="{{$user->surname}}" name="surname" class="form-control" id="surname">
</div>
<!-- other fields -->
<input type="submit" value="Update"/>
</form>
The update method:
public function updateGeneralInfo(Request $request){
$this->validate($request, [
'name' => 'required',
]);
$user = Auth::user();
$user->name = $request->name;
...
$user->save();
return redirect()->back();
}
In your controller you can do a check before you return the view:
if(Auth::check()){
//return view and other stuff
}
else {
//redirect to login
}
In your blade:
<form method="post" action="{{route('user.update')}}">
{{csrf_field()}}
<div>
<label for="name">Name</label>
<input type="text" value="{{auth()->user()->name}}" name="name" class="form-control" id="name">
</div>
<div>
<label for="surname">Surname</label>
<input type="text" value="{{auth()->user()->surname}}" name="surname" class="form-control" id="surname">
</div>
<!-- other fields -->
<input type="submit" value="Update"/>
</form>

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]) }}"

Categories