Why my controller cannot get request from view? - php

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');

Related

Laravel: getting another id from form after submitting// rating functionality for rentals

Route
Route::post('/review/{pId}','RentalController#review');
Controller:
public function review(Request $request,$propId){
$review = new Reviews();
$rpId = rand();
$review->rvid=$rpId;
$review->usid_fk = Auth::user()->uid;
$review->prId_fk = Property::find($propId);
$review->comment = $request->input('comment');
$review->rating = $request->input('rating');
$review->date = Carbon::now();
$review->save();
}
PHP
<form action="/review/{{ $prop->pid}}" method="POST">
{{csrf_field()}}
<div class="modal-body">
<input type="hidden" name="propid" value="{{ $prop->pid }}"/>
<input id="rating" name="rating" class="rating rating-loading" data-show-clear="false" data-min="0" data-max="5" data-step="1" value="0">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">Comment</span>
</div>
<textarea name="comment" class="form-control" aria-label="Comment">
</textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
I am getting "page not found", so I don't know whether should I use "put" or "post" method in route as well as in form .
Your replies would be highly appreciated.
I think the problem is from the action in your form.
change your form to
<form action="{{route('review.post',['id'=>$prop->pid])}}" method="POST">
</form>
and change your route to
Route::post('review/{pId}','RentalController#review')->name('review.post');
Inspect the form from browser and make sure csrf token and action url are well formed.

How to get/input id from another field?

before that, I wanted to make a web app like google classroom. (in this case the Join Classroom section).
The result of my code is:
"Object of class Illuminate \ Database \ Eloquent \ Builder could not
be converted to string"
View:
<form action="{{ route('user.classroom.joinclassroom') }}">
#csrf
<label class="sr-only">Classroom Code</label>
<div class="input-group">
<input method="post" type="text" name="classroom_code" class="form-control" placeholder="eg.XIRPL301 | Max 8 Char/Num" aria-label="Recipient's username" aria-describedby="basic-addon2">
<div class="input-group-append">
<button name="submit" class="btn btn-sm btn-gradient-primary" type="submit">Search</button>
</div>
</div>
</form>
#foreach($classrooms->chunk(4) as $items)
<div class="row">
#foreach($items as $p)
<div class="col-md-4 stretch-card grid-margin">
<div class="card bg-gradient-danger card-img-holder text-white">
<div class="card-body">
<img src="{{ asset('classofus_resource/images/dashboard/circle.svg') }}" class="card-img-absolute" alt="circle-image"/>
<h4 class="font-weight-normal mb-3">{{ $p->name }}
<i class="mdi mdi-account-multiple mdi-24px float-right"></i>
</h4>
<h4 class="mb-5" data-toggle="tooltip" title="{{ $p->description }}">{{ str_limit($p->description, 32) }}</h4>
<form method="post" action="{{ route('user.classroom.joinclassroombutton') }}">
#csrf
<button type="submit" name="submit" class="btn btn-gradient-primary btn-sm">Join</button>
</form>
</div>
</div>
</div>
#endforeach
</div>
#endforeach
Relationship:
Many to Many
Enrollment Controller:
public function searchclassroom(Request $request)
{
$this->cari = $request -> classroom_code;
$classrooms = Classroom::wherein('classroom_code', [$this->cari]) -> get();
return view('pages.user.classroom.joinclassroom',['classrooms' => $classrooms]);
}
/* --------------------------------------- End Of Search Classroom -------------------------------------------- */
/* --------------------------------------- Join Classroom Button ----------------------------------------- */
public function joinclassroombutton(request $request)
{
$enrollment = Enrollment::create([
"classroom_id" => Classroom::select('id')->where('classroom_code', $this->cari),
'user_id' => Auth::user()->id
]);
}
And the point is, how is the syntax for getting classroom id with value with classroom_code? please help me ...
The problem is here, where you getting your classroom ID. You want to get ID, but you've got the Builder instance
"classroom_id" => Classroom::select('id')->where('classroom_code', $this->cari), // Builder
So, you can try:
"classroom_id" => Classroom::where('classroom_code', $this->cari)->first()->id, // just id of your model

Laravel: how to get data with post

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

Modify button doesn't work despite i call the patch method '#method('PATCH')', and i want to show the document but it doesn't appear

