Call controller function from button inside view? - laravel-5

I want to call a controller function when i click a button but the problem is when i enter the page where the button is the function that im testing runs without clicking the button and i dont know why.
Here is my button.
<button href="{{action('PlantillaImagenesController#testboi')}}" style="width:27%" class="p-2 btn btn-primary btn-lg">Guardar</button>
The function that im testing.
public function testboi(){
echo "Funcion controller";
}
My route.
Route::get('/Imagen', 'PlantillaImagenesController#testboi');

Make this change to your button:
Guardar
your button URL should match with the route name,
this is how you call a route in laravel.
You can also name your route like this:
Route::get('imagen', 'PlantillaImagenesController#testboi')->name('imagen');
And use it like this:
Guardar

Related

Laravel Livewire/AlpineJS: Disable a button while loading data from an API and then enabling it again

This should be really simple, but I don't get it. I want to replicate the functionality of a button that when pressed goes to an API (which could take about a minute to get the data and process it), it gets diabled, and after loading the data it gets enabled.
I'm using Laravel/Livewire/Alpine
So, in my livewire component I have:
public $loading = false;
In my blade file, I have the declaration for the div where the button is:
<div
class="grid grid-cols-3 gap-4"
x-data="{
loading: #entangle('loading')
}"
>
Then the button x-binds the disabled property to the loading value, when the button is clicked, it changes the property of the loading variable, and calls the loader function
<button
type="button"
class="btn btn-sm btn-jbn"
x-on:click="loading = true"
x-bind:disabled="loading"
wire:click="loader"
>
Load API
</button>
And it does what it is supposed to do... the button is grayed, it becomes unusable, the cursor change, etc., it executes the loader function in my livewire component, but it never return to the normal state after loading the API. In my livewiere componente I have:
public function loader() {
// API call and logic goes here, this works
$this->loading = false;
}
So I would imagine that at the end of the API process the entangled variable loading would return the button to its normal state, but it doesn't
What am I missing?
Livewire already has incorporated functionality to handle loading-states. Instead of implementing your own, you can use this.
Get rid of all your current loading-logic, and simply use wire:loading with wire:target on your button.
wire:loading can toggle the disabled attribute directly by doing wire:loading.attr="disabled", and wire:target is to set the target for that loading-state to the method you are calling, so in your case that's wire:target="loader".
This means your button looks like this,
<button
type="button"
class="btn btn-sm btn-jbn"
wire:click="loader"
wire:loading.attr="disabled"
wire:target="loader"
>
Load API
</button>

laravel-livewire return view on button click

I have one button in livewire view
<x-jet-button wire:click.prevent="confirmProcess" class="bg-blue-500 hover:bg-blue-700">
Next
</x-jet-button>
and here in controller my method
public function confirmProcess()
{
return view('livewire.confirm');
}
I also have view in livewire folder with name confirm.blade.php
I want to load different view on button click but when I click on button nothing happens.
how can I load other view also I want to use data from same controller also on other view.
Thanks
You can't return view from your confirmProcess method. What you can do is redirect from confirmProcess method to a named route like:
return redirect()->route('confirm');
Then from this route you can call the component like:
Route::get('/confirm-process', \App\Http\Livewire\Confirm::class)->name('confirm');

laravel backup raising 404 to return a view

In short what is happening is, when I try to access a route, Laravel shows a 404 error, with a short description "Page not Found, No query results for model [App\Models\Product] teste ".
Inside of my model I have the method "teste" which invokes a route.
public function teste($crud = false)
{
return '<a class="btn btn-xs btn-default" href="product/teste" data-toggle="tooltip" title="Just a demo custom button."><i class="fa fa-search"></i> Add Item</a>';
}
button image in the datatable line
Adding a button inside of ProductCrudController
$this->crud->addButton('top', 'bteste', 'model_function', 'teste', 'beginning');
Inside of custom.php I have tried already:
CRUD::resource('product/teste', 'ProductCrudController#teste');
Route::get('product/teste', 'ProductCrudController#teste');
Inside of my Controller I have my method named teste
public function teste(){
return view('vendor/backpack/crud/teste');
}
And finally inside of this path I have my view "which is pretty simple" and only retues a simple hello.
What is the purpose
I need to build a form which allows the user to add "a component" for the product once that the product is always customized. This means, a combination of components makes up a new product.
What I need is: Add a new button in the product line which when clicked gonna redirect the user to add all components which makes up this product. Same idea of an order which contain items.
I could not find by myself the possible options to fit my need.
Is that possible to be done by backpack? If so is there any example to be followed?
Thanks

Trigger Ajax request on settings page

I'm writing a plugin that have some fields defined in backend settings.
One of that field is a partial that contain a button, that trigger an Ajax request thanks to the data-attribute API.
<button type="button" class="btn btn-danger" data-request="onReset">
My button
</button>
Where should I put the "onReset()" function ? I tried to put it in my Settings model, even tried to create a Settings controller (whereas not necessary to work with settings page), but I always get the following error :
Ajax handler 'onReset' was not found
I don't know what to do to be able to trigger that onReset() function, can somebody familiar with Laravel / OctoberCMS could point me the right direction ?
Thanks
Settings are rendered by the \System\Controllers\Settings so whatever you write will be handled by \System\Controllers\Settings Controller Class,
so we need to extend \System\Controllers\Settings and we can add dynamic
method there,
inside your plugin's boot method you need to write this extension code
I am renaming your onReset handler to onPrefixReset in this example to avoid any other conflicts.
public function boot() {
\System\Controllers\Settings::extend(function($model) {
$model->addDynamicMethod('onPrefixReset', function() {
\Flash::success('You did it!');
// OR
//return ['some data data'];
});
});
}
onPrefixReset : we are adding some Prefix here, so accidentally we are not overriding internal original methods.
now inside your _partial
<button type="button" class="btn btn-danger" data-request="onPrefixReset">
My button
</button>
so now this onPrefixReset ajax handler is defined in \System\Controllers\Settings as we did that dynamically ;) it will handle your request.
you can return any data from here and it will be received at back-end settings page.
In HTML forms if you have an onReset event, you can add your call inside there:
function updateForm()
{
$.each($('form').find(":input"), function(){
$.uniform.update($(this));
});
}
<form onReset="updateForm();">
In this link you can you can do your own ordering within a click event:
$(document).ready(function() {
$("input:reset").click(function() { /click event
this.form.reset();
window.alert($("input:text").val());
return false;
});
});

ajax method return view code instead redirect on view in laravel

I have many buttons in single form that's why i used ajax to call different method on different button click event. But while I call method using ajax and return view, it send code of view as response, And i need to redirect on that view my method code is as below.
public function store(Request $request)
{
return view('surat.proceed_sell');
}
can i redirect on other view using ajax? Or any other way to call different methods on different button click event then please let me know.
What you could do is return the url where to redirect as a response in your controller as
return url('the path');
and in your ajax success callback you could redirect to the view as
window.location = data;
The url should be defined in your route file where the view is rendered.
You cannot redirect to a view with Ajax because Ajax expects a response. What you should do is redirect to a page instead, using anchors.
First, define your routes
Route::get('surat/proceed', 'SuratController#proceed');
Then use anchor with a button to go that page. It doesn't matter how many buttons you have in the form, as longs as they are not of type submit, they should not submit the form.
<form method="post" action="">
Go to proceed
<button type="submit" class="btn btn-primary">Submit</button>
</form>
If this does not help you, please, update your question with your form code.

Resources