Controller File Code : EmergencyContactsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\EmergencyContacts;
class EmergencyContactsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function create(){
return view('patient/emergencycontacts');
}
public function store(array $data){
echo '<pre>'; print_r($data); die();
}
}
Routes File Code:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index');
Route::get('/patient/emergency-contacts' , 'patient/EmergencyContacts#index' ) ;
Route::get('/emergencycontacts_create' , 'patient/EmergencyContacts#create' ) ;
//Route::post('/emergncycontacts_store' , 'patient/EmergencyContacts#store' ) ;
Route::post('/emergencycontacts_store', ['uses' => 'patient/EmergencyContacts#store', 'as' => 'emergencycontacts_store']);
Route::resource('/patient/emergency-contacts', 'EmergencyContactsController');
Blade file Code : patient/emergencycontacts.blade.php
{!! Form::open(array('route' => 'emergencycontacts_store', 'class' => 'form')) !!}
<div class="form-group">
{!! Form::label('Salutation') !!}
{{ Form::select('salutation', ['Mr.', 'Mrs.', 'Miss.','Ms.']) }}
</div>
<div class="form-group">
{!! Form::label('First Name') !!}
{!! Form::text('firstname', null, array('required', 'class'=>'form-control', 'placeholder'=>'First Name')) !!}
</div>
<div class="form-group">
{!! Form::label('Last Name') !!}
{!! Form::text('lastname', null, array('required', 'class'=>'form-control', 'placeholder'=>'Last Name')) !!}
</div>
<div class="form-group">
{!! Form::label('Relationship') !!}
{{ Form::select('relationship', ['Father', 'Mother', 'Husband','Wife','Son','Daughter','Uncle','Aunty','Other']) }}
</div>
<div class="form-group">
{!! Form::label('Phone') !!}
{!! Form::text('phone', null, array('required'ReflectionException in Container.php line 719: Class App\Http\Controllers\patient/EmergencyContacts does not exist, 'class'=>'form-control', 'placeholder'=>'Phone')) !!}
</div>
<div class="form-group">
{!! Form::label('Fax') !!}
{!! Form::text('fax', null, array('class'=>'form-control', 'placeholder'=>'Fax')) !!}
</div>
<div class="form-group">
{!! Form::submit('Save',array('class'=>'btn btn-primary')) !!}
</div>
{{ Form::close() }}
When I try to submit it give me the following error. I am new in laravel.
Getting error:
ReflectionException in Container.php line 719: Class App\Http\Controllers\patient/EmergencyContacts does not exist
Update the controller's path given in the routes/web.php file:
'EmergencyContactsController#...'
Store function:
public function store(Request $request)
{
echo '<pre>';
print_r($request->all());
echo '</pre>';
}
Related
I want to update the user using this Form:
{!! Form::model($user ,['method'=>'PATCH', 'action'=>['AdminController#update',$user->id], 'files'=>true]) !!}
<div class="form-group">
{!! Form::label('first_name','First Name:') !!}
{!! Form::text('first_name',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('last_name','Last Name:') !!}
{!! Form::text('last_name',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('username','Username (Student ID):') !!}
{!! Form::number('username',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email','Email:') !!}
{!! Form::email('email',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('address','Address:') !!}
{!! Form::text('address','null',['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('phone_number','Phone Number:') !!}
{!! Form::number('phone_number','null',['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password','Password:') !!}
{!! Form::password('password',['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Update User', ['class'=>'btn btn-primary col-sm-6']) !!}
</div>
{!! Form::close() !!}
and this is my Route:
Route::prefix('admin')->group(function(){
Route::post('/users/{id}','AdminController#update');
)};
when I press update button I receive this error:
Please help me, I am Using Laravel 5.4
try following
{{ Form::model($user, array('route' => array('users.update', $user->id), 'method' => 'PUT')) }}
// form fiedls come here
{!! Form::close() !!}
in route file add following route:
Route::resource('users', "UserController");
and in user controller add following code:
public function update(Request $request, $id){
// Add form validation here
$user = User::find($id);
$user->first_name = $request->first_name;
$user->last_name = $request->last_name;
$user->save();
Session::Flash('success', 'The User have been updated successfully!');
return redirect()->route('users.index');
}
don't user AdminController to manage users.
Please use PATCH in routes for example :
Route::patch('/users/{id}','AdminController#update');
your method is post , but you send patch request
you should change to this
'method'=>'post'
you can use this code to fix every thing
form blade
{{ Form::model($user, array('route' => array('update-user', $user->id), 'method' => 'PUT')) }}
route
Route::prefix('admin')->group(function(){
Route::PUT('/users/{id}','AdminController#update')->name('update-user');
)};
I have created a form in Laravel so here are the following files:
The form that someone should submit some details,
contact.blade.php:
#extends('layouts.layout')
#section('content')
<main role="main">
<section class="jumbotron text-center">
<div class="container">
<h1 class="jumbotron-heading">Laravel demo</h1>
<p class="lead text-muted">Please fill the form</p>
#if(count($errors) > 0)
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach
#endif
</div>
</section>
<div class="album text-muted">
<div class="container">
{!! Form::open(['url' => 'contact/submit']) !!}
{!! csrf_field() !!}
<div class="form-group">
{{Form::label('name', 'Name') }}
{{Form::text('name', 'Enter Name', ['class'=> 'form-control'])}}
</div>
<div class="form-group">
{{Form::label('email', 'E-Mail Address') }}
{{Form::text('email', 'example#gmail.com', ['class'=> 'form-control'])}}
</div>
<div class="form-group">
{{Form::label('message', 'Enter Message') }}
{{Form::textarea('message', 'Enter Message', ['class'=> 'form-control'])}}
</div>
<div>
{{Form::submit('Submit', ['class'=> 'btn btn-primary'])}}
</div>
{!! Form::close() !!}
</div>
</div>
</main>
#endsection
Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MessageController extends Controller
{
public function submit(Request $request){
$this->validate($request, [
'name' => 'required',
'email' => 'required'
]);
return 'SUCCESS';
}
}
*
In the Routes web.php file i have included the method as post:
Route::get('/', function () {
return view('home');
});
Route::get('/contact', function () {
return view('contact');
});
Route::post('/contact/submit', 'MessageController#submit');
The error message is " RouteCollection.php (line 251)" .After searching for similar occassions here the problem ussually occurs when in the Routes you use a different method to the specified route method. I'm using POST method for submiting details, I still cannot understand why I get this.
Any help would be appreciated.
Instead of this {!! Form::open(['url' => 'contact/submit']) !!}
Try this.
{!! Form::open(['method'=>'POST','action'=>'MessageController#submit']) !!}
Add the back slash to the url of the form like so :
{!! Form::open(['url' => '/contact/submit']) !!}
I am using a fresh install of Laravel 5.4 now and I also installed https://github.com/appzcoder/crud-generator. I generated a Tickets CRUD using the generator. I was able to publish the "tickets" table to mysql database with "php artisan migrate"
I am currently stuck trying to make the $errors show if there is a missing input to one of the text inputs.
TicketsController.php
<?php
namespace App\Http\Controllers\Users;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Ticket;
use Illuminate\Http\Request;
use Session;
class TicketsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\View\View
*/
public function index(Request $request)
{
$keyword = $request->get('search');
$perPage = 25;
if (!empty($keyword)) {
$tickets = Ticket::where('user_id', 'LIKE', "%$keyword%")
->orWhere('subject', 'LIKE', "%$keyword%")
->orWhere('description', 'LIKE', "%$keyword%")
->paginate($perPage);
} else {
$tickets = Ticket::paginate($perPage);
}
return view('user.tickets.index', compact('tickets'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\View\View
*/
public function create()
{
return view('user.tickets.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
*
* #return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function store(Request $request)
{
$requestData = $request->all();
Ticket::create($requestData);
Session::flash('flash_message', 'Ticket added!');
return redirect('tickets');
}
}
View
<div class="panel panel-default">
<div class="panel-heading">Create New Ticket</div>
<div class="panel-body">
<button class="btn btn-warning btn-xs"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button>
<br />
<br />
#if ($errors->any())
<ul class="alert alert-danger">
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#endif
{!! Form::open(['url' => '/tickets', 'class' => 'form-horizontal', 'files' => true]) !!}
#include ('user.tickets.form')
{!! Form::close() !!}
</div>
</div>
form.blade.php
<div class="form-group {{ $errors->has('user_id') ? 'has-error' : ''}}">
{!! Form::label('user_id', 'User Id', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::number('user_id', null, ['class' => 'form-control']) !!}
{!! $errors->first('user_id', '<p class="help-block">:message</p>') !!}
</div>
</div><div class="form-group {{ $errors->has('subject') ? 'has-error' : ''}}">
{!! Form::label('subject', 'Subject', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::text('subject', null, ['class' => 'form-control']) !!}
{!! $errors->first('subject', '<p class="help-block">:message</p>') !!}
</div>
</div><div class="form-group {{ $errors->has('description') ? 'has-error' : ''}}">
{!! Form::label('description', 'Description', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::text('description', null, ['class' => 'form-control']) !!}
{!! $errors->first('description', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group">
<div class="col-md-offset-4 col-md-4">
{!! Form::submit(isset($submitButtonText) ? $submitButtonText : 'Create', ['class' => 'btn btn-primary']) !!}
</div>
</div>
If don't input the subject text input, I am prompted with page error:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'subject' cannot be null (SQL: insert into `tickets` (`user_id`, `subject`, `description`, `updated_at`, `created_at`) values (1, , dsa, 2017-03-08 13:05:22, 2017-03-08 13:05:22))
Instead of the $errors functionality
Do i need to include something in my TicketsController? and Middleware?
Any help is appreciated.
For this too work,
$this->validate($request, [
'subject' => 'required',
]);
Write above code in action method and then check.
Here is how you can preform validation in laravel
I am watching a Laracast on forms, and my code is quite basic, and yet so is my knowledge of the framework. My code is below, and what your looking at, if you haven't taken a guess, is that I wish to create a list of values to be stored in a database, and to "Redirect" the user to there newly stored values. The "store" part seems to be the area of issue. Whenever I click "add food items" I am not redirected, yet this error appears, MethodNotAllowedHttpException.
Route::get('food/create', 'FoodController#create');
Route::post('fond/post', 'FoodController#store');
Route::get('food/{id}', 'FoodController#index');
namespace App\Http\Controllers;
use App\Food;
use Illuminate\Http\Request;
class FoodController extends Controller
{
public function index($id)
{
$food = Food::find($id);
return view('index')->with('food', $food);
}
public function create()
{
return view('vendor.create');
}
public function store(Request $request)
{
$input = Request::all();
$food = Food::create($input);
return redirect('food/'.$food->id);
}
}
#extends('app')
#section('body')
<h1>Foods!</h1>
<h2>{{ $food->vegetables }}</h2>
<h2>{{ $food->fruit }}</h2>
<h2>{{ $food->grains }}</h2>
<h2>{{ $food->meat }}</h2>
<h2>{{ $food->sugar }}</h2>
#stop('body')
#extends('app')
#section('body')
{!! Form::open(['url'=>'food/store']) !!}
<div class="form-group">
{!! Form::label('vegetables', 'Vegetable item:') !!}
{!! Form::text('vegetables', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('fruit', 'Fruit item:') !!}
{!! Form::text('fruit', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('meat', 'Meat item:') !!}
{!! Form::text('meat', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('grains', 'Grain item:') !!}
{!! Form::text('grains', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('sugar', 'Sugar item:') !!}
{!! Form::text('sugar', null, ['class' => 'form-control']) !!}
</div>
<div>
{!! Form::submit('Add Food items', ['class' => 'btn btn-primary form-control']) !!}
{!! Form::close() !!}
</div>
#stop('body')
Add the get method in you form opening tag. It should look like this:
{!! Form::open(['url'=>'food/store','method'=>'get']) !!}
I am new at Laravel. I am building a code that displays the records that have been checked.
Following is my code.
{!! Form::open(array('action' => 'ClientsController#store','method' => 'POST', 'name' => 'f1'))!!}
#foreach($clients as $client)
{!! Form::checkbox('agree', $client->email, null, ['class' =>'questionCheckBox']) !!}
<article>
{{ $client->user_name }}
{{ $client->email }}
</article>
#endforeach
{!! Form::close() !!}
{!! Form::open(array('action' => 'ClientsController#display','method' => 'GET'))!!}
<div class="form-group">
{!! Form::submit('show-selected ',['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
I have to display $client->email when I click button "show-selected".
My controller method is,
public function display() {
$client = Client::all();
dd(Input::has('agree'));
$client = Input::has('agree') ? true : false;
return $client->email;
}
This should return true, but it is returning false, I am not getting how to get the values of checked check boxes on another page.
This Worked.
My blade file is:
{!! Form::open(array('action' => 'ClientsController#display','method' => 'GET'))!!}
<div class="form-group">
{!! Form::checkbox("agree[]", 'email_1', null) !!} <p>email_1</p>
</div>
<div class="form-group">
{!! Form::checkbox("agree[]", 'email_2', null) !!} <p>email_2</p>
</div>
<div class="form-group">
{!! Form::checkbox("agree[]", 'email_3', null) !!} <p>email_3</p>
</div>
<div class="form-group">
{!! Form::submit('Show-Selected',['class' => 'btn']) !!}
</div>
{!! Form::close() !!}
And the display method is:
public function display(){
$data = Input::get('agree');
$count = count ($data);
echo "Selected email is/are:" . $count;
echo "<br/>";
foreach ($data as $big_name){
echo $big_name;
echo "<br/>";
}
}
After clicking button "Show-selected",the value of checked check-boxes will display.