LARAVEL - Did not update database when submit a form - laravel

When pressed the submit button nothing happens and did not update database. Please help me.
Controller:
public function update(Request $request, $id)
{
$this->validate(request(), [
'name' => 'required',
'email' => 'required|email|unique:users',
]);
//$user = Auth::user();
$user = user::where('id',$id)->update($request->except('_token','_method'));
$user->name = request('name');
$user->email = request('email');
$user->password = bcrypt(request('password'));
$user->firstname = $request->firstname;
$user->middlename = $request->middlename;
$user->lastname = $request->lastname;
$user->birthday = $request->birthday;
$user->nationality = $request->nationality;
$user->gender = $request->gender;
$user->civilstatus = $request->name;
$user->mobilenum = $request->mobilenum;
$user->worknum = $request->worknun;
$user->workadd = $request->workadd;
$user->homeadd = $request->homeadd;
$user->email = $request->email;
$user->save();
return redirect(route('user.user.show'))->with('message','Announcement Updated Succesfully');;
}
edit.blade.php
<form method="POST" action="{{ route('user.user.update', $user->id)}}">
{{ csrf_field() }}
{{ method_field('PATCH') }}
..
..
..
<button type="submit" class="btn btn-primary">Submit</button>
Back
Routes
Route::get('users/{user}', ['as' => 'user.user.show', 'uses' => 'UserController#index']);
Route::get('users/edit/{user}', ['as' => 'user.user.edit', 'uses' => 'UserController#edit']);
Route::PATCH('users/update/{users}', ['as' => 'user.user.update', 'uses' => 'UserController#update']);
Please help me. Thank you in advance!

From what i can see i think you're $request is not passing the unique validation on email.
since it's an update function leaving unique fields in a form (e.g. email - username) unchanged will result in a validation error try to bypass it with this:
'email' => ['required','email',Rule::unique('users')->ignore($id)]

Related

Can't update data in CRUD Laravel 8: ERROR: Ensure that the [name, id, data] of the Livewire component wasn't tampered with between requests

I want to update my CRUD App, but it always give me an error
this is the error
Livewire encountered corrupt data when trying to hydrate the [crud.edit-student-
component] component. Ensure that the [name, id, data] of the Livewire component wasn't
tampered with between requests.
so how to update the data when it change?
my code just give the error message like ADD NEW data, it's the same way as well
this is my code in the function:
public function mount($id)
{
$student= Student::where('id', $id)->first();
$this->fill([
'student_id' => $student->student_id,
'name' => $student->name,
'email' => $student->email,
'phone' => $student->phone,
]);
}
public function updatestudent()
{
$this->update([
$this->student_id = '',
$this->name = '',
$this->email = '',
$this->phone = '',
]);
$this->validate([
'student_id' => 'required|unique:students',
'name' => '',
'email' => '|email|unique:students,email' ,
'phone' => '|numeric|unique:students,phone',
]);
$student = Student::where('id', $this->student_edit_id)->firts();
$student -> student_id = $this->student_id;
$student -> name = $this->name;
$student -> email = $this->email;
$student -> phone = $this->phone;
dd($student);
$student-> save();
session()->flash('message', 'Student has been updated successfully');
$this->student_id = '';
$this->name = '';
$this->email = '';
$this->phone = '';
}
this is my code in blade file:
<form wire:submit.prevent="updatestudent">
<div class="form-group">
<label for="student_id">Student ID</label>
<input type="number" class="form-control" wire:model="student_id"/>
hopefully you help me Sir, thank's

Redirect after data insertion in laravel with success message

