Passing data from form to controller Laravel 5 - php

I'm trying register users in a laravel 5 application using the restful controller.
The problem is that when I dump the data in my store function, I only get the csrf token, but not the values.
Here's what I tried so far:
Input::all();
Request::get();
Here's the code I'm executing:
Form
<form class="form-horizontal" method="post" action="/users">
<div class="form-group">
<label for="name" class="col-lg-2 control-label">
Name
</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="name" value="a">
</div>
</div>
<div class="form-group">
<label for="email" class="col-lg-2 control-label">
Email
</label>
<div class="col-lg-10">
<input type="email" class="form-control" id="email" value="a#a.Fr">
</div>
</div>
<div class="form-group">
<label for="password" class="col-lg-2 control-label">
Pw
</label>
<div class="col-lg-10">
<input type="password" class="form-control" id="password">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Create</button>
</div>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Controller
public function store()
{
$input = Input::only('name','email','password');
$user = new User;
$user->name = $input['name'];
$user->email = $input['email'];
$user->password = Hash::make($input['password']);
$user->save();
}
And Route is just
Route::resource('users','UsersController');

Your input fields are all missing a name attribute! For example
<input type="text" class="form-control" id="name" name="name" value="a">
<!-- ^^^^^^^^^^^ -->

You should put name attributes on the input fields, because html forms communicate with php with name attribute.
Example:
<input........... name="etc">
and in controller use the name you have given an input as a key :
$user->name = $input['etc'];
<input type="text" class="form-control" id="name" name="name" value="a">

Related

Old data not displayed in edit.blade.php

This is my edit method:
public function edit(User $user)
{
// ...
return view('admin.biodata.edit',
[
'title' => 'Edit Biodata',
'active' => 'biodata',
'majors' => Major::all(),
'biodata' => $user
]
);
}
edit.blade.php
#extends('admin.adminDashboard.layouts.main')
#section('container')
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2"></div>
<h1>Edit Student</h1>
<div class="container">
<form action="{{url('admin/biodata')}}" method="POST">
#csrf
<input type="hidden" name="id" value="{{ $biodata->id }}">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" value="{{$biodata->name}}">
</div>
<div class="form-group">
<label for="nrp">NRP</label>
<input type="text" class="form-control" id="nrp" name="nrp" placeholder="Enter NRP" value="{{$biodata->nrp}}">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" value="{{$biodata->email}}">
</div>
<div class="form-group">
<label for="address">Address</label>
<input type="text" class="form-control" id="address" name="address" placeholder="Enter address" value="{{$biodata->address}}">
</div>
<div class="form-group">
<label for="generation">Generation</label>
<input type="text" class="form-control" id="generation" name="generation" placeholder="Enter generation" value="{{$biodata->generation}}">
</div>
<div class="form-group">
<label for="major">Major</label>
<select class="form-control" id="major" name="major_id">
#foreach ($majors as $major)
<option value="{{$major->id}}">{{$major->nama_jurusan}}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
#endsection
When I press the edit button from the index view, the old data is not displayed in the view admin.biodata.edit. I thought I have already passed the user data through the biodata variable.
Replace the value in the template with
value="{{old('name',$biodata->name)}}"

After submitting a form i get an error "Sorry, the page you are looking for could not be found."

