Data does not send to controller - php

I have a project of an online forum with Laravel 8, and in this project, I have a "Like" button at Blade like this:
<form action="{{ route('questions.likes', $show->id) }}" method="POST">
#csrf
<button class="btn">
<i class="fas fa-thumbs-up"></i>
</button>
</form>
$show->id is the question id
And here is the route questions.likes:
Route::post('questions/{id}/thumbsUp' , [LikeController::class, 'store'])->name('questions.likes');
And then I called the Controller LikeController with the store method which goes like this:
public function store(Request $request, Question $id)
{
dd('output')
}
But now the problem is it does not send data to this method, I mean when you click the button nothing happens at all!
So what is going wrong here?
If you would like to take a look at all the routes in the web.php file, here it is.

You will have to change store(Request $request, Question $id) to store(Request $request, Question $question). Laravel will automatically resolve the provide $show->id argument to the Question model if you use the following route:
Route::post('questions/{question}/thumbsUp' , [LikeController::class, 'store'])->name('questions.likes');

<button type="submit" value="Submit">Submit</button>
Set a type for your button, check if it works or not

Related

simple input type file not getting the file

Hi im new to laravel 9 and im trying to upload file from view to controller, but somehow the input file didnt pass the file to controller.
im using laravel 9 with boostrap in it.
heres my code :
view
<form enctype="multipart/form-data" action="{{ route('profile.put') }}" method="POST">
#csrf
#method('POST')
<div class="card-body">
<input id="file" name="file" type="file">
<button type="submit" class="btn btn-block btn-primary">{{ __('Save') }}</button>
</div>
</form>
controller
public function put(Request $request){
return $request;
}
i try to see the $request variable but the result is like this
enter image description here
i try the solution like add enctype="multipart/form-data" but still didnt work.
thank you in advance
I think you simply should get the request by its name or using `$request->all()`, you can do these two things:
In your controller you should write your put method like below:
public function put(Request $request){
$file = '';
if($request->file('file'))
{
$file = $request->get('file);
}
return $file;
}
or you can use the below code in your put method
public function put(Request $request){
return $request->all();
}
Note: If you don't send the file as a JSON Response you should return it to view after saving operation;
and also, there is no need to put #method('POST') inside the form;

Having bug with destroy and update method in Laravel

I'm trying to create simple Laravel application to use resource controller, but i'm having some kind of bug in update and destroy method when I pass the object itself to method.
My update and destroy functions are simple, they just take $todoList and either updates or deletes that object. However, they seem not working as expected. I try to dd($todoList) and it returns correct value, but does not update the database.
public function update(TodoList $todoList)
{
$todoList->update(['completed' => 1]);
return redirect()->route('todos.index');
}
public function destroy(TodoList $todoList)
{
$todoList->delete();
return redirect()->route('todos.index');
}
My blade template looks like this:
<form action="{{route('todos.destroy', $todo)}}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-link btn-sm float-end"><i class="fas fa-trash"></i></button>
</form>
<form action="{{route('todos.update', $todo)}}" method="POST">
<span #class([$todo->completed ? 'bg-success':''])>{{$todo->content}}</span>
#csrf
#method('PATCH')
<button type="submit" class="btn btn-link btn-sm float-end"><i class="fas fa-edit"></i></button>
</form>
I have also tried to pass $todo-id to route, but none seem to be working. I have checked documentation and I'm usually working with documentations, but this seems to be interesting bug to me.
My route is
Route::resource('/todos', TodoListController::class);
and finally my Model is like following
class TodoList extends Model
{
use HasFactory;
protected $fillable = ['content'];
protected $guarded = [];
}

The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST. in laravel

i want to do seller can delete the product
<form action="{{ route('product.destroy'}}" method="post">
{{csrf_field()}}
{{method_field('DELETE')}}
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
this is in web.php
Route::get('/index', 'ProductController#index');//seller view all product
Route::get('/create', 'ProductController#create'); //seller create new product
Route::post('','ProductController#store')->name('product.store'); //store in database
Route::get('/edit/{id}','ProductController#edit'); // seller edit post
Route::put('edit/{id}','ProductController#update')->name('product.update'); //seller update
Route::delete('/{id}','ProductController#destroy')->name('product.destroy');//seller delete product
this is in ProductController
public function destroy($id)
{
$product= Product::find($id);
Storage::delete($product->image);
$product->delete();
return back()->withInfo('Product has been deleted');
}
HELP me please
I think you are missing ID do it like
{{ route('product.destroy', $product->id)}}
you can try removing the post method of the form cause you specify another one and depend on which version you are using you can try #method('DELETE')
#method('DELETE')
it just more elegant
Pass the ID in the route in form tags,
{{ route('product.destroy', $product->id)}}
Also
$product= Product::find($id);
Storage::delete($product->image);
$product->delete();
Check if their is a $product, then delete it, otherwise you'll get an error.

Passing variable from button to controller Laravel

I am having a little routing problem in Laravel 5.2. I have a result page which shows detailed information about personnel. I would like a button, which when enabled, generates a PDF page. Passing the variables has been a problem but I am very close now! I will public my code to elaborate.
result page
<form action="generatePDFpage" method="get">
<button type="submit" class="btn btn-default">Generate PDF!</button>
</form>
routes.php
Route::get('/dashboard/result/generatePDFpage', 'resultController#GeneratePDFc');
GeneratePDFc controller
public function GeneratePDFc(){
$id_array_implode = "HALLO";
$pdf= PDF::loadView('GeneratePDF', ["test"=>$id_array_implode])->setPaper('a4', 'landscape');
return $pdf->stream('invoice.pdf');
}
So, on the result page I am using a array ($id_array) to search the database for the matching records. I need to pass this variable onto the GeneratePDFc controller, so that I can pass that again to the loadView function!
Could someone please help me out? :-)
When you're using get method, you can do just this:
<a href="{{ route('route.name', $parameter) }}">
<button type="submit" class="btn btn-default">Generate PDF!</button>
</a>
For other methods you can use something like this (this one is for DELETE method):
<form method="POST" action="{{ route('route.name', $parameter) }}" accept-charset="UTF-8">
<input name="_method" type="hidden" value="DELETE">
{{ csrf_field() }}
<button type="submit" class="btn btn-sm btn-default">Generate PDF!</button>
<input type="hidden" value="someVariable" />
</form>
To get variable, use something like this:
public function generatePDF(Request $request)
{
$someVariable = $request->someVariable;
I don't know Laravel but I think when in your action="" of the form you can put your route with its parameters no ?
I've found it here : https://laravel.com/docs/4.2/html#opening-a-form
And access the variable in your controller using the $request var

Call route from button click laravel

I am using the Laravel framework and the blade templating engine for one of my projects, where I have a route which looks like
Route::get('/problems/{problem-id}/edit', 'AdminController#editProblem');
I have editProblem method in AdminController which returns a view
public function editProblem(Problem $problem) {
return view('admin.problem-edit', compact('problem'));
}
and I have a button on a view which looks like
<button class="btn btn-xs btn-info pull-right">Edit</button>
Now I want to call this route with the $problem->id when the button will be clicked. I need to pass these value on the route.
how can I do that?
In my opnion, you should use url() Laravel method
To call you route with the problem's id you can do:
Edit
I used an anchor tag, but it will be rendered like you button tag because I kept the same style class you have defined.
Why you should use url() method ?
The reason is simple, the url method will get the full url to your controller. If you don't use this, href link will get appended with current url.
For example, supose you button is located inside a given page
yourdomain.com/a-given-page/
when someone click in your button, the result will be:
yourdomain.com/a-given-page/problems/{problem-id}/edit
when you would like to get this:
yourdomain.com/problems/{problem-id}/edit
Some considerations about your editProblem method
Your route has the '$id', so you need to receive this '$id' in your method
public function editProblem($problem_id) {
$problem = \App\Problem::find($problem_id); //If you have your model 'Problem' located in your App folder
return view('admin.problem-edit', compact('problem'));
}
Try This:
<button type="button" onclick="window.location='{{ url("users/index") }}'">Button</button>
Little Suggestion:
When you're defining routes in laravel give it a unique name, it helps you to keep track on each url like this
Route::get('/problems/{problem-id}/edit', 'AdminController#editProblem')->name('showProblemEditPage');
Route::post('/problems/{problem-id}/edit', 'AdminController#updateProblem')->name('updateProblem');
Now you use this route in blade with just name for post and get both
Exmaple of GET route :
<button type="button" onclick="window.location='{{ route("showProblemEditPage",[$problemIdParameter]) }}'">Button</button>
OR
<a href="{{ route("showProblemEditPage",[$problemIdParameter]) }}">Button</button>
OR
<button type="button redirectToUrl" data-redirect-url="{{ route("showProblemEditPage",[$problemIdParameter]) }}">Button</button>
<script>
$(document).on('click','.redirectToUrl',function(){
let getRedirectUrl = $(this).attr('data-redirect-url');
window.location.href= getRedirectUrl;
});
</script>
Example of POST route :
In case if you are submiting form via submit button click
<form action={{ route('updateProblem',[$problemIdParameter]) }}>
.....
<button type="submit">Submit</button>
</form>
In case if you are submiting form via ajax on button click
<form action={{ route('updateProblem',[$problemIdParameter]) }}>
.....
<button type="button" class="submitForm" >Submit</button>
</form>
<script>
$(document).on('click','.submitForm',function(){
let getForm = $(this).parents('form');
let getFormActionUrl = getForm.attr('action');
$.ajax({
url: getFormActionUrl,
.....
.....
.....
.....
.....
});
});
</script>
You will need to create a link to this route:
Edit
If you use named routes, this will be even easier:
Route::get('/problems/{problem-id}/edit', ['as' => 'problems.edit', 'uses' => 'AdminController#editProblem']);
And then you just need to call the route method:
Edit
Was asked for route inquiry.
so that :
<button onclick="window.location='{{ route("some_route_name") }}'"...>
</button>
in web.php
Route::get('/some_route_url', [SomeRouteController::class,'some_func'])->name('some_route_name');
I used the following code and it worked.
<button class="btn border button" id="go-back-btn" onclick="window.location='{!! route('getDashboardScreen') !!}?MilkCompanyID={!!session('MilkCompanyID')!!}'">Go back</button>

Categories