Hello guyzz I want to redirect back to form for new data entry which is actually form.blade.php view. I can see the data inserted successfully but how I can redirect with success message. my code is given.
public function store(Request $request)
{
$sname = $request->input('sname');
$fname = $request->input('fname');
$gradyear = $request->input('gradyear');
$phone = $request->input('phone');
$email = $request->input('email');
$paddress = $request->input('paddress');
$prog = $request->input('prog');
$job = $request->input('job');
$org = $request->input('org');
$position = $request->input('position');
$data = array(
'sname' => $sname,
"fname" => $fname,
"gradyear" => $gradyear,
"phone" => $phone,
"email" => $email,
"paddress" => $paddress,
"prog" => $prog,
"job" => $job,
"org" => $org,
"position" => $position
);
DB::table('tests')->insert($data);
echo "Data inserted Successfully";
}
On the controller after the code
return back()->with('status', 'successfully inserted');
On the form view
#if(session('status')
{{ session('status') }}
#endif
You can format your message in the CSS you choose
In Controller:
public function store(Request $request)
{
//
return back()->with('success', 'Data inserted Successfully');
}
In Blade:
#if(session()->has('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div>
#endif
Assuming the above code is working the way you want, you can redirect with flashed session data:
public function store(Request $request)
{
$sname = $request->input('sname');
$fname = $request->input('fname');
$gradyear = $request->input('gradyear');
$phone = $request->input('phone');
$email = $request->input('email');
$paddress = $request->input('paddress');
$prog = $request->input('prog');
$job = $request->input('job');
$org = $request->input('org');
$position = $request->input('position');
$data = array(
'sname' => $sname,
"fname" => $fname,
"gradyear" => $gradyear,
"phone" => $phone,
"email" => $email,
"paddress" => $paddress,
"prog" => $prog,
"job" => $job,
"org" => $org,
"position" => $position);
DB::table('tests')->insert($data);
return back()->with('status', 'Data inserted Successfully!');
}
And inside your blade file you can render with something like:
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data

Controller didn't get more arguments laravel

I'm trying to send two arguments and one request to controller with post request. This is the code where I send the arguments:
<form class="form-horizontal" name="form1" method="post" action="{{ route('shipment_view', $uniqueid, $category_name) }}" role="form" enctype="multipart/form-data">
And this is the controller where I'm sending the arguments:
public function storeShipment(Request $request, $number, $category_name){
$category = Category::where('category_name', $category_name)->first();
$user = Auth::user();
$item = new Item([
'id' => $user->id,
'category_id' => $category->id,
'unq' => $number,
'fullname' => $request->input('name'),
]);
$item->save();
}
But when I open the view it gives me error
ErrorException in NewShipmentController.php line 53:
Missing argument 3 for App\Http\Controllers\NewShipmentController::storeShipment()
Update:
My route function:
Route::post('/ship/preview/{number}',[
'uses' => 'Controllerr#storeShipment',
'as' => 'shipment_view'
]);
Any ideas?
replace your route function with the below one
Route::post('/ship/preview/{number}/{category_name}',[
'uses' => 'Controllerr#storeShipment',
'as' => 'shipment_view'
]);
Hope this solves your problem.
You can get the all the parameters through request it self But before this how you are defining the route
public function storeShipment(Request $request){
$number = $request->number;
$category_name = $request->number
}

Failed to save information to database

