Reputation: 463
So I've modified my default User table to a table called 'customusers' where all the data for registered users will go. The data includes name/email/gender/description etc. When they register, they are redirected to a main page where all their data is printed. Now, if they want to edit that data, they click the Edit button, which should take them to a page called "Edit.blade.php", they edit their credentials and press update. Everything till here, is working for me. But when they press Update, it just takes them back to the main page but doesn't update anything. No changes are saved. Following is my code:
Display.blade.php (Main Page):
<div id="wrapper">
<div id="content">
<div id="card">
<div id="front">
<div id="top-pic"></div>
<div id="avatar"><span style="position: absolute;padding-top: 17px;margin-left: 34px;color: #fff;font-size: 64px;" class="h5">
{{ Auth::user()->name[0] }}
</span></div>
<div id="info-box">
<div class="info">
<h1>{{ Auth::user()->name }}</h1>
<h2>{{ Auth::user()->message }}</h2>
</div>
</div>
<div id="social-bar">
<a href="{{ Auth::user()->facebook }}" target="_blank">
<i class="fa fa-facebook"></i>
</a>
<a href="{{ Auth::user()->twitter }}" target="_blank">
<i class="fa fa-twitter"></i>
</a>
{{ link_to_route('display.edit','',[Auth::user()->id],['class'=>'fa fa-edit']) }}
</div>
</div>
Edit.blade.php:
{!! Form::model(Auth::user(),array('route'=>['display.update',Auth::user()->id],'method'=>'PUT')) !!}
<div class="form-group">
{!! Form::text('name',null,['class'=>'form-control','placeholder'=>'Name']) !!}
</div>
<div class="form-group">
{!! Form::email('email',null,['class'=>'form-control','placeholder'=>'Email']) !!}
</div>
<div class="form-group form-row">
<div class="col-5">
{!! Form::select('gender', ['Male' => 'Male', 'Female' => 'Female'], null, ['class'=>'form-control','placeholder'=>'Choose Gender']); !!}
</div>
<div class="col">
{!! Form::text('facebook',null,['class'=>'form-control','placeholder'=>'Facebook ID']) !!}
</div>
<div class="col">
{!! Form::text('twitter',null,['class'=>'form-control','placeholder'=>'Twitter Handle']) !!}
</div>
</div>
<div class="form-group">
{!! Form::textarea('message',null,['class'=>'form-control','placeholder'=>'Talk about yourself']) !!}
</div>
<div class="form-group">
{!! Form::button('Update Profile',['type'=>'submit','class'=>'btn btn-danger col-lg-12']) !!}
</div>
{!! Form::close() !!}
ProfileController (Resource Controller):
class ProfileController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$customusers = DB::table('customusers')->get();
return view ('display',compact('customusers'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(CustomUser $customuser)
{
$customuser = DB::table('customusers')->get();
return view ('edit',compact('customuser'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, CustomUser $customuser)
{
$customuser->update($request->all());
return redirect()->route('display.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
Web.php (Routes):
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::resource('/display','ProfileController');
So when I press update, it just redirects me back to display.blade.php (as it should) but none of the changes are saved. Let me know if you need to look at any more of my code, thanks in advance.
Upvotes: 0
Views: 2239
Reputation: 48
Please dd($request->all());
for us when you hit the update method of your controller.
You can also do this for the $customUser at the same time to make sure it's the right model.
Post the results for me in reply please.
I'd like to make sure that the keys match your DB's table column headers. I also have a feeling that $request->all() is going to include a _token key and that could be causing issues with the update. In such a case, you'll need to remove the _token from the array and the simplest way is to create a new variable as an array and fill the new array with the proper table column names as the keys and their values as their associated input values from the request.
I typically just use the $request and pass in the model ID with the request then i'll do something like this:
$data =[];
$data['twitter'] = $request->get('twitter');
$customuser = Customuser::find($request->customeruserID);
$customeruser->update($data);
Upvotes: 2