How to update database fields using controller Laravel - No error message - database

I am going round in circles trying to update data to the database, I finally have got rid of all the errors so now its correctly redirects with no error yet it isnt updating on the site or on the database.
Any help would be greatly appreciated, thanks!
web.php
Route::put('/my-saved-routes/{myroute_id}', 'MyroutesController#update');
MyroutesController.php
public function update(Request $request, $id)
{
$Myroutes = Myroutes::find($id);
$Myroutes->start = $request->input('start');
$Myroutes->end = $request->input('end');
$Myroutes->waypoints = $request->input('waypoints');
$Myroutes->save();
return redirect('/my-saved-routes');
}
show.blade.php
<div class="col-md-4">
<h2>Route {{ $myroute->myroute_id }}</h2>
<form method="put" action="/my-saved-routes">
{{ method_field('put') }}
{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT" />
<div class="form-group">
<label>Start Point</label>
<input type="text" id="start" name="start" class="form-control" value="{{ $myroute->start }}" required/>
</div>
<div class="form-group">
<label>End Point</label>
<input type="text" id="end" name="end" class="form-control" value="{{ $myroute->end }}" required/>
</div>
<div>
<label for="mode">Mode of Travel</label>
<select id="mode" class="form-control" onchange="calculateAndDisplayRoute();">
<option value="DRIVING" name="driving">Driving</option>
<option value="WALKING" name="walking">Walking</option>
<option value="BICYCLING" name="cycling">Cycling</option>
<option value="TRANSIT" name="public-transport">Public Transport</option>
</select>
</div>
<p>Note: Public Transport is only available for start and end points.</p>
<div id="dynamicInput" class="form-group">
<label>Additional Destinations</label>
<input type="text" name="waypoints" class="form-control" autocomplete="on" value="{{ $myroute->waypoints }}">
</div>
<input type="button" class="btn btn-secondary" value="+" onClick="addInput('dynamicInput');" style="padding:0 10px;">
</br></br>
<input type="button" id="calc-route" style="color:#2b2b2b" class="btn btn-light" value="Calculate Route"/>
<input type="submit" id="update-route" class="btn btn-success" value="Update"/>
<input type="button" class="btn btn-danger" value="Delete"/>
</form> <!-- end of form -->
Myroutes.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
class Myroutes extends Model
{
protected $fillable = [
'myroute_id',
'user_id',
'start',
'end',
'waypoints'
];
protected $primaryKey = 'myroute_id';
}

You can do like below
Myroutes::where('id', $id)
->update(['start' => $request->input('start'),
'end'=>$request->input('end'),
'waypoint'=>$request->input('waypoints')]
);

try to
<form method="put" action="/my-saved-routes">
update
<form method="post" action="/my-saved-routes/{{ $myroute->myroute_id }}">

Try the following:
<form class="form" method="post" action="/my-saved-routes/{{ $$myroute->myroute_id) }}">
{{ method_field('patch') }}
{{ csrf_field() }}

Related

Can't store data using checkbox to database using codeigniter

I try to store some data using checkbox into my database, but it can't stored. Here is my code:
I try to store some data using checkbox into my database, but it can't stored. Here is my code:
I try to store some data using checkbox into my database, but it can't stored. Here is my code:
View:
<form method="post" action="<?php echo base_url().'igd/igd/input_airborne'?>">
<?php echo $this->session->flashdata('message_airborne');?>
<input type="text" class="form-control" name="KUNJUNGAN" value="<?php echo $igd->NOMOR ?>" readonly>
<input type="datetime-local" class="form-control" name="TANGGAL" value="<?php echo date("Y-m-d H:i:s") ?>">
<input type="text" class="form-control" name="OLEH" value="<?php echo $session_user->nip?>" readonly>
<input type="text" class="form-control" name="STATUS" value="1" readonly>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" name="DESKRIPSI" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
TBC AKtif
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" name="DESKRIPSI" id="defaultCheck2">
<label class="form-check-label" for="defaultCheck2">
Campak
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" name="DESKRIPSI" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
MDR TB
</label>
</div>
<button class="btn btn-sm btn-success mt-1 mb-1"> Masukkan</button>
</form>
Controller:
public function input_airborne(){
$DESKRIPSI = $this->input->post('DESKRIPSI');
$TANGGAL = $this->input->post('TANGGAL');
$OLEH = $this->input->post('OLEH');
$STATUS = $this->input->post('STATUS');
$KUNJUNGAN = $this->input->post('KUNJUNGAN');
$data = array(
'DESKRIPSI' => $DESKRIPSI,
'TANGGAL' => $TANGGAL,
'OLEH' => $OLEH,
'STATUS' => $STATUS,
'KUNJUNGAN' => $KUNJUNGAN
);
$this->M_Igd->input_airborne($data, 'igd_inim_airborne');
$this->session->set_flashdata('message_airborne','<div class="alert alert-success alert-dismissible mt-1 fade show" role="alert">
Airborne berhasil dimasukkan
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>');
redirect($_SERVER['HTTP_REFERER']);
}
the DESKRIPSI is field to storing my checkbox entry
it's been solved, i just change
$DESKRIPSI = $this->input->post('DESKRIPSI');
to
$DESKRIPSI = implode(', ', $_POST['DESKRIPSI']);
and don't forget to change name to
name="DESKRIPSI[]";
because data will be stored as array
You should specify:-
<input type="checkbox" name="Days[]" value="Daily">Daily<br>
Add [] to all names Days and work at php with this like as array.
After it, you can INSERT values at different columns at db, or use implode and save values into one column.
<html>
<body>
<form method="post" action="chk123.php">
Flights on: <br/>
<input type="checkbox" name="Days[]" value="Daily">Daily<br>
<input type="checkbox" name="Days[]" value="Sunday">Sunday<br>
<input type="checkbox" name="Days[]" value="Monday">Monday<br>
<input type="checkbox" name="Days[]" value="Tuesday">Tuesday <br>
<input type="checkbox" name="Days[]" value="Wednesday">Wednesday<br>
<input type="checkbox" name="Days[]" value="Thursday">Thursday <br>
<input type="checkbox" name="Days[]" value="Friday">Friday<br>
<input type="checkbox" name="Days[]" value="Saturday">Saturday <br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
At PHP Server Side Script:-
$checkBox = implode(',', $_POST['Days']);

How to attach to User create multiple checkboxes during laravel authentication

Good afternoon everyone!
In my project I have to create a new user which will be a restaurant directly in the user's registration.
Once registered, I gave the possibility to associate more types of restaurant to the logged in user.
I made already migrations and relations everything is working.
This is what i wrote to permit the logged user(restaurant) to add more typologies.
public function typologyAdd() {
$typologies = Typology::all();
return view('pages.typology-add',compact('typologies'));
}
public function typologyStore(Request $request) {
$data = $request -> all();
$user = Auth::user();
$typologies = Typology::findOrFail($data['typologies']);
$user -> typologies() -> sync($typologies);
return redirect() -> route('home');
}
So this is working only once i register the user.
i want to put this logic when i'm in registration page.
I was trying to modify the RegisterController but i don't know how to do that:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'indirizzo' => ['required' ,'string','max:255'],
'piva' => ['required','string','min:11','max:11','digits:11','unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'indirizzo' => $data['indirizzo'],
'piva' => $data['piva'],
'password' => Hash::make($data['password']),
]);
}
and to put checkboxes inside the Register.blade.php
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}">
#csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="new-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<div class="form-group row">
<label for="indirizzo" class="col-md-4 col-form-label text-md-right">{{ __('indirizzo') }}</label>
<div class="col-md-6">
<input id="indirizzo" type="text" class="form-control" name="indirizzo" value="{{ old('indirizzo') }}" maxlength="255" required>
#error('indirizzo')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="piva" minlenght="11" maxlength="11" class="col-md-4 col-form-label text-md-right">{{ __('piva') }}</label>
<div class="col-md-6">
<input id="piva" type="text" class="form-control #error('piva') is-invalid #enderror" name="piva" value="{{ old('piva') }}" required>
#error('piva')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
I hope someone will give me some ideas on how to do it, thanks to those who will read and answer
In the RegisterController.php you can get the newly created user by doing the below.
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'indirizzo' => $data['indirizzo'],
'piva' => $data['piva'],
'password' => Hash::make($data['password']),
]);
// Do actions on $user and finally return $user
return $user;
}
Add new validations rules if you need any & also if you are doing relation data entry i would suggest to do a SQL transaction based insert. Commit data if no SQL error is thrown. Else rollback the changes to prevent issues.

