Laravel: how to get data with post - php

I've created a function in my routes that take some data from a view and send to another view
Route::post('/trans', function(){
$j = Input::get('r');
return view('movs.create')->with($j);
});
this route take the data from this form
<form action="/trans" method="POST">
#csrf
<div class="input-group">
<input type="hidden" class="form-control" name="r" value={{$cooperado->id}}>
<button type="submit" class="btn btn-primary">
<span>+</span>
</button>
</span>
</div>
</form>
but cant set the data in this other form on 'movs.create'
<form method="post" action="{{ route('movs.store') }}">
<div class="form-group">
#csrf
<label for="name">ID COOP:</label>
<input type="number" class="form-control" name="id_coop" readonly/> <-- data must be setted here
</div>
<div class="form-group">
<label for="price">VALOR MOVIMENTACAO:</label>
<input type="number" step=0.01 class="form-control" name="valor"/>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
when i try to set the data in id_coop input, laravel says that the variable doesnt exists

To set data in the create form, you may need to add a value attribute to the id_coop input:
<input type="number" class="form-control" name="id_coop" value="{{ $j }} readonly/>
Also, ->with() needs to be a key (variable name) and value:
Route::post('/trans', function(){
$j = Input::get('r');
return view('movs.create')->with('id_coop', $j);
});
This would mean you use {{ $id_coop }} instead.

with works with key value pair
Route::post('/trans', function(){
$j = Input::get('r');
return view('movs.create')->with('j',$j);
// or return view('movs.create', compact('j')); // it will extract in
//blade as $j
// or return view('movs.create', ['j' => $j]);
});
// you can fetch that data in blade as {{$j}}
<input type="number" class="form-control" name="id_coop" value="{{$j ?? ''}}" readonly/>
Example of with,
return view('greeting')->with('name', 'Victoria'); // name as key and Victorial as value.
{{$j ?? ''}} if data is not set then '' value.

//controller
public function Postdata(Request $request){
$data['j'] = Input::get('r');
return view('movs.create',$data);
}
//route
Route::post('/trans','yourController#Postdata');
//your view
<form method="post" action="{{ url('/store') }}">
<div class="form-group">
#csrf
<label for="name">ID COOP:</label>
<input type="number" class="form-control" name="id_coop" value="{{ $j }}" readonly/> <-- data must be setted here
</div>
<div class="form-group">
<label for="price">VALOR MOVIMENTACAO:</label>
<input type="number" step=0.01 class="form-control" name="valor"/>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
//store route
Route::post('/store','yourController#Savedata');
hope this helps

Related

2 values in checkbox type

I want make function who can switch 2 values - Yes and No in Database. Now i have checkbox type who get me 1 value - "Yes" and when is not checked dont give me value. I want make in controller to switch when I dont get value to switch to "No" value. Now my function work, but change only last result, not this who i want. https://i.imgur.com/os12J8p.png & https://i.imgur.com/PR3K0lT.png. This is my code
<form method="post" action="/adminpanel/hofswitch">
#csrf
<div class="card-body">
#foreach($char as $value)
<div class="card-body">
#if($value->status == "Yes")
<input type="checkbox" name="switch[]" value="{{$value->id}}" checked data-bootstrap-switch data-off-color="danger" data-on-color="success">
<div class="form-group">
<label class="col-form-label" for="inputSuccess"><i class="fas fa-check"></i> Character Class</label>
<input type="text" name="class[]" value="{{$value->class}}" class="form-control is-valid" id="inputSuccess" readonly="true" placeholder="{{$value->class}}">
</div>
#else
<input type="checkbox" name="switch[]" value="{{$value->id}}" data-bootstrap-switch data-off-color="danger" data-on-color="success">
<div class="form-group">
<label class="col-form-label" for="inputError"><i class="far fa-times-circle"></i> Character Class</label>
<input type="text" name="class[]" value="{{$value->class}}" class="form-control is-invalid" id="inputError" readonly="true" placeholder="{{$value->class}}">
</div>
#endif
</div>
#endforeach
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary col-12">Submit</button>
</div>
</form>
and Controller
public function hof_switch(Request $request) {
$count = count(collect($request->get('switch')));
for($i = 0; $i < $count; $i++) {
$switch = $request->switch[$i] ?? null;
if ($switch == true) {
$switch = "Yes";
} else {
$switch = "No";
}
$update = DB::connection('XWEB')->table('XWEB_HOF')
->where('id', $request->switch[$i])
->update(
[
'status' => $switch,
]);
}
return redirect()->back()->withSuccess('You have switch this class successfully!');
}
Fews days ago I've been in similar problem
Put a hidden input with same name, and then try to POST it.
If the checkbox is checked, than the value will be send 1.
And If the checkbox isn't checked, than the value will be send only 0.
<form>
<input type="hidden" name="switch[]" value="0">
<input type="checkbox" name="switch[]" value="1">
</form>