I'm new with laravel and now i making some small project. I have a form, after the submit button pressed i got this error message "Sorry, the page you are looking for could not be found."
Is there anything wrong about my code?
Please help me to fix this issue, so i can continuing the project.
Thanks in advice
view blade, i named it index.blade.php
<div class="col m7 s12">
<form method="submit" action="post">
{{ csrf_field() }}
<div class="card-panel">
<h5>Please Fill Out This Form</h5>
<div class="input-field">
<input type="text" name="name" id="name" required class="validate">
<label for="name">Name</label>
</div>
<div class="input-field">
<input type="email" name="email" id="email" class="validate">
<label for="email">Email</label>
</div>
<div class="input-field">
<input type="text" name="phone" id="phone">
<label for="phone">Phone</label>
</div>
<div class="input-field">
<textarea name="message" id="message" class="materialize-textarea"></textarea>
<label for="message">Message</label>
</div>
<button type="submit" class="btn" blue darken-1>Send</button>
</div>
</form>
controller, i named it LayoutController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class LayoutController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
return view('layouts/index');
}
public function submit(Request $request)
{
$name = $req->input('name');
$email = $req->input('email');
$phone = $req->input('phone');
$message = $req->input('message');
$data = array('name'=>$name,"email"=>$email,"phone"=>$phone,"message"=>$message);
$data->save();
return Redirect::to('/layouts/index');
}
routes web.php
Route::get('/', 'LayoutController#index');
Route::post('/submit', 'LayoutController#submit');
Your form method should be POST and action should be /submit
<form method="POST" action="/submit">
{{ csrf_field() }}
<div class="card-panel">
<h5>Please Fill Out This Form</h5>
<div class="input-field">
<input type="text" name="name" id="name" required class="validate">
<label for="name">Name</label>
</div>
<div class="input-field">
<input type="email" name="email" id="email" class="validate">
<label for="email">Email</label>
</div>
<div class="input-field">
<input type="text" name="phone" id="phone">
<label for="phone">Phone</label>
</div>
<div class="input-field">
<textarea name="message" id="message" class="materialize-textarea"></textarea>
<label for="message">Message</label>
</div>
<button type="submit" class="btn" blue darken-1>Send</button>
</div>
</form>
Try this :
<form method="POST" action="{{ route('submit') }}">
The Error you're getting is because the wrong <form> tag attributes
action => 'The route or page or class method that'll process the form
information'
method => 'This the URI HTTP verb used to transport information, you
can either use POST(sending data as http payload) or GET(sending data
as query string)
changing the <form> tag like this will solve your issue
<form method="POST" action="{{ url('/submit') }}">
The form method should be POST and the action will be your route:
<form method="POST" action="{{ url('/submit') }}">
<div class="col m7 s12">
<form method="POST" action="{{url('/submit')}}">
{{ csrf_field() }}
<div class="card-panel">
<h5>Please Fill Out This Form</h5>
<div class="input-field">
<input type="text" name="name" id="name" required class="validate">
<label for="name">Name</label>
</div>
<div class="input-field">
<input type="email" name="email" id="email" class="validate">
<label for="email">Email</label>
</div>
<div class="input-field">
<input type="text" name="phone" id="phone">
<label for="phone">Phone</label>
</div>
<div class="input-field">
<textarea name="message" id="message" class="materialize-textarea"></textarea>
<label for="message">Message</label>
</div>
<button type="submit" class="btn" blue darken-1>Send</button>
</div>
</form>

How to use loop with array in laravel

