Laravel 8 - following system - php

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>

Related

The GET method is not supported for this route. Supported methods: POST. in laravel 8

i keep getting this error "The GET method is not supported for this route. Supported methods: POST" and i dont know where i'm wrong.
this is my controller
public function admlogin(Request $request){
$request->validate([
'email'=>'required',
'password'=>'required'
]);
$user = Users::where('email', '=', $request->email)->first();
if ($user) {
if ($request->password, $user->password) {
$request->session()->put('id', $user->id);
return redirect('index');
} else {
return back()->with('fail','This email is incorrect');
}
} else {
return back()->with('fail','This email is incorrect');
}
}
and this is my web.php
Route::post('/adlogin', [UserController::class,'admlogin'])->name('adlogin');
Route::get('index/', 'App\Http\Controllers\UserController#index');
and my login.blade.php
<form action="adlogin" method="POST">
#csrf
<div class="form-group">
<input class="form-control" type="text" placeholder="Email">
</div>
<div class="form-group">
<input class="form-control" type="text" placeholder="Password">
</div>
<div class="form-group">
<button class="btn btn-primary btn-block" type="submit">Login</button>
</div>
</form>
You cant define route simply as
<form action="adlogin" method="POST">
have to use laravel helper to define route or url, as
<form action="{{ route('adlogin') }}" method="POST">
or
<form action="{{ url('adlogin') }}" method="POST">
Route::get('admlogin/', 'App\Http\Controllers\UserController#index');

ErrorException Undefined variable: toplam (View: C:\wamp64\www\laravel\resources\views\form.blade.php)

Variable toplam cannot be found
i'm using laravel latest version
HomeController like this
public function get_form()
{
return view('form');
}
public function post_form(Request $request)
{
$bDegisken=$request->birinci;
$iDegisken=$request->ikinci;
$toplam=$bDegisken+$iDegisken;
return view('form')->with('toplam',$toplam);
}
web.php my file is like this
//get
Route::get('/form' , [HomeController::class, 'get_form']);
//post
Route::post('/form' , [HomeController::class, 'post_form']);
form.blade.php
<form action="" method="post">
{{csrf_field()}}
<div class="col-md-6">
<input type="text" name="birinci">
</div>
<div class="col-md-6">
<input type="text" name="ikinci">
</div>
<input type="submit">
</form><br><br><br>
{{"sonuç"." = ".$toplam}}
you have 2 option
1st send data from get method
2nd make option data in view file
Step 1
public function get_form()
{
$toplam = "";
return view('form')->with('toplam', $toplam);
}
step 2
<form action="" method="post">
{{csrf_field()}}
<div class="col-md-6">
<input type="text" name="birinci">
</div>
<div class="col-md-6">
<input type="text" name="ikinci">
</div>
<input type="submit">
</form><br><br><br>
{{"sonuç"." = ".$toplam ?? ""}}<< --------------- it is optional here

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

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>

Laravel form no data submitted

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'));
}

Categories