Why my controller cannot get request from view?

I want to insert some data to database, I need id that I can get from my route and quantity. But my quantity keep define as null.
This is my view
<form method="POST" action="{{ url('beli/'.$produk->id) }}" enctype="multipart/form-data" >
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="exampleInputEmail1">Jumlah Beli :</label>
<input type="name" class="form-control" name ="quantity" id="quantity" placeholder="Jumlah">
</div>
<div class="mt-4">
<div class="btn btn-default btn-lg btn-flat">
<i class=" fa-lg mr-2"></i> <a href="{{url('/beli/'.$produk->id)}}">
Beli Produk </a>
</div>
</form>
and this is my Controller
public function tambah(Request $request, $id)
{
$idtoko = Store::select('id')->where('users_id',Auth::user()->id)->first();
$transaksi = new Transaction();
$transaksi->quantity = $request->get('quantity');
$transaksi->stores_id = $idtoko->id;
$transaksi->save();
}
And this is my route
Route::get('/beli/{id}','KeranjangController#tambah');

How to fix edit interface getting only first word of a column in table in Laravel 5.7?

My edit interface edit.blade.php only gets the first word of the name from my database, this is what the index.blade.php looks like
and when i click on the edit icon of the 3rd line it leads me to edit.blade.php which gives me this
"Nom d'établissement" textfield only gets the first word from the database
Everything looks fine in the database:
this is my edit.blade.php form:
<form method="post" action="{{ route('etablissements.update', $etablissement->id) }}">
#method('PATCH')
#csrf
<div class="col-5">
<div class="form-group">
<label for="nom">Nom Etablissement :</label>
<input type="text" class="form-control" name="nom" value={{ $etablissement->nom }} />
</div>
<div class="form-group">
<label for="price">E-Mail :</label>
<input type="text" class="form-control" name="email" value={{ $etablissement->email }} />
</div>
<div class="form-group">
<label for="quantity">Telephone:</label>
<input type="text" class="form-control" name="telephone" value={{ $etablissement->telephone }} />
</div>
</div>
<button type="submit" class="btn btn-primary">Confirmer</button>
</form>
this is edit function in the controller:
public function edit($id)
{
$etablissement = Etablissement::find($id);
return view('etablissements.edit', compact('etablissement'));
}
and this is update function in the controller:
public function update(Request $request, $id)
{
$request->validate([
'nom'=>'required',
'email'=> 'required',
'telephone' => 'required|numeric'
]);
$etablissement = Etablissement::find($id);
$etablissement->nom = $request->get('nom');
$etablissement->email = $request->get('email');
$etablissement->telephone = $request->get('telephone');
$etablissement->save();
return redirect('/etablissements')->with('success', 'Utilisateur édité');
}
Quote the value attribute.
<input type="text" class="form-control" name="nom" value="{{ $etablissement->nom }}" />
Without quotes, the second word in$etablissement->nom is interpreted as another attribute rather than part of the value of the value attribute.
The email and telephone values are showing up correctly because there are no spaces, but you should quote those as well just in case.

I have the following error: "Type error: Too few arguments to function AlbumController::postEdit(), 1 passed and exactly 2 expected"

