Nishanth  Nishanth - 11 months ago 65
PHP Question

update values using user id in laravel

How can i update the values by using the user id.The controller,layout and route are at the below cases as follows:

Controller.php

public function profileedit() {

$id = \Auth::user()->id;
$user_id = \Auth::user()->user_id;

$teacheredit = DB::table('teachers')
//->join('campaign_type', 'campaign.campaign_id', '=', 'campaign_type.id')
//->select('campaign.*', 'campaign_type.*','campaign.id as campaignid','campaign.status as campaign_status')
->select('teachers.*', 'teachers.id as teacher_id')
->where('teachers.id', '=', $user_id)
->get();

return view('layouts.teacher.profile', array('teacheredit' => $teacheredit));
}

public function profileupdate($ids, Request $request) {
$profile = Teachers::find($ids);
$id = \Auth::user()->id;
$user_id = \Auth::user()->user_id;

$subscriber_id = \Auth::user()->subscriber_id;
$inputteacher = $request->all();

$profileeditdata = array(
'teacherFirstName' => $inputteacher['teacher_firstname'],
'teacherLastName' => $inputteacher['teacher_lastname'],
);
//dd($profileeditdata);

$profile->fill($profileeditdata)->save();
// \Session::flash('success', 'Data Insert Successfully!');
return Redirect('/teacher/profile');
}


In this case edit is working but updation is not working please provide me a help.

profile.php

@extends('layouts.default')
@section('content')
<div id="createStudent" type="view" class="demo-section k-header">
@include('layouts.common.flash-message')
{!! Form::open(array('url' => 'profileupdate/'.$teacheredit[0]->teacher_id,'class'=>'horizontal-form'))!!}

<form id="createTeacherForm" method="post" action="../teacherregistration" >

<ul id="fieldlist" >
<li>
<label style="color:Green;font-size:15px">Profile</label>
</li>
<li>
<table id="lessonPlanTable">
<tr>
<td><label> UserType </label> </td>
<td><label> <?= $teacheredit[0]->teacher_firstname ?> </label></td>
</tr>
<tr>
<td><label> First Name </label> </td>
<td><input type="text" id="teacher_firstname" name="teacher_firstname" value="<?php echo $teacheredit[0]->teacher_firstname ?>" class="k-textbox"/></td>
</tr>
<tr>
<td><label> Last Name </label> </td>
<td><input type="text" id="teacher_lastname" name="teacher_lastname" value="<?php echo $teacheredit[0]->teacher_lastname ?>" class="k-textbox"/></td>
</tr>

<tr>
<td><label> Email </label> </td>
<td><input type="email" id="teacherEmail" name="teacherEmail" value="<?php echo $teacheredit[0]->teacher_email ?>" class="k-textbox"/ readonly></td>
</tr>
</table>
</li>
<li><br>
<button id="updateTeacherProfile" class="k-button k-primary" type="submit">Update</button>&nbsp;&nbsp;
<button id="resetTeacherPassword" class="k-button k-primary" type="button" style="width:20%">Reset Password</button>
</li>
<br><br>

</ul>
</form>

</div>
@stop


Route.php

Route::get('/teacher/profile', 'Teacher\[email protected]');
Route::post('profileupdate/{id}', 'Teacher\[email protected]');


edit is perfectly working without any errors but in updation i make to check the answer using dd() it return the correct answer but after hiding dd() the result will not be updated..

Answer Source

Try to update record with below code:

$profile = Teachers::find($ids);
$profile->teacherFirstName = $inputteacher['teacher_firstname'];
$profile->teacherLastName = $inputteacher['teacher_lastname'];
$profile->save(); 

Note: Removed the fill() because your next method profileedit() already having select for the same records.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download