I can`t update my table. Maybe you can see where is problem.
Edit works fine. It brings value to the fields. If i erase {{ method_field('PUT') }} it saves values normally, but i need to UPDATE
It`s my UPDATE controller
public function update(Request $request, Radar $radar)
{
Radar::update([
'date' => $request->input('date'),
'number' => $request->input('number'),
'distance' => $request->input('distance'),
'time' => $request->input('time'),
]);
return redirect('/radars');
}
Thats how my view looks like:
<form action="{{ url('/radars')}}" method="post" >
{{ method_field('PUT') }}
{{ csrf_field() }}
Routes:
Route::put('radars/{radar}', 'RadarsController#update');
Error:
MethodNotAllowedHttpException
No message
Thank you for help.
You need to specify ID:
{{ url('/radars/') . $radar->id }}
Also, you need to use object and not just a model class. Something like:
public function update(Request $request, Radar $radar)
{
$radar->update($request->all());
return redirect('/radars');
}
Illuminate\Support\Facades\Request
Radar::update(Request::all());
Looking at it, I think it should be like the below:
<form action="{{ url('/radars/' . $radar->id )}}" method="post" >
If you look, you're posting to /radars but your route is radars/{driver}
You need to add where statement to tell which record to update and dont
Radar::where('id',$id)->update([
'date' => $request->input('date'),
'number' => $request->input('number'),
'distance' => $request->input('distance'),
'time' => $request->input('time'),
]);
return redirect('/radars');
If you are using resource route, then in your form, you need to change action from:
url('/radars')
to
route('radars.update', $radar)
and keep the following:
{{ method_field('PUT') }}
Related
I have abstracts table in my database with the Abstract_Status_ID column, I am trying to create a button that changes every Status from '2' to '3'
I have tried to do this:-
My controller
public function publish($A_ID)
{
$abstracts = Project::find($A_ID);
$abstracts->Abstract_Status_ID= 3;
$abstracts->save();
return redirect('/AdvertisedProjects')->with ('success', 'Abstracts Published' );
}
my route
Route::put( '/projects', 'AbstractsController#publish');
Route::post( '/projects', 'AbstractsController#publish');
my view (projects)
Tried with the csrf token and without as eloquent docs says any post/put will be restricted without it.
#if (count ($abstracts)> 0)
#foreach ($abstracts as $abstract)
#if($abstract->Abstract_Status_ID == 2)
{!! Form::open(['action' => ['AbstractsController#publish' , $abstract->A_ID], 'method' => 'post' ]) !!}
{{ csrf_field() }}
{{Form::hidden('_method', 'PUT') }}
{{Form::Submit('Submit Changes',['class' => 'btn btn-primary'])}}
{!! Form::close() !!}
#endif
#endforeach
The error I am getting when clicking the button
(1/1) ErrorException
Missing argument 1 for App\Http\Controllers\AbstractsController::publish()
Also, the code above will show more than one button, any suggestions to make one button change all of them ?
A: If you want to have a button for each ABSTRACTS just change your route to :
Route::put( '/projects/{A_ID}', 'AbstractsController#publish');
B: If you want to have only one button that change all , you can echo their ID in hidden input then send form
so your view would be:
#if (count ($abstracts)> 0)
{!! Form::open(['action' => ['AbstractsController#publish'], 'method' => 'post' ]) !!}
{{ csrf_field() }}
{{Form::hidden('_method', 'PUT') }}
#foreach ($abstracts as $abstract)
#if($abstract->Abstract_Status_ID == 2)
{{Form::hidden('abstract_ids[]', $abstract->A_ID) }}
#endif
#endforeach
{{Form::Submit('Submit Changes',['class' => 'btn btn-primary'])}}
{!! Form::close() !!}
And Your Controller :
public function publish()
{
foreach(request('abstract_ids') as $A_ID){
$abstracts = Project::find($A_ID);
$abstracts->Abstract_Status_ID= 3;
$abstracts->save();
}
return redirect('/AdvertisedProjects')->with ('success', 'Abstracts Published' );
}
You are using form method="POST". I think it should be method="PUT".
The main problem in your code is the definition of your routes, You are passing an argument ($A_ID) to the controllers publish method,
But in your routes, you did never mention that you are passing any argument through your route. So for passing the argument, you can use {$A_ID} in your route URL.
One thing you should always remember is that you should always give your route a name and call it by using its name and route() Laravel helper method.
The benefit of using route name is that if you decide to change the URL-Structure of your route later in the development process, you'll never have to change it everywhere in your application because you did not call it by its URL.
And route name is less error prone
In total, You should define your routes like this
Route::put('projects/{A_ID}/update', 'AbstractsController#publish')->name('projects.update');
Route::post('projects/{A_ID}/post', 'AbstractsController#publish')->name('projects.store');
Then in you blade files now you can call it like
{!! Form::open(['action' => route('projects.store', $abstract->A_ID), 'method' => 'post' ]) !!}
Hope this will clarify and resolve your problem.
I am trying to have the user choose a month from a dropdown list displaying the months, and passes that month value (in numbers) to the controller
I am having trouble making the dropdown list an action so once the user picks the month, the controller gets called.
here is my index page:
<h>Display a logs by monthly <h>
{{ $id=Form::selectMonth('month')}}
<a href="{{action('LogController#monthly',['id' => $id]) }}" class="btn btn-primary">Monthly Logs
</a>
and when I add the Form method inside the tag i am getting an error says the variable is not defined.
here is the Controller.php
public function monthly($id)
{
$dcmlogs = log::with('users')
->whereMonth('created_at', '=', $id)
->paginate(15);
return view('dcmlog.index', compact('dcmlogs'));
}
You can use something like this :
{!! Form::open(['route' => ['logs', $id], 'method' => 'POST' ])!!}
{{ Form::select('in_out',[1 => 'Jan', 2 => 'Feb'] , null, ['class'=>'
form-control'])
}}
{!! Form::close() !! }}
hope this helps ..
I recommend creatintg a route
Route::get('/logs/{id}', 'LogController#monthly')->name('logs');
And use the route function in the template:
route('logs', ['id'->$id]);
I want to create a form so when I click submit it goes to root url with post method. I created following
{{ Form::open(array('route' => array('send'), 'method' => 'post')) }}
...
{{ Form::close() }}
and in routes
Route::get('/', 'Controller#home');
Route::post('/', 'Controller#home')->name('send');
But the $request value from controller is null and the method is get instead of post.
No need to use method=>post with route, it is used with url read in docs. Change your Form::open like as below
Either
{{ Form::open(['route' => 'send']) }}
Or
{{ Form::open( ['url' => '/','method' => 'post'] ) }}
You have two route with identical beginnings ("\") pointing to the same controller function.
Try replacing the second one with something like
Route::post('/home', 'Controller#homepost')->name('send');
Just duplicate your home method and make sure you have something like:
public function homepost (Request $request)
{ .... }
You can use Route::any() If you want same url or function to response with multiple type requests like get, post etc.
Route::any('/', 'Controller#home');
Or you could use Route::match().
Route::match(['get', 'post'], '/', Controller#home');
I must be missing something really simple but I can't seem to find it. So I have my Resource defined in my routes.php but I need an additional route for an advanced search page with filters and stuff, my show/update/edit/create... pages are working perfectly but my search page isn't.
So in routes I have:
Route::resource('hostesses', 'HostessesController');
Route::get('/hostesses/search', 'HostessesController#search');
And I have a search form on my main page like this:
{{ Form::open(array('route' => 'hostesses.search', 'class' => 'navbar-form navbar-right')) }}
<div class="form-group">
{{ Form::text('search_term', '', array('class' => 'form-control')) }}
</div>
{{ Form::submit('Submit', array("class"=>'btn btn-default')) }}
{{ Form::close() }}
And when I use the search form I get the NotFoundHttpException
In my controller I have:
public function search()
{
return View::make('hostesses.search');
}
And I have created the template on views/hostesses/search.blade.php with a simple hello world message to check that it works, but I keep getting the exception!
Change the order of your routes and 'define' the named route of hostesses.search which is in your form
Route::any('/hostesses/search', array(['as' => 'hostesses.search', 'uses' => 'HostessesController#search');
Route::resource('hostesses', 'HostessesController');
Because what is happening is the resource for /hostesses/$id is capturing the search id, and returning an error that id of search does not exist
Also - change your route to Route::any(). This means it will respond to "get" and "post" requests.
However I would recommend splitting your route to be getSearch() and postSearch() functions and do this:
Route::get('/hostesses/search', array(['as' => 'hostesses.getsearch', 'uses' => 'HostessesController#getSearch');
Route::post('/hostesses/search', array(['as' => 'hostesses.postsearch', 'uses' => 'HostessesController#postSearch');
Route::resource('hostesses', 'HostessesController');
public function getSearch()
{
return View::make('hostesses.search');
}
public function postSearch()
{
// Do validation on form
// Get search results and display
}
And update your form
{{ Form::open(array('route' => 'hostesses.postsearch', 'class' => 'navbar-form navbar-right')) }}
You need to define a POST route:
Route::post('/hostesses/postSearch',array('as'=>'hostesses.search','uses' => 'HostessesController#postSearch'));
Then in your controller
public function postSearch()
{
var_dump(Input::get('search_term'));
}
I'm having a hard time setting up simple links/actions.
In my index view, I have this little form that I want to launch the getTest action in the ProjectsController when I click on the button:
{{ Form::open(array('action' => array('ProjectsController#getTest', $project->id))) }}
<button type="submit"><i class="icon-arrow-up"></i></button>
{{ Form::close() }}
This is the getTest function :
public function getTest(){
echo "test";
return 'test';
}
But this keeps getting me a "Array_combine(): Both parameters should have an equal number of elements" error.
I tried making this work with a route. with this form open instead :
{{ Form::open(['method' => 'GET', 'route' => ['test_route', $project->id]]) }}
And this route :
Route::get('projects/test', array('as' => 'test_route', 'uses' =>'ProjectsController#getTest'));
But I still have the same error.
I can't find any good doc on routing/sending to actions that don't give me this problem. I don't see what
Your route doesn't need parameter, so I think this code is sufficient:
{{ Form::open(['method' => 'GET', 'route' => 'test_route']) }}
I believe the problem is you are adding parameters to the action, but you are not managing those parameters in your routes, nor is your getTest() function accepting any parameters. Another problem is you are setting your route as a GET route, but your form is going to be using POST.
It would be much easier instead on your form to use Form::hidden('id', $project->id); And then in your getTest() function, you could get the variable using $id = Input::get('id');. You'd also be able to use your route name in your form as well. Form::open(array('route'=> 'test_route', method=> 'get'));