How To Solve This Update Error In Laravel

I'm creating a web site. And I have created a registration page. I want to update my details.
But, It gives me this error and I have also uploaded a picture of errors below. -
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message
** Error Picture
How can I Fix this ??
Here is my AdminPanel.blade.php
<table class="table table-bordered">
<tr>
<td> Name </td>
<td> Email </td>
<td> Images </td>
</tr>
#foreach($data as $value )
<tr>
<td> {{ $value->username }} </td>
<td> {{ $value->email }} </td>
<td> <img src='{{ $value->filemove }}' style='width:100px;height:100px;'> </td>
<td> <input type="submit" name="update" value="Update" class="btn-primary"> </td>
<td> <input type="submit" name="delete" value="Delete" class="btn-danger"> </td>
</tr>
#endforeach
</table>
Here is my AdminUpdate.blade.php
<form action="edit{{ $users[0]->id }}" method="post" enctype="multipart/form-data">
{{ method_field('PUT') }}
{{ csrf_field() }}
<div class="form-group">
<label>Username : *</label>
<input type="text" class="form-control" name="username" value="{{$users[0]->username}}" placeholder="Enter Your Username" required>
</div>
<div class="form-group">
<label>Email : *</label>
<input type="email" class="form-control" name="email" value="{{$users[0]->email}}" placeholder="Enter Your Username" required>
</div>
<div class="form-group">
<label>Password : *</label>
<input type="password" class="form-control" name="password" value="{{$users[0]->password}}" placeholder="Enter Your Password" required>
</div>
<div class="form-group">
<label>Upload Profile Picture :</label>
<input type="file" class="form-control-file" name="file_img" aria-describedby="fileHelp">
<small id="fileHelp" class="form-text text-muted">If U Want , U Can Skip Upload A Profile Picture</small>
</div>
#section('btnName',"Update")
<input type="submit" class="btn btn-primary" onclick="myFunction()" name="submit" value="#yield('btnName')">
</form>
Here is my AdminPanelController.php
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
use App\User;
use Validator;
use Illuminate\Support\Facades\Input;
class AdminPanelController extends Controller
{
public function index()
{
$data = User::all();
//$data = login::orderBy('created_at', 'desc')->get();
return view('AdminPanel', ['data' => $data]);
}
public function adminedit(Request $request, $id)
{
$this->validate($request, [
'email' => 'required'
]);
$users = User::find($request['id']);
$users->username = $request['username'];
$users->email = $request['email'];
$users->update();
return redirect('AdminPanel');
}
}
Here is my Route
Route::put('edit/{id}','AdminPanelController#adminedit');
you should replace your action url
//make sure you have $users[0]->id data here
<form action="{{ action('AdminPanelController#adminedit',['id'=> $users[0]->id]) }}" ....>

