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"]) !!}
Related
I'm trying to get the correct routes for the Laravel Contact form on a single page website but I'm not sure how to apply the routes so far, since i've done it with sites that aren't single page (parallax - like website/ scrollable).
These are the routes I am creating, so everything stays on the homepage(no redirects at all because it's a one page scrollable website)
Route::get('/', 'ContactUsController#create')->name('contact.create');
Route::post('/', 'ContactUsController#store')->name('contact.store');
My Controller looks like this: Please note that the create controller returns the view to my index which of course is routed like so ('/'),
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Mail\ContactEmail;
class ContactUsController extends Controller
{
public function create()
{
return view('index');
}
public function store(Request $request)
{
$contact = [];
$contact['name'] = $request -> get('name');
$contact['phone'] = $request -> get('phone');
$contact['email'] = $request -> get('email');
$contact['subject'] = $request -> get('subject');
$contact['message'] = $request -> get('message');
//send mail logic here
Mail::to(config('mail.support.address'))->send(new ContactEmail($contact));
flash('Your Message has been sent!) -> success();
return redirect()-> route('/');
}
}
And below is my Contact Form:
{!! Form::open(['route' => 'contact.store', 'class' => 'text-light '])!!}
{!! Form::label('name', 'Your Name', ['class' => 'text-light'])!!}
{!! Form::text('name', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('phone', 'tel', ['class' => 'text-light'])!!}
{!! Form::text('phone', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('email', 'Email', ['class' => 'text-light'])!!}
{!! Form::text('email', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('subject', 'Subject', ['class' => 'text-light'])!!}
{!! Form::text('subject', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('message', 'Your Message Here..', ['class' => 'text-light'] )!!}
{!! Form::textarea('message', null, ['class' => 'form-control text-light'])!!}
{!! Form::submit('Submit', ['class' => 'btn btn-info']) !!}
{!! Form::close() !!}
#if($errors -> any())
<div class="alert alert-danger">
<ul>
#foreach($errors -> all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#include('flash::message')
Solved problem with solution :
//routes for contact form
Route::get('/contacts', 'ContactUsController#create')->name('contact.create');
Route::post('/contacts', 'ContactUsController#store')->name('contact.store');
Everything else same.
Thanks guys for all support.
//Create the routes in web.php
Route::get('contact',['as' => 'contact, 'uses' => 'ContactController#create']);
Route::post('contact',['as' => 'contact', 'uses' => 'ContactController#store']);
// Create the ContactController using the command
php artisan make:controller ContactController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ContactRequest;
use Mail;
class ContactController extends Controller
{
public function create()
{
return view('front.contact.index');
}
public function store(ContactRequest $request)
{
\Mail::send('emails.contact',
array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'user_message' => $request->get('message')
), function($message)
{
$message->from('hello#outlined.co');
$message->to('poornima#outlined.co', 'outlined')->subject('New Contact
Request from outlined');
});
return back()->with('status', 'your message has been received');
}}
On successful form submission, I should be seeing 'in here' within the browser but I get re-directed to a page not found view:
Sorry, the page you are looking for could not be found.
My form control:
{!! Form::open(['url' => 'prize_draw_store', 'class' => 'wffm-form-module text-left']) !!}
{!! Form::label('fullname', 'Name:', array('class' => 'control-label')) !!}
{!! Form::text('fullname', null, array('required', 'class'=>'form-control text-box single-line', 'placeholder'=>'')) !!}
{!! Form::label('email_address', 'Email Address:', array('class' => 'control-label')) !!}
{!! Form::text('email_address', null, array('required', 'class'=>'form-control text-box single-line', 'placeholder'=>'')) !!}
<div class="form-submit-border">
{{ Form::submit('Submit', array('class' => 'btn btn-default')) }}
</div>
{!! Form::close() !!}
My routes are well-defined:
/routes/web.php
use App\Http\Controllers\PrizeDrawController;
use App\Http\Requests\PrizeDrawFormRequest;
Route::get('/',
['as' => 'prize_draw', 'uses' => 'PrizeDrawController#create']);
Route::post('/',
['as' => 'prize_draw_store', 'uses' => 'PrizeDrawController#store']);
PrizeDrawController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\PrizeDrawFormRequest;
class PrizeDrawController extends Controller
{
public function create() {
return view('front.pages.home');
}
public function store(PrizeDrawFormRequest $request) {
var_dump('in here');
return redirect('thankyou')->with('status', 'Form submitted!');
}
}
PrizeDrawFormRequest.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PrizeDrawFormRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'email_address' => 'required|email',
'fullname' => 'required'
];
}
}
I'm expecting the code to flow like thus:
1. http://aacomp.local/prize_draw_store -- POST form submission
2. App\Http\Controllers\PrizeDrawController#store
3. PrizeDrawController::store(PrizeDrawFormRequest $request)
4. Redirect a user to thankyou page
We don't seem to reach step 3 though? I must be missing something basic -
are you able to help?
Thanks in advance.
The problem lies with:
Route::post('/',
['as' => 'prize_draw_store', 'uses' => 'PrizeDrawController#store']);
Changing this route to
Route::post('/foobar',
['as' => 'prize_draw_store', 'uses' => 'PrizeDrawController#store']);
and updating the form action:
{!! Form::open(['url' => 'foobar', ...
say - will get the desired results. Thanks to Darius. V for this.
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'm using PHPstorm with Laravel 5.2.3, and currently trying to update a persons information by reusing a form from my create page. I've followed everything to the T from the laracast tutorials, but for some reason I'm getting the MethodNotAllowed error when I hit submit on an update.
Routes
Route::group(['middleware' => ['web']], function()
{
...
Route::get('create','ResourceController#create');
Route::post('create', 'ResourceController#store');
Route::resource('pages', 'ResourceController');
});
Controller
class ResourceController extends Controller
{
...
public function create()
{
return view('pages.create');
}
public function store(Requests\CreateNewContactRequest $request)
{
ContactPerson::create($request->all());
return redirect('resource');
}
public function edit($id)
{
$user = ContactPerson::findOrFail($id);
return view('pages.edit')->with(compact('user'));
}
public function update($id, Request $request)
{
$user = ContactPerson::findOrFail($id);
$user->update($request->all());
return redirect('pages.resource');
}
}
Edit View
#extends('app')
#section('content')
{!! Form::model($user, ['method' => 'PATCH', 'action' => ['ResourceController#update', $user->id]]) !!}
<div class="form">
{!! Form::label('first', 'First Name: ') !!}
{!! Form::text('First_Name', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('last', 'Last Name: ') !!}
{!! Form::text('Last_Name', null, ['class' => 'form']) !!}
</div>
...
{!! Form::close() !!}
View Source
It shows that it's a POST method with laravel spoofing it as a PATCH.
Error
It generates wrong action in <form> tag. Try using:
'route' => ['pages.update', $user->id]
instead of:
'action' => ['ResourceController#update', $user->id]
Is it works?.
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);
}