How to pass value from a button to a controller method in laravel 5 - laravel

I working on a simple laravel 5.2 application. I want to pass a value $id from the view when a button is clicked to the controller method destroy(). This is what i have tried:
This is my route
Route::get('delete/{id}', array('as' => 'delete', 'uses' => 'ContactsController#destroy'));
... and this is my button in a view:
Delete
But this code didn't work. Thanks for any help.

According to the documentation you should be able to do this:
{{ action('ContactsController#destroy', ['id' => $contacts->id]) }}

Your route is expecting an id so you don't need to pass an identifier. The below code should work for you.
Make sure you're using the correct brackets as by default in Laravel 5.2 you'll need to use {!! !!} instead of {{ }}.
Delete

Since it is a GET request, you can simply have you own href instead of using blade. I just find this more readable:
Delete

Related

How to pass parameters in named route in Laravel

I want to pass the parameters in the url in my blade template, the url is:
href="/finance/invoices/download/{{ $invoice->file_path }}
and in web.php the route is defined as:
Route::get('invoices/download/{year}/{month}/{file}','InvoiceController#download')->name('download');
and the file is stored in the database as :
2018/07/invoiceberry_invoice_template_1.docx
How should I do that?
For add to previous answer, you make do this:
route('download',['year' => $year, 'month' => $month, 'file' => $file]);
Its simple you can do
{{ route('download', 1) }}
If you want pass more than one parameters you should pass as an array like this:
{{ route('download', ['year'=>1987]) }}
{{ route('download', ['year'=>1987, 'month'=>june, 'day'=>11]) }}
You can simply pass the parameters to a route using route() helper.
Which takes the second parameters as a array.
route('download',[$year, $month, $file]);
Hope this helps

Using Named URL in blade template

In Django, I can do this:
Account Link
which would give me domain/account/login where I have that URL named in my urls.py
url(r'^account/login/$', views.Login.as_view(), name='account_login'),
I want to do something similar in Laravel 5.2
I currently have something like this:
Route::get('/survey/new', ['as' => 'new.survey', 'uses' => 'SurveyController#new_survey']);
How do I use in my template, plus passing in parameters?
I came across this: https://laravel.com/docs/5.1/helpers, but it was just a piece of a white page without relevant content of how to actually use it.
You can use the route() helper, documented here.
My Link
If you include laravelcollective/html you could use link_to_route(). This was originally part of the core but removed in Laravel 5. It's explained here
{!! link_to_route('new.survey', 'My Link') !!}
The Laravel Collective have documented the aforementioned helper here. The function prototype is as follows
link_to_route($routeName, $title = null, $parameters = [], $attributes = []);
If for example you wanted to use parameters, it accepts an array of key value pairs which correspond to the named segments in your route URI.
For example, if you had a route
Route::get('surveys/{id}', 'SurveyController#details')->name('detail.survey');
You can generate a link to this route using the following in the parameters.
['id' => $id]
A full example, echoing markup containing an anchor to the named route.
{!! link_to_route('new.survey', 'My Link', ['id' => $id]) !!}

laravel 5.1 : how can i use my route in blade?

I want to link to my route in blade. How can i do this?
<a href="roue" > go first</a>
if you define Route name you can use that in your blade :
define Route Name :
Route::get('/admin/transfer/forms-list', [
'as' => 'transfer.formsList',
'uses' => 'Website\TransferController#IndexTransferForms'
]);
now you can use that in your blade like this :
<a href="{{URL::route('transfer.formsList')}}" type="submit">
go first</a>
Of course if you use form collective you can use this :
{!! link_to_route('route.name', 'go first') !!}
There are few ways to use routes in a view.
The simplest one is using route() helper:
go first
If you're using Laravel Collective HTML & Forms, it will create full link, not only path to the route:
{!! link_to_route('route.name', 'go first') !!}
Or:
{!! HTML::linkRoute('mainpage', 'hey') !!}
Using URL facade. Wouldn't recommend, because you're getting redundant code:
go first

Store method not working using resource route

I am having trouble figuring out why my data is not being posted and stored in my database. I have used the resource routes for another form and it works fine, but here for some reason it won't work. Clicking submit just seems to refresh the page, no errors to work from!
So I have a form which gets the workout routines from a database, and on submission I want this to create a new Workout "session" in my database table (called "Workouts"). The form is this:
{{ Form::open(array('url' => '/')) }}
<div class="form-group">
{{ Form::text('workout_name', Input::old('workout_name'), array('class' => 'form-control', 'placeholder' => 'Session Name')) }}
</div>
<div class="form-group">
{{ Form::select('routines', $routine_names, null, array('class' => 'form-control')) }}
</div>
{{ Form::submit('Select Routine', array('class' => 'btn btn-success pull-right')) }}
{{ Form::close() }}
In my HomeController I have this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Routine;
use App\Workout;
class HomeController extends Controller
{
public function index()
{
$routines = Routine::all();
$routine_names = Routine::lists('routine_name');
return view('workout')->with(array('routines'=>$routines, 'routine_names'=>$routine_names));
}
public function store()
{
$workout = new Workout;
$workout->workout_name = Input::get('workout_name');
$workout->save();
}
}
I have a model created for the Workout, and the route for this page is the following:
Route::resource('/', 'HomeController');
I can't figure out where I'm going wrong. The index method in my controller is working, as it is returning the correct view with the data I need. The form also looks OK I think, as I'm posting to the same page, but submitting doesn't seem to carry out the code I have in the store method of the HomeController.
Any help would be appreciated!
Thanks :)
Change your route declaration from:
Route::resource('/', 'HomeController');
To something like this:
Route::resource('/workout', 'WorkoutController');
If you are using the resources controller creator command of php artisan then all the specific routes are created for you. To see all listed routes you can type , php artisan routes. This will show you RESTFUL routes even for your POST method .
And also even you did not created the resources controller and did made the routes with manual way then you can create ,
Route::POST('/workout' , SomeController#post);
I am trying to say , you have to use the different POST method for the form submission .
Hope this will solve your problem . Thanks.

How call route resource in Laravel

I have problem. My route definition contains:
route::resource('admin/settings/basic','admin\settings\BasicController');
but I don't know how can I call the edit action from basiccontroller in my a href link.
href='{{ link_to_route('admin/settings/basic/edit') }}'
Please give me some advice.
Given a route like the following:
app/Http/routes.php
Route::resource('profile', 'ProfileController');
Your controller could look something like this:
app/Http/Controllers/ProfileController
public function edit($id)
{
$profile = Profile::all(); // Grab some data
return view('profile.edit', [$profile]); // Pass some data to the Edit view
}
In the view, you might have a form for editing like so:
resources/views/profile/edit.blade.php
<?= Form::model($profile, ['route' => ['profile.update', $profile->id], 'method' => 'PUT', 'class' => 'form-horizontal']) ?>
That form routes to ProfileController#update
For other routes, such as an index, it is handled all for you. You just have to make sure you return the correct view in your ProfileController#index, and hitting the route for /profile will be passed through that method
You can always refer to the documentation as well - RESTful Resource Controllers

Resources