Using angular submit button causes redirect instead of calling function in SharePoint

I have a form within my angular app (within SharePoint) that uses routing via hashbang, but when I click on a button in my form, it redirects to the root (like it can't resolve the URL so it uses the otherwise setting in my config), instead of executing the function.
Here is the HTML (my controller is defined in the routing):
<form name="newItem" class="form-horizontal" data-ng-submit="createItem()">
<fieldset>
<div class="form-group">
<label class="col-lg-2 control-label" for="itemtype">Type *</label>
<div class="col-lg-10">
<select class="form-control" id="itemtype" data-ng-model="selectedType"
data-ng-options="opt as opt.label for opt in types" required>
<option style="display:none" value="">Select a type</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label" for="title">Title *</label>
<div class="col-lg-10">
<input class="form-control" name="title" id="title" type="text" data-ng-model="itemtitle" placeholder="Add your title (Limited to 70 characters)" data-ng-maxlength="70" required>
({{70 - newItem.title.$viewValue.length}} Characters Remaining)
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label" for="body">Body *</label>
<div class="col-lg-10">
<textarea class="form-control" name="body" id="body" data-ng-model="itembody" rows="4" placeholder="Add your body (Limited to 500 characters)" data-ng-maxlength="500" required> </textarea>
Your summary will be displayed as follows ({{500 - newItem.body.$viewValue.length}} Characters Remaining):<br /> {{itembody}}
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button class="btn btn-default" data-ng-click="cancel()">Cancel</button>
<button class="btn btn-primary" data-ng-click="newItem">Submit</button>
</div>
</div>
</fieldset>
</form>
Here is my controller:
appControllers.controller('appItemPostCtrl', ['$scope', '$location', 'appItems', 'appTypes', function ($scope, $location, appItems, appTypes) {
var itemEntry = new appItems;
console.log(itemEntry);
$scope.types = [];
appTypes.query(function (typedata) {
var itemTypes = typedata.value;
// Foreach type, push values into types array
angular.forEach(itemTypes, function (typevalue, typekey) {
$scope.types.push({
label: typevalue.Title,
value: typevalue.Title,
});
})
});
$scope.createItem = function () {
itemEntry.Title = $scope.itemtitle;
itemEntry.$save();
}
$scope.cancel = function () {
}
}]);
UPDATE: It appears that this is related to SharePoint (My Angular Form is in SharePoint), as even setting the button type to submit as follows triggers the refresh instead of running the function. SharePoint is wrapping everything in a form since it inherits from the Master page of the Web, so when I added two "Angular Forms" to the page, the first angular form was closing the tag on the SharePoint form so the second was able to work. Does anyone have a stable workaround (beyond creating a custom masterpage). Image as follows:
I solved it by closing the tag of SharePoint instead of creating a custom masterpage. Ex:
<!-- Close the default form tag put in place by SharePoint instead of creating a custom Masterpage without this element that requires increased permissions and complexity to deploy. Without this tag closed, the form below will not render properly -->
</form>
<div>
<form id="newItemForm" class="form-horizontal" data-ng-submit="createItem()">
<div class="form-group">
<label class="col-lg-2 control-label" for="itemtype">Type *</label>
<div class="col-lg-10">
<select class="form-control" id="itemtype" data-ng-model="selectedType"
data-ng-options="opt as opt.label for opt in types" required>
<option style="display:none" value="">Select a type</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label" for="title">Title *</label>
<div class="col-lg-10">
<input class="form-control" name="title" id="title" type="text" data-ng-model="itemtitle" placeholder="Add your title (Limited to 70 characters)" data-ng-maxlength="70" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label" for="body">Body *</label>
<div class="col-lg-10">
<textarea class="form-control" name="body" id="body" data-ng-model="itembody" rows="4" placeholder="Add your body (Limited to 500 characters)" data-ng-maxlength="500" required> </textarea>
</div>
</div>
<div class="col-lg-10 col-lg-offset-2">
<!--<button type="button" class="btn btn-default" data-ng-click="cancel()">Cancel</button>-->
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
Add type='button' to the buttons. I had this same problem before and assumed it was an angular bug of some kind.
Do both buttons exhibit this behavior or just the Submit button?
The submit button calls newItem from ng-click, but the name of the function in the js is actually createItem.

Angular-UI Bootstrap's modal won't use bootstrap theme form

I'm having a style problem with my modal.
Here's the code of my modal :
<script type="text/ng-template" id="contact">
<div class="modal-header">
<i class="fa fa-paper-plane"></i> Contact
<a ng-click="closeModal()" style="float: right;"><i class="fa fa-times text-danger"></i></a>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form">
<input class="col-md-6" type="text" ng-model="contact.name" placeholder="{{ data.contact.name }}" required />
<input class="col-md-6" type="email" ng-model="contact.mail" placeholder="{{ data.contact.mail }}" required />
<input type="text" ng-model="contact.subject" placeholder="{{ data.contact.subject }}" required />
<textarea ng-model="contact.message" rows="5" placeholder="{{ data.contact.desc }}" required></textarea>
<form>
</div>
<div class="modal-footer">
The Footer Goes There
</div>
</script>
What am I doing wrong ?
EDIT :
I'm using Bootstrap 3.1.1 & UI-Bootstrap 0.11.0,
Add class="form-control" to your inputs.
<div class="modal-body">
<form class="form-horizontal" role="form">
<input class="form-control col-md-6" type="text" ng-model="contact.name" placeholder="{{ data.contact.name }}" required />
<input class="form-control col-md-6" type="email" ng-model="contact.mail" placeholder="{{ data.contact.mail }}" required />
<input class="form-control" type="text" ng-model="contact.subject" placeholder="{{ data.contact.subject }}" required />
<textarea class="form-control" ng-model=" contact.message" rows="5" placeholder="{{ data.contact.desc }}" required></textarea>
</form>
</div>

Resources