Inserting Data into Database with Laravel 5 - php

I am trying to Update user Profile after they Login, It works well and says PROFILE UPDATED SUCCESSFULLY but nothing inserted on the Database Table, so in essence, the View shows nothing of the Updated form fields. Please anyone with an idea of where i am getting this wrong will be highly appreciated, Thanks.
CONTROLLER Update FUNCTION
public function update(Request $request)
{
$rules = [
'name' => 'required',
'email' => 'required',
'phone' => 'required|numeric',
'country' => 'required',
'gender' => 'required',
'birthday' => 'required',
'fb' => 'url',
'twitter' => 'url',
'gp' => 'url',
'instagram' => 'url',
'personal_site' => 'url',
'aboutme' => 'url',
'linkedin' => 'url',
'pinterest' => 'url'
];
$data= $request->all();
$validator = Validator::make($data, $rules);
if($validator->fails()){
return Redirect::back()->withInput()->withErrors($validator);
}
$user = Auth::user();
$user->name = $data['name'];
$user->email = $data['email'];
$user->phone = $data['phone'];
$user->country = $data['country'];
$user->birthday = $data['birthday'];
$user->address = $data['address'];
if($user->save()) {
$profile_id = $user->id;
$profile = Profile::find($profile_id);
if(count($profile) > 0) {
$profile->gender = $data['gender'];
$profile->city = $data['city'];
$profile->state = $data['state'];
$profile->aboutmyself = $data['aboutmyself'];
$profile->fb = $data['fb'];
$profile->twitter = $data['twitter'];
$profile->gp = $data['gp'];
$profile->instagram = $data['instagram'];
$profile->personal_site = $data['personal_site'];
$profile->aboutme = $data['aboutme'];
$profile->linkedin = $data['linkedin'];
$profile->pinterest = $data['pinterest'];
// $profile = $user->profile()->save($profile);
$profile->save();
}
} else {
return redirect()->back()->withInput()->withInfo("Something went wrong. Please, try again");
}
return redirect()->route('profile')->withSuccess("Your Profile Successfully Updated.");
}
MY VIEW (profile-edit.blade.php)
<div class="form-group row">
{!! Form::model($user, array('route' => 'post.edit.profile', 'method' => 'post', 'class' => 'form-horizontal')) !!}
{!! Form::label('name', "Full Name", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-8">
<div class="row">
<div class="col-md-9">
{!! Form::text('name', null, array('class' => 'form-control', 'placeholder' => 'Your Full Name', 'required' => 'required')) !!}
</div>
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('email', "Email Address", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-6">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">
<i class="material-icons md-18 text-muted">mail</i>
</span>
{!! Form::email('email', null, array('class' => 'form-control', 'placeholder' => '', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('phone', "Phone Number", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-6">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">
<i class="material-icons md-18 text-muted">mail</i>
</span>
{!! Form::text('phone', null, array('class' => 'form-control', 'placeholder' => 'e.g. +8801711223344', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('gender', "Gender", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
{!! Form::select('gender', $gender, array('class' => 'c-select form-control', 'id' => '', 'required' => 'required')) !!}
</div>
</div>
<div class="form-group row">
{!! Form::label('birthday', "Birthday", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('birthday', null, array('class' => 'datepicker form-control', 'placeholder' => '01/28/2016','id' => 'birthday', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('address', "Address", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('address', null, array('class' => 'form-control', 'placeholder' => 'Street No., Area...','id' => 'address', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('city', "City", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('city', $user->profile->city, array('class' => 'form-control', 'placeholder' => 'City', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('state', "State", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('state', $user->profile->state, array('class' => 'form-control', 'placeholder' => 'State', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('country', "Country", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-8">
<div class="row">
<div class="col-md-6">
{!! Form::text('country', null, array('class' => 'form-control', 'placeholder' => 'Country','id' => '')) !!}
</div>
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('aboutmyself', "About Me", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::textarea('aboutmyself', Auth::user()->profile->aboutmyself, array('class' => 'form-control', 'rows' => 4, 'placeholder' => 'About Yourself')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('fb', "Facebook Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('fb', $user->profile->fb, array('class' => 'form-control', 'placeholder' => 'https://facebook.com/username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('twitter', "Twitter Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('twitter', $user->profile->twitter, array('class' => 'form-control', 'placeholder' => 'https://twitter.com/username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('gp', "Google+ Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('gp', $user->profile->gp, array('class' => 'form-control', 'placeholder' => 'https://plus.google.com/+username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('personal_site', "Personal Site", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('personal_site', $user->profile->personal_site, array('class' => 'form-control', 'placeholder' => 'http://www.mywebsite.me')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('instagram', "Instagram Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('instagram', $user->profile->instagram, array('class' => 'form-control', 'placeholder' => 'https://www.instagram.com/username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('linkedin', "LinkedIn Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('linkedin', $user->profile->linkedin, array('class' => 'form-control', 'placeholder' => 'https://www.linkedin.com/username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('pinterest', "Pinterest Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('pinterest', $user->profile->pinterest, array('class' => 'form-control', 'placeholder' => 'https://www.pinterest.com/username')) !!}
</div>
</div>
</div>
<!-- <div class="form-group row">
<label for="password" class="col-sm-3 form-control-label">Change Password</label>
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon3">
<i class="material-icons md-18 text-muted">lock</i>
</span>
<input type="text" class="form-control" placeholder="Enter new password">
</div>
</div>
</div> -->
<div class="form-group row">
<div class="col-sm-8 col-sm-offset-3">
<div class="media">
<div class="media-left">
{!! Form::submit('Save Changes', array('class' => 'btn btn-success')) !!}
</div>
<!-- <div class="media-body media-middle p-l-1">
<label class="c-input c-checkbox">
<input type="checkbox" checked>
<span class="c-indicator"></span> Subscribe to Newsletter
</label>
</div> -->
</div>
</div>
</div>
{!! Form::close() !!}
User MODEL (User.php)
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Zizaco\Entrust\Traits\EntrustUserTrait;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword,EntrustUserTrait {
EntrustUserTrait::can as may;
Authorizable::can insteadof EntrustUserTrait;
}
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
public function profile(){
return $this->hasOne('App\Profile','user_id','id');
}
public function pending(){
return $this->hasMany('App\PendingTransfers', 'user_id', 'id');
}
public function transaction(){
return $this->hasMany('App\Transaction', 'user_id', 'id');
}
}
ROUTE
Route::group(array('middleware' => 'auth'), function()
{
Route::get('logout', ['as' => 'logout', 'uses' => 'Auth\AuthController#logout']);
Route::get('profile', ['as' => 'profile', 'uses' => 'UsersController#profile']);
Route::get('edit-profile', ['as' => 'edit.profile', 'uses' => 'UsersController#edit']);
Route::post('edit-profile', ['as' => 'post.edit.profile', 'uses' => 'UsersController#update']);
Route::post('edit-photo', ['as' => 'post.edit.photo', 'uses' => 'UsersController#photoUpdate']);
Route::get('dashboard', array('as' => 'dashboard', 'uses' => 'Auth\AuthController#dashboard'));
Route::get('change-password', array('as' => 'password.change', 'uses' => 'Auth\AuthController#changePassword'));
Route::post('change-password', array('as' => 'password.doChange', 'uses' => 'Auth\AuthController#doChangePassword'));
UPDATE: Thanks for the Prompt response but that didn't fix it though, Am thinking the Fault might be from the CONTROLLER FUNCTION at this point:
if($user->save()) {
$profile_id = $user->id;
$profile = Profile::find($profile_id);
if(count($profile) > 0) {
$profile->gender = $data['gender'];
$profile->city = $data['city'];
$profile->state = $data['state'];
$profile->aboutmyself = $data['aboutmyself'];
$profile->fb = $data['fb'];
$profile->twitter = $data['twitter'];
$profile->gp = $data['gp'];
$profile->instagram = $data['instagram'];
$profile->personal_site = $data['personal_site'];
$profile->aboutme = $data['aboutme'];
$profile->linkedin = $data['linkedin'];
$profile->pinterest = $data['pinterest'];
// $profile = $user->profile()->save($profile);
$profile->save();
OR Probably from the VIEW Opening Form route/variable $user
{!! Form::model($user, array('route' => 'post.edit.profile', 'method' => 'post', 'class' => 'form-horizontal')) !!}

Related

Search data from database in textbox to display - Laravel

my problem is i cannot search and cannot disply the values in my texboxes.
what i want is to search the id of each user and display its data to my textbox
How can I do this on this video This Video in Laravel?
as of now I have this
here is my page
View
{!! Form::open(['action' => 'Admin\EmployeeFilemController#search', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<input type="text" name="id" class="form-control" placeholder="Enter ID to Search"><br>
<input type="submit" class="btn btn-primary btn-md" name="search" value="Search Data">
{!! Form::close() !!}
Controller
public function search(Request $request){
$output = "";
$employees = DB::table('employeefms')->where('id')->get();
return redirect('/admin/employeemaintenance');
}
my View Inputs
<div class="form-group col-md-2">
{{Form::label('employee_no', 'Employee No.')}}
{{Form::text('employee_no', '',['class' => 'form-control', 'placeholder' => 'Employee No.'])}}
</div>
<div class="row">
<div class="form-group col-md-4">
{{Form::label('last_name', 'Last Name')}}
{{Form::text('last_name', '',['class' => 'form-control', 'placeholder' => 'Last Name'])}}
</div>
<div class="form-group col-md-4">
{{Form::label('first_name', 'First Name')}}
{{Form::text('first_name', '',['class' => 'form-control', 'placeholder' => 'First Name'])}}
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
{{Form::label('middle_name', 'Middle Name')}}
{{Form::text('middle_name', '',['class' => 'form-control', 'placeholder' => 'Middle Name'])}}
</div>
<div class="form-group col-md-4">
{{Form::label('nick_name', 'Nick Name')}}
{{Form::text('nick_name', '',['class' => 'form-control', 'placeholder' => 'Nick Name'])}}
</div>
</div>
You don't seem to pass the id entered by the user in your controller function.
$employees = DB::table('employeefms')->where('id')->get();
You may have to do the following changes
$input = $request->all();
$id = $input['id']
// $employees = DB::table('employeefms')->where('id', $id)->get();
// actually, if 'id' is the primary key, you should be doing
$employee = DB::table('employeefms')->find($id);
// now pass the data to the view where you want to display the record
// like so
return view('name_of_view', compact('employee'));
Then, use Laravel's Form-Model binding
{!! Form::model($employee,
['action' => ['Admin\EmployeeFilemController#update', $employee->id],
'method' => 'patch' // or whatever method you have defined
]) !!}
// your form fields specified above will go here
{!! Form::close() !!}

Redirect to wrong action when validation fails after form submission in laravel 5.0

I have two form in one view when I leave the text field empty it redirects to wrong action. I am looking forwrad for the solution of this problem. Thanks in hope.
Form one works fine when I enter data in ll the input fields. /inquiry/store also contains http request validation.
View code which contains two forms.
<div class="col-lg-6">
{!! Form::open(['role' => 'form', 'url' => '/inquiry/store', 'class' => 'form-horizontal', 'method'=>'POST']) !!}
<div class='form-group'>
{!! Form::label('name', 'Student Name *', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('name', $student->name,['autocomplete'=>'off' , 'placeholder' => 'Student Name', 'class' => 'form-control']) !!}
</div>
</div>
<div class='form-group'>
{!! Form::label('father_name', 'Father Name *', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('father_name',$student->father_name, ['autocomplete'=>'off' , 'placeholder' => 'Father Name', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('class', 'Class', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('class', $student->admission_class,['autocomplete'=>'off' , 'placeholder' => 'Class', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('roll_no', 'Roll Number', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('roll_no', $student->roll_no,['autocomplete'=>'off' , 'placeholder' => 'Roll Number', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('date', 'Date',['class' => 'control-label col-md-4']) !!}
<div class="col-md-8 date">
<div class="input-group input-append date" id="dateRangePicker">
{!! Form::input('date', 'date', null, ['autocomplete'=>'off' , 'placeholder' => 'Date of Birth', 'class'=>'form-control col-height datepicker']) !!}
<span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
</div>
</div>
</div>
<div class="form-group">
{!! Form::label('teacher_name', 'Teacher Name', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::select('teacher_name', $teacher, ['autocomplete'=>'off' , 'placeholder' => 'Teacher Name', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('remarks', 'Remarks', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::textarea('remarks', null, ['autocomplete'=>'off' ,
'placeholder' => 'Remarks',
'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-3 col-md-12">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
<input type="reset" class="btn btn-default" value="Reset">
</div>
</div>
{!! Form::close() !!}
second form for searching the student. When I submit the first form with out some empty field it redirect to /inquiry/search_stu. and displays MethodNotAllowedHttpException .
<form role="form" id="search_form" action="/inquiry/search_stu" method="post" class="form-inline">
<div class='form-group'>
<label class="control-label col-md-4">Search Student *</label>
<input class="form-control" type="text" name="search" placeholder="Admission No" required>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class='form-group'>
<input type="submit" form="search_form" class="btn btn-primary" name="submit">
<input type="reset" class="btn btn-default">
</div>
</form>
Controller function for search student.
public function search_student(Request $request)
{
$teacher = \App\Teacher::lists('name', 'name');
$adminid = $request['search'];
$student = Admission::where('admission_no',$adminid)->first();
return View::make('/inquiry/create', ['student'=> $student,'teacher' => $teacher]);
}
When I submit the store form it redirects to search student.
store function .
public function store(Requests\StoreInquiryRequest $request) {
$input = Input::all();
$inquiry = new Inquiry();
$inquiry->name = $input['name'];
$inquiry->father_name = $input['father_name'];
$inquiry->date = $input['date'];
$inquiry->class = $input['class'];
$inquiry->teacher_name = $input['teacher_name'];
$inquiry->roll_no = $input['roll_no'];
$inquiry->remarks = $input['remarks'];
try {
$inquiry->save();
return redirect()->to('inquiry')->with('message', 'success| Student details have been saved.');
} catch (Exception $ex) {
\Log::error($ex);
return redirect()->to('inquiry')->with('message', 'error| There was an error adding new student, please try again later.');
}
}
Http request for validation when wants to store data
public function rules()
{
$cid = \Route::input('id');
$isEdit = isset($cid) ? true : false;
$rules = array(
'name' => 'required|string|Max:50',
'father_name' => 'required|string|Max:50',
'class' => 'required|string|Max:50',
'date' => 'required|date|Max:50',
'teacher_name' => 'required|string|Max:50',
'remarks' => 'required',
'roll_no' => 'required',
);
return $rules;
}
You can performe as many forms on one page as you wish. As far as I see you should have proper validtion in your controller - different validation for each form.
You need to use proper Laravel Validation. Reading this will make your coding process easier and faster: https://laravel.com/docs/5.0/validation
Example of validation in controller in Laravel 5.0:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|unique|max:255',
'body' => 'required',
]);
}
Laravel Valiation also let you perform validations with query to database qithout writing whole SQL query: https://laravel.com/docs/5.0/validation#available-validation-rules
To display errors in view use:
#if($errors->has())
#foreach ($errors->all() as $error)
<div>{{ $error }}</div>
#endforeach
#endif
Good luck!

Laravel 5.3 - Sometimes views get broken and show script

I think it's a bit hard to explain with words so I put some screenshots. Sometimes the view get broken after several refresh like 2 out of 5 tries. It always break on content right div until the end of the page.
Not Break: https://i.stack.imgur.com/vpRJU.png
Break After Several Refresh: https://i.stack.imgur.com/swbQ1.png
I think the problem can be at my master blade layout or the page.
This is the Master Blade Layout
<!DOCTYPE html> <html lang="en">
<head>
#include('partial._head')
</head>
<body>
<div class="container main-container">
<div class="row row-bg">
#include('partial._nav')
</div>
<div class="row row-bg">
#include('partial._title')
</div>
<div class="row row-bg">
<div class="col-md-10 col-md-offset-1 content-container">
<div class="text-center text-uppercase content-title">
#yield('content-title')
#yield('session')
</div>
<hr>
<div class="content">
#yield('content')
</div>
</div>
</div>
<div class="row row-bg pb-1">
<div class="col-md-5 col-md-offset-1 content-left">
#yield('content-left')
</div>
<div class="col-md-5 content-right">
#yield('content-right')
</div>
</div>
</div>
#include('partial._footer')
#include('partial._script')
</body>
</html>
This is the Page code
#extends('main')
#section('title', 'Registrasi Akun')
#section('content-title')
Registrasi Akun
#stop
#section('content')
<div class="col-sm-12">
{!! Form::open(['method' => 'POST', 'route' => 'register', 'class' => 'form-horizontal' ]) !!}
<div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
{!! Form::label('username', 'Username', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-9">
{!! Form::text('username', null, ['class' => 'form-control',
'required' => 'required',
'minlength' => '6',
'maxlength' => '15',
'pattern' => '^[a-zA-Z0-9_-]*$',
'title' => 'only accept alphanum and dashes'
]) !!}
<small class="text-danger">{{ $errors->first('username') }}</small>
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
{!! Form::label('password', 'Password', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-9">
{!! Form::password('password', ['class' => 'form-control',
'required' => 'required',
'minlength' => '8',
'maxlength' => '16'
]) !!}
<small class="text-danger">{{ $errors->first('password') }}</small>
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
{!! Form::label('password_confirmation', 'Password Again', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-9">
{!! Form::password('password_confirmation', ['class' => 'form-control',
'required' => 'required'
]) !!}
<small class="text-danger">{{ $errors->first('password_confirmation') }}</small>
</div>
</div>
<hr>
<div class="form-group{{ $errors->has('fullname') ? ' has-error' : '' }}">
{!! Form::label('fullname', 'Nama Lengkap', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-9">
{!! Form::text('fullname', null, ['class' => 'form-control',
'required' => 'required',
'pattern' => '^[a-zA-Z ]*$',
'title' => 'only accept alphabet and spaces'
]) !!}
<small class="text-danger">{{ $errors->first('fullname') }}</small>
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
{!! Form::label('email', 'Email', ['class' =>'col-sm-3 control-label']) !!}
<div class="col-sm-9">
{!! Form::email('email', null, ['class' => 'form-control',
'required' => 'required',
'placeholder' => 'eg: foo#bar.com'
]) !!}
<small class="text-danger">{{ $errors->first('email') }}</small>
</div>
</div>
<div class="form-group{{ $errors->has('phone_number') ? ' has-error' : '' }}">
{!! Form::label('phone_number', 'No. Telepon', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-9">
{!! Form::text('phone_number', null, ['class' => 'form-control',
'required' => 'required',
'pattern' => '^[0-9]*$',
'title' => 'input number only eg: 081222333444'
]) !!}
<small class="text-danger">{{ $errors->first('phone_number') }}</small>
</div>
</div>
<div class="form-group{{ $errors->has('gender') ? ' has-error' : '' }}">
{!! Form::label('gender', 'Input label', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-9">
{!! Form::select('gender', [null => 'Choose...', 'male' => 'Pria', 'female' => 'Wanita'], null, ['id' => 'gender', 'class' => 'form-control',
'required' => 'required'
]) !!}
<small class="text-danger">{{ $errors->first('gender') }}</small>
</div>
</div>
<div class="form-group{{ $errors->has('address') ? ' has-error' : '' }}">
{!! Form::label('address', 'Input', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-9">
{!! Form::textarea('address', null, ['class' => 'form-control',
'required' => 'required',
'minlength' => '50'
]) !!}
<small class="text-danger">{{ $errors->first('address') }}</small>
</div>
</div>
</div>
#stop
#section('content-left')
<div class="form-group">
<div class="checkbox{{ $errors->has('setuju') ? ' has-error' : '' }}">
<label for="setuju" class="text-white">
{!! Form::checkbox('setuju', '1', 0, ['id' => 'setuju', 'required' => 'required']) !!}
Saya telah membaca dan menerima <br> <a class="disini" data-toggle="modal" data-target="#syarat" href="#">Syarat dan Peraturan</a> dari Garena.
</label>
</div>
<small class="text-danger">{{ $errors->first('setuju') }}</small>
</div>
#include('partial._modal')
#stop
#section('content-right')
{!! Form::submit('Register', ['class' => 'btn this-btn btn-garena pull-right']) !!}
{!! Form::close() !!}
#stop
#section('scripts')
<script>
var password = document.getElementById("password"),
confirm_password = document.getElementById("password_confirmation");
function validatePassword(){
if(password.value != confirm_password.value) {
confirm_password.setCustomValidity("Passwords Don't Match");
} else {
confirm_password.setCustomValidity('');
}
}
password.onchange = validatePassword;
confirm_password.onkeyup = validatePassword;
</script>
#stop
where is your 'scripts' yield ?
according to this line #section('scripts') you should have one.

htmlentities() expects parameter 1 to be string, array given when submitting the for

I have this form elements that contains text, select and text which generates array of the received data. for eg: {{ Form::text('email[]', null, ['class' => 'form-control', 'placeholder' => trans('Email')]) }}
If I do not validate the data displays just fine. but when I submit with validation error is displayed.
<div id="home" class="tab-pane fade in active">
<h3>Company Detail
</h3>
<div class="form-group">
{{ Form::label('name', 'Name', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('name', null, ['class' => 'form-control', 'placeholder' => trans('Name')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('country_id', 'Country', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::select('country_id', $country, null, ['class' => 'form-control']) }}
</div>
<!--col-lg-10-->
</div>
<!--form control-->
<div class="form-group">
{{ Form::label('city_id', 'City', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::select('city_id', $city, null, ['class' => 'form-control']) }}
</div>
<!--col-lg-10-->
</div>
<!--form control-->
<div class="form-group">
{{ Form::label('companyType_id', 'Company Type', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::select('companyType_id', $companyType, null, ['class' => 'form-control']) }}
</div>
<!--col-lg-10-->
</div>
<!--form control-->
<div class="form-group">
{{ Form::label('companySubType_id', 'Company Sub Type', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::select('companySubType_id', $companySubType, null, ['class' => 'form-control']) }}
</div>
<!--col-lg-10-->
</div>
<!--form control-->
<div class="form-group">
{{ Form::label('description', 'Description', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('description', null, ['class' => 'form-control', 'placeholder' => trans('Discription')]) }}
</div>
<!--col-lg-10-->
</div>
<!--form control-->
<div class="form-group">
{{ Form::label('established_year', 'Established Year', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::selectYear('year', 1950, date('Y'), null, ['class' => 'form-control']) }}
</div>
<!--col-lg-10-->
</div>
<!--form control-->
<div class="form-group">
{{ Form::label('location', 'Address', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('location', null, ['class' => 'form-control', 'placeholder' => trans('Address')]) }}
</div>
<!--col-lg-10-->
</div>
<!--form control-->
<div class="form-group">
{{ Form::label('number_of_employees', 'Number Of Employees', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('number_of_employees', null, ['class' => 'form-control', 'placeholder' => trans('Number Of Employees')]) }}
</div>
<!--col-lg-10-->
</div>
<!--form control-->
</div>
<div id="menu1" class="tab-pane fade">
<h3>Other Detail
</h3>
<div class="form-group">
{{ Form::label('email', 'Email', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
<div id="addEmailField">
<p>{{ Form::text('email[]', null, ['class' => 'form-control', 'placeholder' => trans('Email')]) }}
</p>
</div>
<p>
<input type="button" value="Add Email Field" onclick="addEmail();" class="btn btn-success" />
</p>
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('phone_type', 'Phone', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
<div class="form-inline" id="addPhoneField">
<p>
{{ Form::select('phone_type[]', ['home'=>'Home', 'work'=>'Work', 'mobile'=>'Mobile', 'fax'=>'Fax'], null, ['class' => 'form-control']) }}
{{ Form::text('phone_number[]', null, ['class' => 'form-control', 'placeholder' => trans('Phone Number')]) }}
</p>
</div>
<p>
<input type="button" value="Add Phone Field" onclick="addPhone();" class="btn btn-success" />
</p>
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('website_url', 'Website Url', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
<div id="addWebsiteField">
<p>
{{ Form::text('website_url[]', null, ['class' => 'form-control', 'placeholder' => trans('Website Url')]) }}
</p>
</div>
<p>
<input type="button" value="Add Website Field" onclick="addWebsite();" class="btn btn-success" />
</p>
</div>
<!--col-lg-10-->
</div>
</div>
<div id="menu2" class="tab-pane fade">
<h3>Menu 2
</h3>
<div class="form-group">
{{ Form::label('Sunday_open', 'Sunday Open', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('Sunday_open', null, ['class' => 'form-control', 'placeholder' => trans('Sunday Open')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('Sunday_close', 'Sunday Close', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('Sunday_close', null, ['class' => 'form-control', 'placeholder' => trans('Sunday Close')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('Monday_open', 'Monday Open', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('Monday_open', null, ['class' => 'form-control', 'placeholder' => trans('Monday Open')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('Monday_close', 'Monday Close', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('Monday_close', null, ['class' => 'form-control', 'placeholder' => trans('Monday Close')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('Tuesday_open', 'Tuesday Open', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('Tuesday_open', null, ['class' => 'form-control', 'placeholder' => trans('Tuesday Open')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('Tuesday_close', 'Tuesday Close', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('Tuesday_close', null, ['class' => 'form-control', 'placeholder' => trans('Tuesday Close')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('Wednesday_open', 'Wednesday Open', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('Wednesday_open', null, ['class' => 'form-control', 'placeholder' => trans('Wednesday Open')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('Wednesday_close', 'Wednesday Close', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('Wednesday_close', null, ['class' => 'form-control', 'placeholder' => trans('Wednesday Close')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('Thursday_open', 'Thursday Open', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('Thursday_open', null, ['class' => 'form-control', 'placeholder' => trans('Thursday Open')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('Thursday_close', 'Thursday Close', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('Thursday_close', null, ['class' => 'form-control', 'placeholder' => trans('Thursday Close')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('Friday_open', 'Friday Open', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('Friday_open', null, ['class' => 'form-control', 'placeholder' => trans('Friday Open')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('Friday_close', 'Friday Close', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('Friday_close', null, ['class' => 'form-control', 'placeholder' => trans('Friday Close')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('Saturday_open', 'Saturday Open', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('Saturday_open', null, ['class' => 'form-control', 'placeholder' => trans('Saturday Open')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('Saturday_close', 'Saturday Close', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('Saturday_close', null, ['class' => 'form-control', 'placeholder' => trans('Saturday Close')]) }}
</div>
<!--col-lg-10-->
</div>
</div>
<div id="menu3" class="tab-pane fade">
<h3>Branch
</h3>
<div id="addBranchField">
<div class="form-group">
{{ Form::label('location', 'Location', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('location[]', null, ['class' => 'form-control', 'placeholder' => trans('Location')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('branch_description', 'Description', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('branch_description[]', null, ['class' => 'form-control', 'placeholder' => trans('Description')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('long', 'Longitude', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('long[]', null, ['class' => 'form-control', 'placeholder' => trans('Longitude')]) }}
</div>
<!--col-lg-10-->
</div>
<div class="form-group">
{{ Form::label('lat', 'Latitude', ['class' => 'col-lg-2 control-label']) }}
<div class="col-lg-10">
{{ Form::text('lat[]', null, ['class' => 'form-control', 'placeholder' => trans('Latitude')]) }}
</div>
<!--col-lg-10-->
</div>
</div>
<!-- addBranchField -->
<p class="col-lg-offset-2">
<input type="button" value="Add Branch Field" onclick="addBranchField();" class="btn btn-success" />
</p>
</div>
as you can see there are input that takes array of values. When I submit the form it displays error.
Also I have the rules for the form
$this->validate($request, [
'companyName' => 'required',
]);
for now I am just printing the submitted value. print_r($request->all());
$this->validate($request, [
'email.*' => 'required',
]);
try to validate array type input data this way

issue with file uploading in laravel

I am try to upload a file with laravel but when submit the form it gives the below error
Exception
Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed
here is my blade:
{{ Form::open(array('route' => 'drivers.store', 'files' => true, 'class' => 'form-horizontal')) }}
<form role="form">
<div class="form-group first-field">
{{ Form::label('first_name', 'First Name:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4 ">
{{ Form::text('first_name', $value = null, array('placeholder' => 'ex-Jon', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('first_name') }} </span>
<div class="form-group">
{{ Form::label('last_name', 'Last Name:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('last_name', $value = null, array('placeholder' => 'ex-Doe', 'required' => 'required', 'class' => 'form-control', 'class' => 'form-control')) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('last_name') }} </span>
<div class="form-group">
{{ Form::label('email', 'Email:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('email', $value = null, array('placeholder' => 'ex-test#example.com', 'rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('email') }} </span>
<div class="form-group">
{{ Form::label('contact_number', 'Phone:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('contact_number', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('contact_number') }} </span>
<div class="form-group">
{{ Form::label('sin', 'SIN:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('sin', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('sin') }} </span>
<div class="form-group">
{{ Form::label('license_number', 'License Number:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('license_number', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('license_number') }} </span>
<div class="form-group">
{{ Form::label('license_file', 'License File:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::file('license_file') }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('license_file') }} </span>
<div class="form-group">
{{ Form::label('street_address', 'Street Address:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('street_address', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('street_address') }} </span>
<div class="form-group">
{{ Form::label('password', 'Password:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::password('password',array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-text">Between 6 and 12 Characters</span>
</div>
<span class='error-text'> {{ $errors->first('password') }} </span>
<div class="form-group">
{{ Form::label('password_confirmation', 'Confirm Password:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::password('password_confirmation', array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
</div>
<div class="form-group">
{{ Form::submit('Add Driver', array('class' => 'btn btn-primary center-block sh-request-button sign-up')) }}
</div>
</form>
{{ Form::close() }}
//controller:
public function store()
{
$input = \Input::all();
////echo "</pre>";print_r($input); $file= \Input::file('license_file.name');
$validator = $this->_modelDriver->validator($input);
if ($validator->fails()) {
return \Redirect::route('drivers.create')->withInput($input)->withErrors($validator);
}
else {
echo "test success";exit;
}
}
//validator:
public function validator(array $input, $isUpdate=false)
{
if(!$isUpdate) {
$rules = array(
'first_name'=>'required|alpha|min:2',
'last_name'=>'required|alpha|min:2',
'email'=>'required|email|unique:drivers',
'password'=>'required|between:6,12|confirmed',
'password_confirmation'=>'required|between:6,12'
);
} else {
$rules = array(
'firstname'=>'required|alpha|min:2',
'lastname'=>'required|alpha|min:2',
'email'=>'required|email',
);
}
return Validator::make($input, $rules);
}
I am new to laravel. So if someone can tell what am I doing wrong and how to fix this.
Thanks
This is happening because you are trying to return with the file input.
You should write this
$input = \Input::except('license_file');
return \Redirect::route('drivers.create')->withInput($input)->withErrors($validator);
instead of only
return \Redirect::route('drivers.create')->withInput($input)->withErrors($validator);

Categories