I'm quite new to laravel, created all that is needed but i can't edit or add new entries into the database. However i can successfully delete. what could be the problem not making the application write to the database?
This is my store function
public function store(Request $request)
{
//validate data
$validation=$this->validate($request, [
'username' => 'required|max:50',
'role_id' => 'required|max:50',
'email' => 'required|email|max:50',
]);
$user = new User;
$user->username = $request->username;
$user->role_id = $request->role_id;
$user->email = $request->email;
$user->save();
// redirect
Session::flash('message', 'Successfully added the record!');
Session::flash('alert-type', 'success');
return Redirect::to('user');
}
Route
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('auth.login');
});
Route::auth();
Route::get('/home', 'HomeController#index');
Route::resource('user', 'UserController');
Route::resource('role', 'RoleController');
User Controller
public function store(Request $request)
{
//validate data
$validation=$this->validate($request, [
'username' => 'required|max:50',
'role_id' => 'required|max:50',
'email' => 'required|email|max:50',
]);
$user = new User;
$user->username = $request->username;
$user->role_id = $request->role_id;
$user->email = $request->email;
$user->save();
// redirect
Session::flash('message', 'Successfully added the record!');
Session::flash('alert-type', 'success');
return Redirect::to('user');
}
Your routes
<?php
Route::get('/', function () {
return view('auth.login');
});
Route::auth();
Route::get('/home', 'HomeController#index');
Route::post('/user/store', 'UserController#store');
Your form
<form id="form-add-user" role="form" method="POST" action="{{ url('/user/store') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
...
</form>
Change your store method with below given and let me know which message will print?
public function store(Request $request)
{
//validate data
$validation=$this->validate($request, [
'username' => 'required|max:50',
'role_id' => 'required|max:50',
'email' => 'required|email|max:50',
]);
$user = new User;
$user->username = $request->username;
$user->role_id = $request->role_id;
$user->email = $request->email;
$user->save();
//Success redirect
if($user){
Session::flash('message', 'Successfully added the record!');
Session::flash('alert-type', 'success');
}
//Failed redirect
else {
Session::flash('message', 'Something went wrong!');
Session::flash('alert-type', 'success');
}
return Redirect::to('user');
}
For get the ride of basic CRUD with laravel example.

Laravel: Missing argument 1 for AdminController::getUpdateUser()

I'm new to larevel and i've been trying to solve this problem starting yesterday up to now and haven't make any progress and im stuck here....
PLEASE HELP :(
Here's my code for the routes:
Route::get('/admin/edit', array('uses' => 'AdminController#getUpdateUser', 'as' => 'getUpdateUser'));
Route::resource('admin1', 'AdminController');
Here's my code for the button viewing the update (I think the problem might be here and i cant think of a code that will call the id D: )
{{ link_to_action('AdminController#getUpdateUser', 'Edit', array($users->id),array('class' => 'btn btn-info')) }}
--
Here's my code at my AdminController:
public function getUpdateUser($id)
{
$users = User::find($id);
//load view and pass users
return View::make('admin.edit')
->with('users', $users);
}
public function updateUser($id)
{
$validate = Validator::make(Input::all(), array(
'firstname' => 'required',
'lastname' => 'required',
'middlename' => 'required',
'address' => 'required',
'birthday' => 'required',
'contact' => 'required|numeric',
'email' => 'required'
));
if ($validate->fails()) {
return Redirect::to('admin.edit')
->withErrors($validate)
->withInput(Input::except('password'));
}
else
{
$user = User::find($id);
$user->firstname = Input::get('firstname');
$user->lastname = Input::get('lastname');
$user->middlename = Input::get('middlename');
$user->address = Input::get('address');
$user->birthday = Input::get('birthday');
$user->contact = Input::get('contact');
$user->email = Input::get('email');
if($user->save())
{
return Redirect::route('admin.view')->with('success', 'USER HAS BEEN UPDATED');
}
else
{
return Redirect::route('admin.view')->with('fail', 'An error occured while updating the user. Please double check your inputs and try again.');
}
}
}
Here's my code for admin.edit
#extends('layouts.master')
#section('head')
#parent
<title>Manage Users</title>
#stop
#section('content')
<div class ="container">
<h1>Edit User</h1>
{{ Form::model($users, array('route' => array('admin1.updateUser', $users- >id), 'method' => 'PUT')) }}
--textboxes and inputs here--
{{ Form::submit('Update', array('class' => 'btn btn-info')) }}
Please help me T_T THANK YOU!!!
What you missed is putting the id actually in the route URL as a route parameter. Like this:
Route::get('/admin/edit/{id}', array('uses' => 'AdminController#getUpdateUser', 'as' => 'getUpdateUser'));

Resources