I am getting MethodNotAllowedHttpException retuned when updating a form in Laravel 5.2. I understand there could be an issue with the put method.
The form sending from the index:
{!! Form::model('Customers', ['route'=>['products.update', Auth::user()->id]]) !!}
{{ Form::hidden('business', Auth::user()->name, array('class' => 'form-control', 'required' => '','maxlength'=>'255'))}}
{{ Form::label('post', 'Mailbox')}}
{{ Form::checkbox('post',1, null, array('class' => 'form-control'))}}
The Controller is:
public function update(Request $request, $id)
{
$this->validate($request, array (
'post' => '',
'mailbox' => '',
'conum' => '',
'prefix' => '',
'telans' => '',
'TC' => 'required',
));
//store
$post = Customers::find($id);
$post->post = $request->input('post');
$post->postpro = $request->input('mailbox');
$post->telans = $request->input('telans');
$post->conum = $request->input('conum');
$post->prefix = $request->inut('prefix');
$post->tc = $request->input('TC');
//save
$post->save();
//session flash message
//Session::flash('success','This customer has now been added');
//redirect
return redirect('/home');}
And the route is as follows:
Route::resource('products', 'ProductsController');
Thank you
Your forgot the quotes, replace this :
PHP
$post->post = $request->input(post);
with this :
$post->post = $request->input('post');
do not forget to set the _method as put.
Related
I want to save some form data, and I'm Getting error.
The Error
Action App\Http\Controllers\Admin\ConcursoController#store not defined. (0)
My Form
{!! Form::open(['action'=>'Admin\ConcursoController#store', 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('company','Entidade')}}
{{Form::text('company','',['class' => 'form-control', 'placeholder' => 'Nome da entidade aquí..'])}}
</div>
{{Form::submit('submeter', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
My Route
$this->group(['middleware' => ['auth:admin'], 'namespace' => 'Admin', 'prefix' => 'admin'], function(){
$this->get('/', 'AdminController#index')->name('admin.home');
$this->resource('concursos', 'ConcursoController');
});
Controller index Method
public function index()
{
$concursos = Concurso::all();
$title = 'Concursos';
return view('admin.concursos.index',compact('title'))->with('concursos',$concursos);
}
Controller Create method
public function create()
{
return view('admin.concursos.create');
}
Controller Store Method
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required',
]);
//Criar concurso
$concurso = new Concurso;
$concurso->title = $request->input('title');
$concurso->body = $request->input('body');
$concurso->save();
return redirect('/admin/concursos')->with('Success', 'Concurso Adicionado');
}
Laravel version 5.7.14
Probably check this file: App\Http\Controllers\Admin\ConcursoController and see if you have a function/method called 'store'. The error is pretty straightforward that script can't find that function.
replace your form
['route' => ['concursos.store']
like
{!! Form::model($transactions, ['route' => ['transaction.store'], 'method' => 'POST','class'=>"form-horizontal"]) !!}
I'm trying to populate the data to edit form. Here's my model
public function EditBatch($id,$request){
$data= DB::table('in_batch')
->where('id', $id)
->update(array(
'id'=>$request->input('id'),
'file_name' => $request->input('file_name'),
'batch_type' => $request->input('batch_type'),
'activity_type' => $request->input('activity_type'),
'schedule_time' => $request->input('schedule_time'),
'predecessor' => $request->input('predecessor'),
'priority' => $request->input('priority'),
'batch_remark'=>$request->input('batch_remark'),
'approved_by' => Auth::user()->id,
'approved_on'=>date("Y-m-d H:i:s"),
));
return $data;
}
here's my controller
public function edit($id){
$obatch = new BatchType();
$batch_type = $obatch->GetBatchTypeDropDown();
$batch = new ManageBatch();
$batch->GetBatchById($id);
return view('batch.edit', array('batch'=>$batch,'batch_type'=>$batch_type));
}
here's my view
{!! Form::open (array('url' => array('batch/update',$batch->id), 'class' => 'form-horizontal', 'method' => 'post','id'=>'editbatch')) !!}
<div class="form-group">
{!! Form::label('batch_id', 'batch_id',array('class'=>'col-md-4 control-label')) !!}
<div class="col-md-6">
{!! Form::text('batch_id',$batch->id,array('class'=>'form-control','id'=>'batch_id')) !!}
</div>
</div>
{!! Form::close() !!}
when i trying to load the data to the view as above error is displaying
Undefined property: App\Models\Batch\ManageBatch::$id (View: C:\wamp\www\hutch-in-portal\resources\views\batch\edit.blade.php)
how to solve this ?
thankyou
well i found a solution and the mistake was in the controller method
public function edit($id)
{
$obatch = new BatchType();
$batch_type = $obatch->GetBatchTypeDropDown();
$ouser = new ManageBatchUser();
$batch_user = $ouser->GetUserDropDown();
$batch = new ManageBatch();
$batch_details=$batch->GetBatchById($id);
return view('batch.edit',array('batch_details'=>$batch_details[0],'batch_type'=>$batch_type,'batch_user'=>$batch_user));
}
since i'm passing a single row to the view . i must add the index [0] in return. finally it worked
I have a problem with send email with Laravel, it's my method:
public function store(CreateUserRequest $request){
$name = Input::get('name');
$imie = Input::get('imie');
$nazwisko = Input::get('nazwisko');
$email = array('email' => Input::get('email'));
$password = Input::get('password');
Mail::send(['name' => $name], function ($message) use ($name, $imie, $nazwisko, $email, $password) {
$message->from('us#example.com', 'System Magazyn');
$message->attach('Your temporary password: '.['password' => $password]);
$message->to(['email'=>$email])->subject('Rejestracja Magazyn');
});
User::create($request->all());
Session::flash('addUserOK', 'Użytkownik dodany poprawnie.');
return redirect('user');
}
This method saved to database new user. I want send email to new user with information on the correct registration and information with temporary password.
I did as it is written in the documentation https://laravel.com/docs/5.2/mail#sending-mail
and as it is in this topic: Laravel 4 from contact form to admin email, but still Laravel returned error:
FatalThrowableError in Mailer.php line 149: Type error: Argument 2
passed to Illuminate\Mail\Mailer::send() must be of the type array,
object given, called in
C:\xampp\htdocs\kurwa_magazyn\magazyn_michal\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php
on line 219
EDIT:
This is form to register new user:
{!! Form::open(['route' => 'user.store', 'method' => 'post', 'class' => 'form-horizontal']) !!}
{!! Form::text('name', null, ['class' => 'form-control', 'placeholder' => 'Nazwa użytkownika...']) !!}<br />
{!! Form::text('imie', null, ['class' => 'form-control', 'placeholder' => 'Imię...']) !!}<br />
{!! Form::text('nazwisko', null, ['class' => 'form-control', 'placeholder' => 'Nazwwisko...']) !!}<br />
{!! Form::email('email', null, ['class' => 'form-control', 'placeholder' => 'Adres e-mail...']) !!}<br />
{!! Form::text('password', uniqid(), array('class' => 'form-control', 'placeholder' => 'Podaj hasło...')) !!}<br />
{!! Form::select('permissions', array('0' => 'Pracownik fizyczny', '2' => 'Magazynier', '3' => 'Demo użytkownik', '1' => 'Administrator'), NULL, ['class' => 'form-control']) !!}<br />
{!! Form::hidden('remember_token', bcrypt(uniqid(rand(0,9)))) !!}
{!! Form::submit('Dodaj', ['class' => 'btn btn-default']); !!}
{!! Form::close() !!}
I know this is an old question, but, for anyone with the same problem, the second argument of Mail::send must be an array. This array could contain any data that will be read in the view.
The code would be like this:
$data = ['foo' => 'baz'];
Mail::send(['name' => $name], $data, function ($message) use ($name, $imie, $nazwisko, $email, $password) {
$message->from('us#example.com', 'System Magazyn');
$message->attach('Your temporary password: '.['password' => $password]);
$message->to(['email'=>$email])->subject('Rejestracja Magazyn');
});
Then, in the view, the variable $foo could be printed:
<p>Variable foo: {{$foo}}</p>
I think the issue you have is in a weird combination of this:
$email = array('email' => Input::get('email'));
and this
$message->to(['email'=>$email])->subject('Rejestracja Magazyn');
According to API docs method to accepts either combination of email/name or or an array
Try to change your code to:
$message->to($email)->subject('Rejestracja Magazyn');
Since you already defined that array earlier here:
$email = array('email' => Input::get('email'));
I think you need to change the $email variable in this case because as per your code $email is key->value type array passed in the Email sending function and for Email sending we only need to send an array with only values containing an email address.
Change $email = array('email' => Input::get('email')); to $email = array(Input::get('email'));
my problem is that when i submit my form it pass through the get method.
The workflow is form submit -> get -> post and it should be form submit -> post.
I need to have a condition in my get method to validate de array is not null
My code:
Routes
Route::get('/pre-register-birth',array('as'=>'pre-register-birth', 'uses'=>'UserController#preRegisterBirthData'));
Route::post('/pre-register-birth', 'UserController#preRegisterBirthDataPost');
View
{{ Form::open(array('method' => 'POST', 'action' => 'UserController#preRegisterBirthDataPost',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
Controller
public function preRegisterBirthData()
{
$user = Session::get('user');
if ($user)
return View::make('user/pre-register-birth')->with('tempUser', $user);
else
return Redirect::Route('pre-register-get');
}
public function preRegisterBirthDataPost()
{
$validator = Validator::make(Input::all(),
array(
'birthplace' => 'required|max:100',
'birthdate' => 'required|max:100|date|date_format:Y-m-d'
)
);
if ($validator->fails()) {
return Redirect::Route('pre-register-birth')
->withErrors($validator)
->withInput();
} else {
$user = array(
'email' => Input::get('email'),
'pass' => Input::get('pass'),
'name' => Input::get('name'),
'surname' => Input::get('surname'),
'birthplace' => Input::get('birthplace'),
'birthdate' => Input::get('birthdate'),
'hourKnow' => Input::get('hourKnow'),
'dataCorrect' => Input::get('dataCorrect'),
'news' => Input::get('news'),
);
return Redirect::Route('pre-register-terms')->with('user', $user);
}
}
I think I've seen this issue before. Laravel for whatever odd reason doesn't like the action => ... So, change your form declaration from a to b:
// A
{{ Form::open(array('method' => 'POST', 'action' => 'UserController#preRegisterBirthDataPost',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
// B
{{ Form::open(array('method' => 'POST', 'url' => 'pre-register-birth',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
Notice I changed action => ... to url => ... It's a small change, but it might solve it. However, you may need to add in:
Route::post('/pre-register-birth', array('as'=> 'pre-register-birth', 'uses' => 'UserController#preRegisterBirthDataPost'));
So it recognizes the named route.
Hope this helps!
I'm trying to update my records in my ProjectsController, however when I try to route to the controller I am getting thrown the following error:
ErrorException
Undefined variable: project
I'm not too sure as too what I've done wrong and I'm sorry to overload you guys with code but not sure where the problem lies. Bit of a newbie with Laravel so would be great to get some help!
The function it is referring to is the following:
public function edit($id)
{
// get the project
$project = Project::find($project);
// show the edit form and pass the project
return View::make('projects.edit')
->with('project', $project);
}
My update function is as follows:
public function update($id)
{
// validate
// read more on validation at http://laravel.com/docs/validation
$rules = array(
'project_name' => 'required',
'project_brief' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
// process the login
if ($validator->fails()) {
return Redirect::to('projects/' . $id . '/edit')
->withErrors($validator)
->withInput(Input::except('password'));
} else {
// store
$project = Project::find($id);
$project->project_name = Input::get('project_name');
$project->project_brief = Input::get('project_brief');
$project->save();
// redirect
Session::flash('message', 'Successfully updated!');
return Redirect::to('profile');
}
}
I route to the Project Controller as follows:
Route::group(["before" => "auth"], function()
{
Route::any("project/create", [
"as" => "project/create",
"uses" => "ProjectController#create"
]);
Route::any("project/{resource}/edit", [
"as" => "project/edit",
"uses" => "ProjectController#edit"
]);
Route::any("project/index", [
"as" => "project/index",
"uses" => "ProjectController#index"
]);
Route::any("project/store", [
"as" => "project/store",
"uses" => "ProjectController#store"
]);
Route::any("project/show", [
"as" => "project/show",
"uses" => "ProjectController#show"
]);
});
My form is as follows:
<h1>Edit {{ $project->project_name }}</h1>
<!-- if there are creation errors, they will show here -->
{{ HTML::ul($errors->all()) }}
{{ Form::model($project, array('route' => array('projects.update', $project->id), 'method' => 'PUT')) }}
<div class="form-group">
{{ Form::label('project_name', 'Project Name') }}
{{ Form::text('project_name', null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('Project Brief', 'Project Brief') }}
{{ Form::textarea('project_brief', null, array('class' => 'form-control', 'cols' => '100')) }}
</div>
{{ Form::submit('Edit the Project!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
Looks like you misplaced $project in find(), should be $id here:
public function edit($id)
{
// get the project
$project = Project::find($id);
// show the edit form and pass the project
return View::make('projects.edit')
->with('project', $project);
}