I have a student registration form, when you enroll your'e child you fill up the form then inside the form has information about the father like name, occupation,email and etc.. then inside the form also you can put your'e child's name, age gender etc .. now if you have multiple child you can click the + button to add another row for the second child then once you submit the form the two child you key-in will have the same father's information.
Question: how do i construct it into my Controller? and how do i loop the parent's information. Currently with my code i can only save array of child information i can't loop the father's information in each child.
Controller:
public function store(Request $request)
{
//dd($request->all());
$this->validate($request, [
'student_name.*'=>'required|max:50',
'age.*'=>'required|integer',
//Roles Goes Here
]);
$input = $request->all();
if (count($input['student_name']) > 0) {
for ($i = 0 ; $i < count($input['student_name']) ; $i++) {
$leads = new Lead();
$leads->student_name = $input['student_name'][$i];
$leads->gender = $input['gender'][$i];
$leads->age = $input['age'][$i];
$leads->father_name=$request->father_name;
$leads->father_contact1=$request->father_contact1;
$leads->father_email=$request->father_email;
$leads->father_occupation=$request>father_occupation;
$leads->father_religion=$request->father_religion;
$leads->save();
}
VIEW:
<form action="{{route('leads.store')}}" method="POST">
{{csrf_field()}}
<div class="col-sm-12">
<h3 class="well">Parent's Information</h3>
<div class="row">
<div class="col-sm-4 form-group">
<label>FATHER'S NAME</label>
<input class="form-control" id="txtuppercase1" type="text" name="father_name" value="{{old('father_name')}}" placeholder="Enter FATHER NAME.." >
</div>
<div class="col-sm-4 form-group">
<label>CONTACT</label>
<input type="text" id="txtuppercase2" name="father_contact1" value="{{old('father_contact1')}}" placeholder="Enter CONTACT.." class="form-control" >
</div>
<div class="col-sm-4 form-group">
<label>EMAIL ADDRESS</label>
<input type="text" name="father_email" value="{{old('father_email')}}" placeholder="Enter EMAIL ADDRESS.." class="form-control" >
</div>
<div class="col-sm-4 form-group">
<label>OCCUPATION</label>
<input type="text" name="father_occupation" value="{{old('father_occupation')}}" placeholder="Enter OCCUPATION.." class="form-control" >
</div>
<div class="col-sm-4 form-group">
<label>RELIGION</label>
<input type="text" name="father_religion" value="{{old('father_religion')}}" placeholder="Enter RELIGION.." class="form-control" >
</div>
</div>
<br>
<div class="row">
<div class="col-md-12">
<div data-role="dynamic-fields">
<div class="form-inline">
<div class="form-group">
<label class="sr-only" for="student_name">NAME</label>
<input type="text" class="form-control" id="student_name" name="student_name[]" placeholder="STUDENT NAME">
</div>
<span>-</span>
<div class="form-group">
<label class="sr-only" for="age">AGE</label>
<input type="text" class="form-control" id="age" name="age[]" placeholder="AGE">
</div>
<span>-</span>
<div class="form-group">
<label class="sr-only" for="gender">GENDER</label>
<input type="text" class="form-control" id="gender" name="gender[]" placeholder="GENDER">
</div>
<span>-</span>
<div class="form-group">
<label class="sr-only" for="school">SCHOOL</label>
<input type="text" class="form-control" id="last_school_attended" name="last_school_attended[]" placeholder="SCHOOL">
</div>
<button class="btn btn-danger" data-role="remove">
<span class="glyphicon glyphicon-remove"></span>
</button>
<button class="btn btn-primary" data-role="add">
<span class="glyphicon glyphicon-plus"></span>
</button>
</div> <!-- /div.form-inline -->
</div> <!-- /div[data-role="dynamic-fields"] -->
</div> <!-- /div.col-md-12 -->
</div> <!-- /div.row -->
<br>
<div>
<button type="submit" class="btn btn-success">SUBMIT</button>
CANCEL
</div>
</form>
You don't need to loop the parent's information. You can directly access the parent's information through $input['father_name'];

Laravel MethodNotAllowedHttpException when i try to save fields?

Hi i try to save fields in db ot 2 steps but i have MethodNotAllowedHttpException
on step 1 in my controller i save the 2 fields in db
public function storeNumber(Request $request){
$number = new Number;
$number->user_id = $user = \Auth::user()->id;
$number->number = $this->getGeneratedNumber();
$number = new Number($request->all());
$number->save();
return redirect('numbers/{id}/details');
}
view
<form class="form-horizontal" method="POST" action="{{action('NumberController#storeNumber')}}">
{{ csrf_field() }}
<div class="form-group">
<div class="col-md-12">
<button type="submit" class="btn btn-primary btn-block">
Generate Numbers
</button>
</div>
</div>
model
class Number extends Model
{
/**
* #var array
*
*/
protected $fillable = [
'number'
];
}
on step 2 i want to save another field in same db with same controller this is my another store function for store another fields in same db . but when i try to save laravel say MethodNotAllowedHttpException.
public function store(Request $request, $id){
$number = Number::find($id);
$number = new Number($request->all());
$number->save();
return redirect('numbers');
}
this is my view
<form method="post" action="{{action('NumberController#store', $id)}}">
{{csrf_field()}}
<input name="_method" type="hidden" value="PATCH">
<div class="form-group">
<label for="number" class="control-label">Number</label>
<input type="text" class="form-control form-control-lg disabled" placeholder="Number" name="number" value="{{$number->number}}">
</div>
<div class="form-group">
<label for="comment" class="control-label">Comment</label>
<textarea name="comment" class="form-control form-control-lg" cols="30" rows="10">{{$number->comment}}</textarea>
</div>
<div class="form-group">
<label for="accept" class="control-label">Accept</label>
<input type="radio" name="accept" value="1">Yes<br>
<input type="radio" name="accept" value="0">No<br>
</div>
#if($number->accept == 1)
<div class="form-group">
<label for="name" class="control-label">Number</label>
<input type="text" class="form-control form-control-lg disabled" placeholder="Name" name="name" value="{{$number->name}}">
</div>
<div class="form-group">
<label for="city" class="control-label">City</label>
<input type="text" class="form-control form-control-lg" placeholder="City" name="city" value="{{$number->city}}">
</div>
<div class="form-group">
<label for="postcode" class="control-label">Postcode</label>
<input type="number" class="form-control form-control-lg" placeholder="Postcode" name="postcode" value="{{$number->postcode}}">
</div>
<div class="form-group">
<label for="address">Address</label>
<textarea name="address" class="form-control form-control-lg" cols="30" rows="10">{{$number->address}}</textarea>
</div>
#else
<p>TODO status for NO</p>
#endif
<div class="form-group">
<div class="col-md-12">
<button type="submit" class="btn btn-default">Finish</button>
</div>
</div>
</form>
You have a "_method" filed with "PATCH" value, so you have to change the route to "patch" instead of "post".
Please change blade content of update as follow
<form method="post" action="{{action('NumberController#store', $id)}}">
to
<form method="post" action="{{action('NumberController#update', $id)}}">
return redirect('numbers/{id}/details');
Here is incorrect, what is id?

How To Solve Laravel and MySql Error Called SQLSTATE[23000]

I want to create a Registration form. So, I have created a RegShortController.php file and after I have created view file called RegShort.blade.php. In RegShortController.php file , I have used a function called insert. But , When I run the Web Site , I got this error -
"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name'
cannot be null (SQL: insert intoacademic(name,username,pw)
values (, , ))"
So , How to Fix this ??
Here is the RegShort.blade.php file
Unfortunately , I have removed name attributes in username and password when I edit this Thread. Now , I fixed it.
<form action="{{ route('RegShort') }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label>Name : *</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label>Username : *</label>
<input type="text" class="form-control" name="username" required>
</div>
<div class="form-group">
<label>Password : *</label>
<input type="password" class="form-control" name="password">
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>
Here is the RegShortController.php file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class RegShortController extends Controller
{
public function index()
{
return view('RegShort');
}
function insert(Request $req)
{
$name = $req->input('name');
$username = $req->input('username');
$password = $req->input('password');
$data = array("name"=>$name,'username'=>$username,"pw"=>$password);
DB::table('academic')->insert($data);
}
}
Here is the Route that I have created.
Route::any('/RegShort', 'RegShortController#insert')->name('RegShort');
name field must be non empty, so u should validate data before inserting it (server side validation):
function insert(Request $req)
{
$this->validate($req, [
'name' => 'required'
]);
$name = $req->input('name');
$username = $req->input('username');
$password = $req->input('password');
$data = array("name"=>$name,'username'=>$username,"pw"=>$password);
DB::table('academic')->insert($data);
}
BTW would be nice to validate data by javascript as well before sending data to server (client side validation).
Edit ur HTML, uve missed name attributes for ur inputs:
<form action="{{ route('RegShort') }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label>Name : *</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label>Username : *</label>
<input type="text" class="form-control" name="username" required>
</div>
<div class="form-group">
<label>Password : *</label>
<input type="password" name="password" class="form-control">
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>
Also change ur routes:
Route::get('/RegShort', 'RegShortController#index')->name('RegShort');
Route::post('/RegShort', 'RegShortController#insert');
It seems that you are unable to get data from the $req object. Be sure to check your data before inserting into the table.
you dont have name attribute for username and password
<div class="form-group">
<label>Name : *</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label>Username : *</label>
<input type="text" name="username" class="form-control" required>
</div>
<div class="form-group">
<label>Password : *</label>
<input type="password" name="password" class="form-control">
</div>
Try to update html:
You are messing: name attribute here
<input type="text" name="username" class="form-control" required>
<input type="password" name="password" class="form-control">
Code:
<form action="{{ route('RegShort') }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label>Name : *</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label>Username : *</label>
<input type="text" name="username" class="form-control" required>
</div>
<div class="form-group">
<label>Password : *</label>
<input type="password" name="password" class="form-control">
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>

Categories