I want to call an action of a controller in laravel blade, when using router group...
route
$router->group([
'namespace' => 'Admin',
'middleware' => 'auth',
], function () {
resource('admin/adsense', 'AdsenseController');
resource('admin/post', 'PostController');
});
So, I want call an action of adsenseController in the blade template
{!! Form::model($var, ['method' => 'PATCH','route' => ['what should i write to call an action ']]) !!}
Example (without router groupe)
route
Route::resource('subject','SubjectController');
blade template
{!! Form::model($var, ['method' => 'PATCH','route' => ['subject.actionName']]) !!}
thanks
If you want to use action, try to use this:
'action' => 'SubjectController#index'
Instead of this:
'route' => ['someroute']
https://laravelcollective.com/docs/5.0/html#opening-a-form
So , i want call an action of adsenseController in the blade template
Why don't you simply point your form's action directly to the controller? Eg
{!! Form::model($var, ['method' => 'PATCH', 'action' => 'Controller#method']) !!}
If you want to redirect to a custom route, it's this simple
{!! Form::model($var, ['method' => 'PATCH', 'route' => 'route.name']) !!}
Check out the documentation on Laravel.com
Related
I got this error when I access to router:
Symfony \ Component \ HttpKernel \ Exception \
MethodNotAllowedHttpException No message
I tried to find out that but I couldn't fix it.
Router(web.php)
Route::get('/test', function ()
{
return view('subdomains.account.pages.test');
});
Route::post('/testForm', 'FormController#store');
View(subdomains.account.pages.test.blade.php)
{!! Form::open(['action' => [ 'FormController#store' ], 'method' => 'POST']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
Controller
namespace Ares\Http\Controllers;
use Illuminate\Http\Request;
class FormController extends Controller
{
public function store()
{
return "test";
}
}
The problem is that code always use the GET method instead of POST.
How can I fix it?
EDIT: I just found cause this issue is because I don't use Laravel using php artisan serve, and I only have a virtual host in the public server, how I can solve this without using artisan serve?
FIX: (EDITED)
The issue is by forcing the trailing slash at the end of the URL.
Try This:
FormController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormController extends Controller
{
public function store()
{
return "test";
}
}
subdomains/account/pages/test.blade.php
{!! Form::open(['action' => [ 'FormController#store' ], 'method' => 'POST']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
web.php
Route::get('/test', function ()
{
return view('subdomains.account.pages.test');
});
Route::post('/testForm', 'FormController#store');
Output: When you use your route /test the test button will be displayed on the output page
When you click on the test button, it will be redirect to /testForm page and get the output test
You should try this:
{!! Form::open(['url' => 'testform.store', 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'post']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
Updated answer
Route::post('/testForm', 'FormController#store')->name('testform.store');
{!! Form::open(['route' => 'testform.store', 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'post']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
Try to use code below for subdomains.account.pages.test
{!! Form::open(['action' => [ '\Ares\Http\Controllers\FormController#store' ], 'method' => 'POST']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
You should remove closer from your routes,
Route::get('/test', 'FromController#index');
Route::post('/testForm', 'FormController#store');
then run,
php artisan route:clear
Then submit your form
so in my blade files i have these code to do CRUD,
<form method="POST" class="form-horizontal" action="{{ url('tickets/'.$ticket->id) }}"
enctype="multipart/form-data">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="PATCH" />
so i created my api routes. code below. (routes/api.php)
Route::group(['middleware' => ['api']], function () {
Route::resource('/v1/tickets','Api\TicketsController');
});
these are my routes. for web, it's in routes.php, because i got it from a package.
Route::resource($main_route_path, 'App\Http\Controllers\TicketsController', [
'names' => [
'index' => $main_route.'.index',
'store' => $main_route.'.store',
'create' => $main_route.'.create',
'update' => $main_route.'.update',
'show' => $main_route.'.show',
'destroy' => $main_route.'.destroy',
'edit' => $main_route.'.edit',
],
'parameters' => [
$field_name => 'ticket',
],
]);
now, the PROBLEM, is that every time i click the button using the form request, the whole operation goes to the api, any thoughts why is this happening?
All the routes in the api.php are grouped with the prefix, middleware and namespace by default. You don't have to add the api middleware again like you are.
For the web routes you can just use TicketsController since the namespace already points to the Controllers folder.
Can you run php artisan route:list and see what shows up?
Also you can do this to change the name prefix of all resource routes.
Route::resource($main_route_path, 'TicketsController', [
'names' => $main_route,
'parameters' => [
$field_name => 'ticket',
],
]);
I have from:
{{ Form::open(array('url' => 'announcements', 'class' => 'form-inline', 'method' => 'GET')) }}
And checkbox in form:
{{Form::checkbox('only_subscriptions', '1', old('only_subscriptions'))}}
After submit I get empty old():
old('only_subscriptions')
You would use ->withInput() in redirect(), not in view().
I know that to insert a attribute in forms in laravel we do something like below
{{ Form::open(array('url' => 'comment', 'method' => 'post')) }}
But what i want to do is insert a attribute like this and not in commas
<form data-abide>
Because I'm using foundation as a front end framework so need to use its validation.
You can add it similar way you add method :
{{ Form::open(array('url' => 'comment', 'method' => 'post', 'data-abide' => 'formclass', 'onSubmit' => 'return false;')) }}
I am implementing a simple controller for a mini-project of mine. For the simplicity of this question, only two views matter: the create song, and edit song views. Both of these views contain the same form fields, so I created a form partial called _form.
Since the forms have different purposes - despite having the same fields - I pass on to the partial a couple of variables to specify the value of the submit button label, and the cancel button route.
For example:
edit.blade.php:
(...)
{!! Form::model($song, ['route' => ['songs.update', $song->slug], 'method' => 'PATCH']) !!}
#include('songs._form', [
'submitButtonLabel' => 'Update',
'returnRoute' => 'song_path',
'params' => [$song->slug]
])
{!! Form::close() !!}
(...)
create.blade.php:
(...)
{!! Form::open(['route' => 'songs.store']) !!}
#include('songs._form', [
'submitButtonLabel' => 'Save',
'returnRoute' => 'songs_path'
])
{!! Form::close() !!}
(...)
And here is the _form.blade.php partial:
(...)
<div class="form-group">
{!! Form::submit($submitButtonLabel, ['class' => 'btn btn-success']) !!}
{!! link_to_route($returnRoute, 'Cancel', isset($params) ? $params : [], ['class' => 'btn btn-default', 'role' => 'button']) !!}
</div>
Now, my question is (finally):
As you can see, in the Cancel button of my form partial, I am using isset($params) ? $params : [] to default the $params variable to [] when it is not set.
Is there a better way to do this? Here, under Echoing Data After Checking For Existence, Laravel supports this alternative echo: {{ $name or 'Default' }}, but this does not work since I am trying to use it inside a {!! !!} block already...
So, is the ternary operator using the isset() function the best solution for this case? (The one I am currently using)
You can simply pass the variable $params an empty array ([]) in create.blade.php and remove the condition on your partial.
Then you can set the default value on your .blade files
As an alternative you can set a default value on your controller and send it as $params if they are not set (your slug).
Hope it helps