I have a button (anchor tag) in a VueJs component, when clicked, it routes to another page, loading a laravel blade view.
Inside vueJs component
Checkout
This route returns a laravel blade file.
Route::get('/checkout', function(){
return view('checkout');
});
Now I have a data property in my vueJs component which I would need to use in checkout blade view. How do I pass this data from vueJs component to Laravel blade view.
NB: I'm familiar with passing data from laravel to VueJs but not the reverse.
first of all the way you are trying, anchor tag is not a good way. and I donw know How Many data you need to send so I can suggest simple and easy way which is params.
suppose you need to send data id, name, address.
<a :href="'/checkout?id='+id+'&name='+name+'&address='+address">Checkout</a>
and in laravel
Route::get('/checkout', function(Request $request){
$data=$request->all()
return view('checkout')with('data',$data);
});
Related
There is an existing Livewire component and its corresponding blade file in the code structure.I want to create a new Laravel Controller and blade file.The blade files include variables that are defined in the Livewire component so when I return the newly created blade file from the new Laravel controller it shows undefined variable error.Is there any way to pass the variables defined in Livewire component in the newly created blade file?Also is there any alternative to this approach?
This is the blade file and the variable.
#if($variable)
#endif
This variable is defined the livewire component.
public $variable=false;
And the newly created controller is returning the above mentioned blade file.
Well I can't give you any definite answer because no code was posted and I don't know what your aim is
But if you really do need to pass variables to the new blade file, you can look into livewire browser events
https://laravel-livewire.com/docs/2.x/events
You can dispatch a browser event in the child component and listen for it in the parent
I once did this in a project of mine
$this->dispatchBrowserEvent('nationality-updated',['nationality' => $this->nationality]);
$this->dispatchBrowserEvent('state-updated',['state' => $this->state]);
And I listened to it in a parent component like this
<script>
window.addEventListener('nationality-updated',event => {
#this.set('state.nationality', event.detail.nationality)
})
window.addEventListener('state-updated',event => {
#this.set('state.state', event.detail.state)
})
</script>
Note that state.nationality is a livewire variable I accessed from Javascript and I'm not quite sure if that is optimal
But if you must you can follow the above
i am trying to delete some livewire blade and component with
php artisan livewire:delete showcase
and then in the routing(web.php) i connect it with the one in the controller (ShowcaseController), but when i load the page, its still asking the blade from livewire (Showcase.blade.php) instead the blade that i made outside livewire folder (the one that has been deleted).
does anyone know how to solve this or remove livewire blade and component that has been made safely? im using laravel 8 and livewire 2
thank you
showcase routing code
//------------------Controller Showcase------------------//
use App\Http\Controllers\ShowcaseController;
Route::get('/showcase', [ShowcaseController::class, 'index'])->name("showcase");
showcase controller code
public function index()
{
$products = Product::all();
return view('Showcase.showcase', [
'products' => $products,
]);
}
all done guys i just put the blade.php outside view folder, all done, sorry for my clumsy still learning new thing haha
Currently my users must get the visit form given by Route::get then fill it in to get back a result view given by Route::post. I need to create a shareable link such as /account/search/vrm/{vrm} where {vrm} is the VRM that is usually filled in on the form page. This VRM then needs to redirected to Route::post as post data. This needs to be done by my controller. How can I do this in my controller?
Routes:
// Shows form view
Route::get('/account/search', 'User\AccountController#getSearch')->name('account.search');
// Shows result view
Route::post('/account/search', 'User\AccountController#runSearch');
// Redirect to /account/search as POST
Route::get('/account/search/vrm/{vrm}', function($vrm) { ???????? });
POSTs cannot be redirected.
Your best bet is to have them land on a page that contains a form with <input type="hidden"> fields and some JavaScript that immediately re-submits it to the desired destination.
You can redirect to a controller action or call the controller directly, see the answer here:
In summary, setting the request method in the controller, or calling a controller's action.
Ps: I don't want to repeat the same thing.
For those who comes later:
If you are using blade templating engine for the views, you can add '#csrf' blade directive after the form starting tag to prevent this. This is done by laravel to prevent cross site reference attacks. By adding this directive, you can get around this.
return redirect()->route('YOUR_ROUTE',['PARAM'=>'VARIABLE'])
I wanted to disable employees from a button on my index.blade.php page. Currently, the options of disabling employees (setting the status column in the database to false) is either to have an edit.blade.php view and update the value there, which is pretty standard for any laravel app or to have a new view for example, changestatus.blade.php, with the proper routes offcourse and update the value there. I am using the second implementation and it's working perfectly.
What i wanted to implement is to have a button on the index page which will change the status of the employee without going to a edit.blade.php or changestatus.blade.php page.
What i have tried
I have created new routes and created a button to link to the changestatus function
Routes.php
Route::put('employees/{employee}/changestatus', 'EmployeesController#changestatus')->name('employees.changestatus');
Route::resource('employees', 'EmployeesController');
EmployeeController
public function changestatus($EmployeeID)
{
$employee = Employee::find($EmployeeID);
$employee->status = true;
$employee->Save();
}
On my view i created a button with the following link
{{ URL::route('employees.changestatus', $employee->EmployeeID) }}
When i click that link, i get the MethodNotAllowedHttpException in RouteCollection.php error.
I even tried to change the Route::put to Route::Patch, but it's the same thing.
Is it even possible to achieve what I'm trying to do? If so, how?
When you click on a hyperlink, the web browser submits a GET request. Your route has been defined as being a PUT so that's why you're getting an exception.
You could either change the route to a GET by defining it like this:
Route::get('employees/{employee}/changestatus', 'EmployeesController#changestatus')->name('employees.changestatus');
Which isn't very ReSTful since a GET request should really only be used for returning a resource rather than modifying it.
Or, you could modify the button so that it submits a form like this:
<form method="post" action="{{ route('employees.changestatus', $employee->EmployeeID) }}">
{{ method_field('PUT') }}
<button type="submit">Button Text</button>
</form>
Note that you can't simply set the form method to PUT since this method isn't generally supported by web browsers. Laravel supports method spoofing which you can read all about here:
http://laravel.com/docs/5.1/routing#form-method-spoofing
I have the following test function, that I want to call directly from my URL or by clicking a link from my blade view.
public function callMeDirectlyFromUrl()
{
return "I have been called from URL :)";
}
My Question: Is it possible to call a function directly from button-click or URL link from blade view in Laravel?
Here is the solution:
We assume you have a function callMeDirectlyFromUrl in YourController, here how you can call the function directly from URL, Hyperlink or Button.
Create Route
Route::get('/pagelink', 'YourController#callMeDirectlyFromUrl');
Add link in the view blade php file
Link name/Embedded Button
This has been tested on Laravel 4.2 -> 5.2 so far.
By clicking on this link or call the URL www.somedomain.com/pagelink the function will executed directly.