I have a model ,a controller and a form for introducing entries to database.The problem is that refresing the page cause duplicate entries in database.How to resolv it?
loan.controller:
public function store(Request $request)
{ $id = Auth::id();
$loan=New loan;
$loan->cod_user=$id;
$loan->nume =$request->name;
$loan->data=$request->date;
$loan->durata=$request->period;
$loan->valoare_rata_luna=$request->month;
$loan->valoare_totala=$request->amount;
$loan->save();
return view('loans');
}
the form from loans view:
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="blacktext"><h3>My Loans:</h3></div>
</div>
<div class="col-md-4"></div>
<div class="col-md-2"></div>
<div class="col-md-2"><button type="button" class="btn btn-success" data-toggle="modal" data-target="#modal1">
<span class="glyphicon glyphicon-plus-sign"></span> Add Loan</button></div>
<div class="modal fade " id="modal1" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3>Add Loan</h3>
</div>
<div class="modal-body text-right">
<form action="{{ route('loan.store') }}" method="POST">
<p class="al-left">
{{csrf_field()}}
<label for="date">Date:</label>
<input type = "date" name="date" placeholder="Date" id="date">
</p>
...
<p class="al-left">
<label for="amount">Amount:</label>
<input type = "number" name="amount" placeholder="0" id="amount">
</p>
<input type="submit" class="btn btn-sm btn-primary" name="submit" value="Add">
Just redirect to the route (the route which serves the view) instead of the view at the end of your store method:
public function store(Request $request)
{ $id = Auth::id();
$loan=New loan;
$loan->cod_user=$id;
$loan->nume =$request->name;
$loan->data=$request->date;
$loan->durata=$request->period;
$loan->valoare_rata_luna=$request->month;
$loan->valoare_totala=$request->amount;
$loan->save();
return redirect(route(('loans')); //asumming that you named a route as loans
}
Try to use
return redirect()-back();
It will work for sure.
POST still exists upon refresh .. what you can do is have a page separated from store function .. like this ..
ROUTE
Route::get('/loans', 'LoanController#viewLoanForm');
Route::post('/loans', 'LoanController#store');
LoanController
public function viewLoanForm()
{
return view('loan');
}
public function store(Request $request)
{
// process your input
return redirect('/loans');
}
I think you should change your return method like:
public function store(Request $request)
{ $id = Auth::id();
$loan=New loan;
$loan->cod_user=$id;
$loan->nume =$request->name;
$loan->data=$request->date;
$loan->durata=$request->period;
$loan->valoare_rata_luna=$request->month;
$loan->valoare_totala=$request->amount;
$loan->save();
return redirect()->route('loans');
}
Related
I want to setup Edit Modal Form but always failed in update data, when i click update button in edit modal, it always redirect to 404 page, but the url in the browser its appropriate with route register in route file,
Is it need jquery for handle update modal form or not?
here my Controller:
RoleController.php
namespace App\Http\Controllers\Permissions;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;
public function edit(Role $role) {
return view('admin.permissions.roles.edit', [
'role' => $role
] );
}
public function update(Request $role, $id) {
request()->validate([
'name'=>'required',
]);
$role = Role::findOrFail($role->id);
$role->update([
'name' => request('name'),
'guard_name' => request('guard_name')?? 'web',
]);
return back();
My route:
adminroute.php
use App\Http\Controllers\Permissions\RoleController;
use App\Http\Controllers\UserManageController;
use App\Http\Controllers\Permissions\RoleManageController;
use App\Http\Controllers\RoleManageController as ControllersRoleManageController;
use Illuminate\Support\Facades\Route;
use Spatie\Permission\Models\Role;
//USER ROLE
Route::put('user-roles/{role}/update',[RoleController::class, 'update'])->name('roles.update');
I was trying to change the route in form action, but it never success
and my view
admin/permissions/roles/edit.blade.php
<div class="card-body">
#foreach($roles as $role)
<table class="card">
<form action="{{ route('roles.update', $role->id) }}" enctype="multipart/form-data" method="post">
#csrf
#method('patch')
<div class="modal fade" id="editform-{{ $role->id }}" tabindex="-1" role="dialog" aria-labelledby="form"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editrole">Edit Role</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" id="name" class="form-control"
value="{{ old('name') ?? $role->name }} ">
</div>
<div class="form-group">
<label for="guard_name">Guard Name</label>
<input type="text" name="guard_name" id="guard_name"
placeholder='Default to "Web"' class="form-control"
value="{{ old('guard_name') ?? $role->guard_name }}">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</div>
</div>
</form>
</table>
#endforeach
</div>
Route change that i tried :
Route::match
Route::resource
but always failed to update modalform, and redirect to 404 page
I was searching for literature but not solve till now, i would happy if you help my problem. Thanks
I am new to programming especially laravel. I am trying to make a CRUD and have already added example data in prequel (using Docker). I can see the data, but when I´m trying to create new posts with a form I get Code 419 page expired. I know that´s normal and the solution is to add #csrf to the form. But after doing this I get 403 Forbidden. I tried a lot but can´t find a solution to fix it.
I would be really happy if someone could help me fix my problem.
Here is my create.blade.php
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">{{ __('Alle Gerichte') }}</div>
<div class="card-body">
<form action = "/recipe" method="POST">
#csrf
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name">
</div>
<div class="form-group">
<label for="beschreibung">Beschreibung</label>
<textarea class="form-control" id="beschreibung" name="beschreibung" rows="5"></textarea>
</div>
<input class="btn btn-primary mt-4" type="submit" value="absenden">
</form>
<a class="btn btn-primary btn-sm mt-3 float-right" href="/recipe"><i class="fas fa-circle-up"></i>Zurück</a>
</div>
</div>
</div>
</div>
</div>
#endsection
hi is that you have created validation rules
in StoreRecipeRequest
do that
public function authorize()
{
return true;
}
Controller Code will be like this:
public function store(StoreRecipeRequest $request)
{
//dd($request);
$recipe = new Recipe( [
'name' => $request->name,
'beschreibung' => $request->beschreibung,
]);
$recipe->save();
return redirect('/recipe');
}
Also if it's not solved. Then let's try it.
public function store(StoreRecipeRequest $request)
{
$recipe = new Recipe();
$recipe->name = $request->name;
$recipe->beschreibung = $request->beschreibung;
$recipe->save();
return redirect('/recipe');
}
also, can you add in your Recipe Model?
protected $fillable = [
'name',
'beschreibung',
];
I have this problem getting my string to an input type file, I'd try changing the input type to text, and when I return $request it works (just with type text, with file type it returns empty).
I'd put enctype="multipart/form-data" but that still empty value for file input.
web.php
Route::get('/profile', 'miPerfilController#index')->name('profile');
Route::post('/profile/update', 'miPerfilController#updatePhoto')->name('profile.update');
updatePhoto.blade.php
<form class="form-group" method="POST" action="/profile/update" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="modal fade row" id="updatePhoto">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="card-body">
<div class="mb-5 form-group" >
<h3 class="pull-left">Update profile image</h3>
<button type="button" class="close pull-right" data-dismiss="modal">
<span>
×
</span>
</button>
</div>
<label v-for="error in errors" class="text-danger">#{{ error }}</label>
<div class="form-group">
<label for="name">Choose image<span class="help"></span></label>
<br><br>
<input type="file" name="profile_image" id="profile_image"
class="form-control">
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-primary" value="Guardar">
</div>
</div>
</div>
</div>
</div>
</form>
miPerfilController.php
public function updatePhoto( Request $request )
{
return $request;
}
Result
write the form tag like this
<form class="form-group" method="POST" action="{{ route('profile.update') }}" enctype="multipart/form-data">
Try this
public function updatePhoto( Request $request , $id )
{
return $request->all();
}
You should try to get files using $request->file() method.
public function updatePhoto( Request $request , $id ){
if ($request->file('profile_image')) {
print_r($request->file('profile_image'));
} else {
echo 'file not found';
}
}
Thanks.
I have a resource route
Route::resource('climb-excluded','CexcludedController',['only'=>['store','update','destroy']]);
And my code in view to save data
<div class="col-lg-4">
<form class="form" method="POST" action="{{ route('climb-excluded.store') }}">
{{ csrf_field() }}
<div class="card">
<div class="card-head style-primary">
<header>Add item</header>
</div>
<div class="card-body floating-label">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<input type="text" class="form-control" id="name" name="name">
<label for="name">Name</label>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<button type="submit"
class="btn btn-block btn-success ink-reaction">
Add
</button>
</div>
</div>
</div>
</div>
A button to destroy data:
{!! Form::open( array('route'=>array('climb-excluded.destroy', $excluded->id),
'method'=>'DELETE')) !!}
<button type="submit"
class="btn ink-reaction btn-floating-action btn-sm btn-danger "
rel="tooltip"
title="Delete">
<i class="fa fa-trash-o" aria-hidden="true"></i>
</button
{!! Form::close() !!}
Store method form controller:
public function store(Request $request)
{
$this->validate($request,[
'name' => 'required|max:255'
]);
$excluded = new Cexcluded;
$excluded -> name = $request->name;
$excluded->save();
//redirect to
Session::flash('success','New item sucessfully added !');
return back()->withInput(['tabs'=>'second4']);
}
Destroy method form controller:
public function destroy($id)
{
$trekExcluded = Cexcluded::find($id);
$trekExcluded->tours()->detach();
$trekExcluded ->delete();
Session::flash('success','Item sucessfully deleted !');
return back()->withInput(['tabs'=>'second4']);
}
The trouble/bug that I'm facing is I can insert first row into table successfully. But when I go for the second one, the store method is somehow redirected to destroy method and deletes the first inserted row also. While I've clearly declared store method in action attribute of the form.
FYI: Both routes exists in same view/page. Destroy method in col-md-8with foreach loop while store method in col-md-4
Its quite obvious, that your form don't have a unique name or id, so that's why the second method is redirected to destroy method. Do something like this:
cex-store-1
cex-destroy-1
I am trying to save data inserted from a modal. Now i have a view called users where i show the list of users.now if admin clicks on add more users it opens a bootstap modal where i have a field called no of users requested. So whatever value the admin fills that must be save into database. I am confused on how to save this.
Edited...................................................
After reseller logs in he can request more users to admin so i created that modal to request the users required so i want to store the number of users required by that reseller along with his/her key
My Controller is :
public function index ()
{
$usertype=$this->session->userdata('usertype');
if($usertype ==="reseller")
{
$key= $this->session->userdata('key');
$this->db->where("key",$this->session->userdata('key'));
$this->data['users'] = $this->user_m->get();
// Load view
$this->data['subview'] = 'reseller/user/index';
$this->load->view('reseller/_layout_main', $this->data);
}
else
{
$this->load->view('permission');
}
}
My view Is :
Request More Users
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add the number of users you want</h4>
</div>
<div class="modal-body">
<form id="loginForm" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-xs-3 control-label">Number Of Users</label>
<div class="col-xs-5">
<input type="text" class="form-control" name="username" id="spinnerInput" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Password</label>
<div class="col-xs-5">
<input type="password" class="form-control" name="password" />
</div>
</div>
<div class="form-group">
<div class="col-xs-5 col-xs-offset-3">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Your question unclear but will give it ago. On your form you need to have form action if you do not have it then will not work. make sure also url helper is auto loaded.
Docs http://www.codeigniter.com/docs
Also would look into codeigniter form validation http://www.codeigniter.com/user_guide/libraries/form_validation.html
And form helper
http://www.codeigniter.com/user_guide/helpers/form_helper.html
<form action="<?php echo base_url('controller-name/function');?>" method="post" method="post">
You may need to use index.php in base_url();
<form action="<?php echo base_url('index.php/controller-name/function');?>">
View
Request More Users
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add the number of users you want</h4>
</div>
<div class="modal-body">
<form id="loginForm" method="post" class="form-horizontal" action="<?php echo base_url('controller-name/request');?>">
<div class="form-group">
<label class="col-xs-3 control-label">Number Of Users</label>
<div class="col-xs-5">
<input type="text" class="form-control" name="username" id="spinnerInput" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Password</label>
<div class="col-xs-5">
<input type="password" class="form-control" name="password" />
</div>
</div>
<div class="form-group">
<div class="col-xs-5 col-xs-offset-3">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
Model
class User_m extend CI_Model {
function request_user() {
$data = array(
'key' => $this->session->userdata('key'),
'total_user' => $this->get_total(), // create a column in table
'username' => $this->input->post('username')
);
$this->db->insert('table', $data);
}
function request_update_user() {
$data = array(
'total_user' => $this->get_total() // create a column in table
'username' => $this->input->post('username')
);
$this->db->where('key', $this->session->userdata('key'));
$this->db->update('table', $data);
}
function get() {
$this->db->where('key', $this->session->userdata('key'));
$query = $this->db->get('table');
return $query->result_array();
}
function get_total() {
$this->db->where('key', $this->session->userdata('key'));
$query = $this->db->get('table');
return $query->num_rows();
}
}
Controller
class Reseller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('user_m');
}
public function index () {
$usertype = $this->session->userdata('usertype');
if($usertype == "reseller") {
$this->data['users'] = $this->user_m->get();
// Load view
$this->data['subview'] = 'reseller/user/index';
$this->load->view('reseller/_layout_main', $this->data);
} else {
$this->load->view('permission');
}
}
public function request() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('modal_view');
} else {
$this->user_model->request_user();
redirect('your_controller');
}
}
}