the edit doesn't work when i click on edit button it recover the values of the input but do not modify it in the database
Edit.blade.php
#extends('index')
#section('content')
<script language="javascript" type="text/javascript" src={{ URL::to('js/extension.js') }}></script>
<div class="form-group" style="margin-top: 100px;">
<form class="forms-sample" action="/documentSideBar" method="get" enctype="multipart/form-data">
#csrf
#method('PATCH')
<div class="form-group">
<label for="nomDocument">Nom document</label>
<input type="text" class="form-control" id="nomDocument" name="nomDocument"
value="{{$document->nomDocument}}" onChange='getoutput2()'>
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" id="description" name="description"
value="{{$document->description}}">
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<label class="col-sm-3 col-form-label">Type</label>
<div class="col-sm-9">
<select class="form-control">
<option>Documents administratifs</option>
<option>Documents financiƩres</option>
</select>
</div>
</div>
</div>
</div>
<div class="form-group">
<label for="fille">Importer un fichier</label>
<input type="file" onChange='getoutput()' name="file" id="fille" class="form-control">
<input id='extension' type='hidden' name='extension'>
<input id="pathe" type="hidden" name="path">
</div>
<button type="submit" class="btn btn-success mr-2">Modifier</button>
<button class="btn btn-light">Annuler</button>
</form>
</div>
#endsection
documentSideBar.blade.php
<td style="width:300px">
<a href="/download/{{$document->id}}" class="btn btn-success"><i
class="glyphicon glyphicon-download-alt"></i></a>
<a href="/documentSideBar/{{$document->id}}/edit" style="float:right" class="btn btn-primary">
<i class="glyphicon glyphicon-edit"></i>
</a>
<form action="{{ action('DocumentsController#destroy', $document->id)}}" method="POST">
#method('DELETE')
#csrf
<button class="btn btn-danger"><i
class="glyphicon glyphicon-trash"></i></button>
</form>
</td>
this is the edit and update method in the controller
DocumentsController.php
public function edit($id)
{
$document = Document::findOrFail($id);
return view('documents.edit', compact('document'));
}
public function update(Document $document)
{
$document->nomDocument = request('nomDocument');
$document->description = request('description');
$d=request('path');
if(isset($d)){
$document->path ='username'.'/'.request('path');
}
$document->save();
return redirect('/documentSideBar');
}
public function show($id)
{
$document = Document::findOrFail($id);
return view('documentSideBar', compact('document'));
}
also i I want to adjust the buttons , idon't how to make the space between them
the error was in the web.php : here is the right route
Route::resource('documentSideBar','DocumentsController');
Route::get('download/{id}','DocumentsController#download');
Route::get('show/{id}','DocumentsController#show');
Route::post('/update/{id}','DocumentsController#update');
You should change your form method to POST, and change your action to the resource pattern:
<form class="forms-sample" action="{{route('documentSideBar.update', $document->id)}}" method="POST" enctype="multipart/form-data"
#csrf
#method('PATCH')
When you're modifying data in your database you should use POST, not GET.
Take a look here to see more information about the methods.
You also should take a look at the Resource Controller section to understand more about what are you using

SQLSTATE[HY000]: General error: 1364 Field 'reply_text' doesn't have a default value

I have a page that shows a topic, And underneath the topic there are replies. In between these 2, there is a text field where the user can type a reply. The problem is. I get the error in the title when I try to post a reply. I used the same method on a previous project of mine and there it works just fine. How can I solve this?
Here are the files
topic.blade.php
<div class="card">
<div class="card-content">
<span class="card-title">Leave a Reply</span>
<div class="row">
<form method="POST" action="{{ route('createreply') }}">
{{ csrf_field() }}
<input type="hidden" name="user_id" value="{{ Auth::user()->id }}">
<input type="hidden" name="post_id" value="{{ $topic->id }}">
<div class="form-group col s12">
<textarea id="message-body textarea1" class="form-control materialize-textarea" name="reply" placeholder="Type your reply"></textarea>
</div>
<div class="col s12">
<button class="btn right blue-grey darken-4" type="submit">Reply</button>
</div>
</form>
</div>
</div>
</div>
ReplyController.php (Store method)
public function store(Request $request)
{
Reply::create($request->input());
return back();
}
Web.php
route::post('/reply/create', 'ReplyController#store')->name('createreply');
Thank you in advance!
<textarea id="message-body textarea1" class="form-control materialize-textarea" name="reply_text" placeholder="Type your reply"></textarea>
Try this. The name attribute was not reply_text as you have in db

Categories