I have the following problem when trying to edit an "album", hopefully they can help me, I'm a little frustrated haha.
The Form
<form name="editalbum" action="{{ action('AlbumController#postEdit', $album->id) }}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<fieldset>
<h2>Editar <strong>{{$album->name}}</strong></h2>
<br></br>
<div class="form-group">
<label for="name">Nombre del proyecto</label>
<input name="name" type="text" class="form-control" value="{{ $album->name }}" required>
</div>
<div class="form-group">
<label for="description">Descripción del proyecto</label>
<textarea name="description" rows="10" cols="50" type="text" class="form-control" value="{{ $album->description }}" required></textarea>
</div>
<div class="form-group">
<label for="location">Locación:</label>
<input name="location" type="text" class="form-control" value="{{ $album->location }}" required>
</div>
<div class="form-group">
<label for="year">Año:</label>
<input name="year" type="text" class="form-control" value="{{ $album->year }}" required>
</div>
<button type="submit" class="btn btn-primary">Editar</button>
</fieldset>
</form>
So far I think everything is going well because I try to post in the ID of the model.
The function:
public function postEdit(Request $request, $id)
{
$album = Album::find($id);
$album = Album::all();
if(count($album) > 0){
$album->name = Input::get('name');
$album->description = Input::get('description');
$album->year = Input::get('year');
$album->location = Input::get('location');
$album->save();
Alert::success('Successfully Updated', 'Congratulations');
return view('admin.dashboard');
} else {
Alert::error('Facilities not found', 'Error');
return view('galeries');
}
I think you made error in routes.php
It should look like this:
Route::post('albums/update/{id}', ['uses' => 'AlbumController#postEdit']);
One solution will be to remove the DI Request object
public function postEdit($id)
{
//rest of code
}
note: the param has to be passed as a array
action="{{ action('AlbumController#postEdit', ['id' => $album->id]) }}"

laravel 5.3 MethodNotAllowedHttpException in RouteCollection.php line 218:

(SOLVED) Thanks..
I just want to make a new view called "tambah.blade.php" and the controller is "JurnalController.php" with method "tambahJurnal", but it show an error. What's wrong with my route?
Here is my form:
<h1>Tambah Jurnal</h1>
<form method="post" class="tambahJurnal" action="{{ route('tambah') }}" >
{{ csrf_field() }}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
</form>
And here is my method in JurnalController:
public function tambahJurnal(Request $request){
$jurnal = new Jurnals;
$jurnal->no_jurnal = $request->no_jurnal;
$jurnal->tgl_jurnal = $request->tgl_jurnal;
$jurnal->keterangan = $request->keterangan;
$jurnal->save();
}
This is my route:
Route::post('/tambah', 'JurnalController#tambahJurnal');
And it show an error like this:
enter image description here
You are creating a route for the POST method with this line:
Route::post('/tambah', 'JurnalController#tambahJurnal');
But then, you're trying to perform a GET request with your browser on that URL. That's why you're getting that error.
Try adding this line as well:
Route::get('/tambah', 'JurnalController#tambahJurnal');
#1. Add this route in your route file.
Route::get('/tambah', function()
{
return view('tambah');
});
#2. change in tambah.blade.php file
<form method="post" class="tambahJurnal" action="{{ route('tambah') }}" >
to
<form method="post" class="tambahJurnal" action="{{ url('tambah') }}" >
Thanks
You can write this. Hopefully this will solve your problem.
<h1>Tambah Jurnal</h1>
<form method="post" class="tambahJurnal" action="{{ url('tambah') }}" >
{{ csrf_field() }}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
</form>
try in form action ="/tambah"
try with
Route::any('/tambah', 'JurnalController#tambahJurnal');
first then if it works fine you can change to
Route::post('/tambah', 'JurnalController#tambahJurnal');
any will work for get post put ....
Change this line
Route::post('/tambah', 'JurnalController#tambahJurnal');
to
Route::post('tambah', 'JurnalController#tambahJurnal')->name('tambah');
and use blade Form
<h1>Tambah Jurnal</h1>
{!! Form::open(['route' => 'tambah','method' => 'POST','class' => 'tambahJurnal']) !!}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
{!! Form::close() !!}
Advantage of using blade Form is , you don't explicitly need to specify {{ csrf_field() }}, blade injects csrf token itself.
Add route to show view
Route::get('/tambah', 'JurnalController#index');
And add index method to your controller
public function index(){
return view("tambah");
}
Also add /
action="{{ route('/tambah') }}"
Sometimes it happened that you are in /tambah and trying to post url becomes /tambah/tambah

Categories