I have edit page contains 3 fields company, dealership, branch, when I click one of the save list of the branch I need to show the details into the page but when I did this it shows undefined variable error throwing up, how to fix this and also need to update the form after listing the details in the corresponding fields
Edit Page
#include('theme.header')
<?php
use App\Company;
?>
<div class="page-content-wrapper ">
<div class="container-fluid">
<div class="row">
<div class="col-sm-12">
<div class="page-title-box">
<div class="btn-group float-right">
</div>
<h4 class="page-title">Branch Management</h4>
</div>
</div>
</div>
<!-- end page title end breadcrumb -->
<div class="row">
<div class="col-12">
<div class="card m-b-30">
<div class="card-body">
<h4 class="mt-0 header-title">Branch</h4>
<br>
<br>
{!! Form::open(['method' => 'PUT', 'route' => ['branchs.update',$branch->id]] ) !!}
<div class="form-group row">
<label class="col-sm-2 col-form-label">Company</label>
<div class="col-sm-10">
<?php
$comp=Company::where('comp_id',$branch->comp_id)->first();
$companies=Company::where('status','0')
->get();
?>
<input type="hidden" name="br_id" id="br_id" value="{{$branch->br_id}}">
<select class="form-control" id="company" name="company">
<option selected value="{{$branch->br_id}}">{{$comp->name}}</option>
#foreach($companies as $company)
<option value="{{$company->comp_id}}">{{$company->name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Dealership</label>
<div class="col-sm-10">
<?php
$cn = App\Dealership::where('dlr_id', $branch->dlr_id)->first();
$companies =App\Dealership::where('status', '0')
->get();
?>
<select class="form-control" id="dealer" name=" dealer">
<option>Select Dealership</option>
#foreach($dealership as $dealerships)
<option value="{{$dealerships->dlr_id}}">{{$dealerships->name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label for="example-text-input" class="col-sm-2 col-form-label">Branch Name</label>
<div class="col-sm-10">
<input class="form-control" type="text" id="branch" name="branch" value="{{$branch->name}}">
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="page-title-box">
<div class="btn-group float-right">
<button class="btn btn-primary" type="submit">Button</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div> <!-- end col -->
</div> <!-- end row -->
</div>
</div>
#include('theme.footer')
Controller File
<?php
namespace App\Http\Controllers;
use App\Branch;
use App\Company;
use App\Dealership;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class BranchController extends Controller
{
public function index()
{
$companies = Company::where('status', '0')->get();
$dealership = Dealership::where('status', '0')->get();
$branches = Branch::where('status', '0')->get();
return view('branch.index', compact('branches'));
}
public function store(Request $request)
{
$branch_id = new Branch;
$branch_id = Branch::orderBy('br_id', 'desc')->take(1)->get();
if (count($branch_id) > 0) {
$id = $branch_id[0]->br_id;
$id = $id + 1;
} else {
$id = 1;
}
$branch = new Branch;
$branch->br_id = $id;
$branch->name = $request->input('branch');
$branch->dlr_id = $request->input('dealer');
$branch->comp_id = $request->input('company');
$branch->created_id = '0';
$branch->save();
return redirect()->back()->with('message', 'Successfully saved');
}
public function edit(Branch $branch)
{
$branch=Branch::where('id',$branch->id)->first();
return view('branch.edit',['branches'=>$branch]);
}
public function update(Request $request,$id)
{
$branch = Branch::findOrFail($id);
$branch->status = '1';
$branch->save();
if ($branch) {
$branchs = new Branch();
$branchs->comp_id = $request->input('company');
$branchs->dlr_id = $request->input('dealer');
$branchs->name = $request->input('branch');
$branchs->created_id = '0';
$branchs->save();
if ($branchs) {
return redirect('/branch')->with('message', 'Successfully saved');
}
}
}
public function destroy(Branch $branch)
{
DB::table('branches')
->where('id', $branch->id)
->update(['status' => '-1']);
return back()->with('message', 'Successfully Deleted');
}
}
You are passing branch with parameter name branches. Change your controller code to:
public function edit(Branch $branch)
{
$branch=Branch::where('id',$branch->id)->first();
return view('branch.edit',['branch'=>$branch]);
}
You are sending 'branches' from your controller and used $branch in your view! Try to change it in your edit() function like:
public function edit(Branch $branch)
{
$branch=Branch::where('id',$branch->id)->first();
return view('branch.edit',['branch' => $branch]);
}
And also, in your index() function change
return view('branch.index', compact('branches'));
To
return view('branch.index', compact(['branches', 'companies', 'dealership']));
Hope this will helps you!
Everything seems to be correct just that you are sending branches instead of branch and when the view looks up for branch it's not available .. that's the error you are getting
Related
I'm trying to save in database some elements and a photo with a form, I've added the "enctype="multipart/form-data" on the form and I've executed the command "php artisan storage:link", but when I click on Upload button, Laravel returns me this error: "Call to a member function store() on null" on file PostsController
Here is my file PostsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Post;
class PostsController extends Controller
{
public function submitpost(Request $req)
{
$posttitle = $req->input('posttitle');
$postauthor = $req->input('postauthor');
$postcontent = $req->input('postcontent');
$img = $req->input('img')->store('public/img');
$dati = compact('posttitle', 'postauthor', 'postcontent', 'img');
$b = new Post();
$b->posttitle = $posttitle;
$b->postauthor = $postauthor;
$b->postcontent = $postcontent;
$b->img = $img;
$b->save();
$posts = Post::all();
return view('blog', compact('posts'));
}
}
This is my view file addpost.blade.php
<main class="main-content">
<div class="container-fluid photos">
<div class="row justify-content-center">
<div class="col-md-6 pt-4" data-aos="fade-up">
<h2 class="text-white mb-4">Create a new post</h2>
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-12">
<form action="{{route('submitpost')}}" enctype="multipart/form-data" method="post">
#csrf
<div class="row form-group">
<div class="col-md-12">
<label class="text-white" for="subject">Post Title</label>
<input type="subject" name="posttitle" id="subject" placeholder="Give a title to the post" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="subject" readonly hidden name="postauthor" id="subject" value="{{Auth::user()->name}}" class="form-control">
</div>
</div>
<div class="row form-group mb-5">
<div class="col-md-12">
<label class="text-white" for="message">Content of post</label>
<textarea name="postcontent" id="message" cols="30" rows="7" class="form-control" placeholder="Write your post here"></textarea>
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="file" name="img" value="{{old('img')}}" placeholder="Image" value="">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="submit" value="Create Post" class="btn btn-primary btn-md text-white">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</main>
This is my Route in web.php
Route::post('/addpost/submitpost', 'PostsController#submitpost')->name('submitpost');
And this is my model Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'posttitle',
'postauthor',
'postcontent',
'img',
];
protected $table = 'posts';
}
As you can see in the documentation, uploaded files can be accessed using the file() method.
Try updating this:
$img = $req->input('img')->store('public/img');
To this:
$path = $req->file('img')->store('public/img');
^^^^^^^^^^
Also, you can confirm if the request actually has the file in its payload checking if the file exists, to avoid exceptions:
if ($req->hasFile('img')) {
$path = $req->file('img')->store('public/img');
}
This and more useful methods, can be seen in the File Uploads section of the docs.
For example if there is a form which creates customers, and when you filled the form and submit the page should redirect and show the data which was inserted to that form by a specific individual id. in my case it redirects to a page and views all the data in the database.
Here's my Web.php
Route::get('customers','CustomersController#index');
Route::get('customers/create','CustomersController#create');
Route::post('customers','CustomersController#store');
Route::get('customers/{customer}','CustomersController#show');
Here's my Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Customer;
use App\Company;
class CustomersController extends Controller
{
public function index(){
$customers = Customer::all();
return view('customers.index',compact('customers'));
}
public function create(){
$companies = Company::all();
return view ('customers.create',compact('companies'));
}
public function store()
{
$data = request()->validate([
'name'=>'required | min:3',
'email'=>'required | email',
'active'=>'required ',
'company_id'=>'required',
]);
Customer::create($data);
return redirect('customers');
}
// * Route Model binding
public function show(Customer $customer){
return view('customers.show',compact('customer'));
}
}
Here's my Customer Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $guarded = [];
public function getActiveAttribute($attribute){
return [
0 => 'Inactive',
1 => 'Active',
] [$attribute];
}
public function scopeActive($query){
return $query->where('active',1);
}
public function scopeInactive($query){
return $query->where('active',0);
}
public function company()
{
return $this->belongsTo(Company::class);
}
}
Here's my Create Blade
#extends('layouts')
#section('title','Add New Customer')
#section('content')
<div class="row">
<div class="col-12">
<h1>Add New Customer</h1>
</div>
</div>
<div class="row">
<div class="col-12">
<form action="/customers" method="POST" >
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" value="{{old('name')}}">
<div>{{$errors->first('name')}}</div>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" name="email" value="{{old('email')}}">
<div>{{$errors->first('email')}}</div>
</div>
<div class="form-group">
<label for="">Customer Status</label>
<select name="active" id="active" class=" form-control">
<option value="" disabled>Select Customer Status</option>
<option value="1">Active</option>
<option value="0">Inactive</option>
</select>
</div>
<div class="form-group">
<label for="company_id">Company</label>
<select name="company_id" id="company_id" class=" form-control">
#foreach ($companies as $company)
<option value="{{ $company->id }}">{{ $company->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class=" btn btn-dark">Add Customer</button>
#csrf
</form>
</div>
</div>
#endsection
Here's my show blade
#extends('layouts')
#section('title','Details for ' . $customer->name)
#section('content')
<div class="row">
<div class="col-12">
<h1>Details for {{ $customer->name }}</h1>
</div>
</div>
<div class="row">
<div class="col-12">
<p><strong>Name : </strong> {{ $customer->name }}</p>
<p><strong>Email : </strong> {{ $customer->email }}</p>
<p><strong>Company : </strong> {{ $customer->company->name }}</p>
<p><strong>Status : </strong> {{ $customer->active }}</p>
</div>
</div>
#endsection
Redirect to the show route rather than the customers index route:
$customer = Customer::create($data);
return redirect('customers/' . $customer->id);
I wish to be able to edit my users through the admin panel but this returns the following error to me:
Trying to get property 'id' of non-object
it will be an error in my view with the call of the variable ID if I change it I have the same thing with my variable name.
I use the users table and in no other place in my code do I have problems
help me please
URI : /role-edit/{id}
View :
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4>Edit register roles</h4>
</div>
<div class="card-body">
<form action="/role-register-update/{{ $users->id }}" method="POST">
{{ csrf_field() }}
{{ method_field('PUT') }}
<div class="form-group">
<label>Name</label>
<input type="text" name="name" value="{{ $users->name }}" class="form-control">
</div>
<div class="form-group">
<label>Give role</label>
<select name="type" class="form-control">
<option value="admin">Admin</option>
<option value="vendor">Vendor</option>
<option value="">None</option>
</select>
<button type="submit" class="btn btn-success">Update</button>
Cancel
</div>
</form>
</div>
</div>
</div>
</div>
</div>
Controller :
class DashboardController extends Controller
{
public function registered()
{
$users = User::all();
return view('admin.registeradmin')->with('users', $users);
}
public function edit(Request $request,$id)
{
$users = User::findOrFail($id);
return view('admin.edit-register')->with('users',$users);
}
public function update(Request $request, $id)
{
$users = User::findOrFail($id);
$users->name = $request->input('name');
$users->usertype = $request->input('type');
$users->update();
return redirect('/role-register')->with('status', 'You data is update');
}
public function destroy($id)
{
$users = User::where('id', $id);
if ($users != null)
{
$users->delete();
return redirect('/role-register')->with('status', 'User is correctly deleted !');
}
return redirect('/role-register')->with('status', 'User is not correctly deleted !');
}
}
Routes :
Route::get('/', function () {
return view('pages.home');
});
Route::get('/aboutus', function () {
return view('pages.aboutus');
})->name('aboutus');
Auth::routes();
Route::get('profile', 'UserProfileController#show')->middleware('auth')->name('profile.show');
Route::post('profile', 'UserProfileController#update')->middleware('auth')->name('profile.update');
Route::get('/home', 'HomeController#index')->name('home');
Route::group(['middleware' => ['auth', 'admin']], function () {
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
Route::get('/role-register', 'Admin\DashboardController#registered');
Route::get('/role-edit/{id}', 'Admin\DashboardController#edit');
Route::put('/role-register-update/{id}', 'Admin\DashboardController#update');
Route::delete('/role-delete/{id}', 'Admin\DashboardController#destroy');
});
Add dd($users) in the edit function of your controller. If you get the data, add the following to the form action form action:
{{route('routename',['id'=>$users->id])}}
// Controller
public function Updateprofile(Request $request)
{
if (Auth::check() && Auth::user()->role->id == 2) {
$this->validate($request, [
'name' => 'required',
'email' => 'required|email'
]);
$image = $request->file('image');
$slug = str_slug($request->name);
if (isset($image))
{
$currentDate = Carbon::now()->toDateString();
$imagename = $slug.'-'.$currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(600,500);
if (!file_exists('storage/uploads/profile'))
{
mkdir('storage/uploads/profile',0777,true);
}
unlink('storage/uploads/profile/'.Auth::user()->image);
$image_resize->save('storage/uploads/profile/'.$imagename);
}else{
$imagename = Auth::user()->image;
}
$user = User::find(Auth::id());
$user->name = $request->name;
$user->email = $request->email;
$user->image = $imagename;
$user->save();
Toastr::success('Profile Successfully Updated :)', 'Success');
return redirect()->back();
}
}
// blade file
<form method="POST" action="{{route('user.profile.update')}}" class="form-horizontal" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="row clearfix">
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-5 form-control-label">
<label for="name">Name : </label>
</div>
<div class="col-lg-10 col-md-10 col-sm-8 col-xs-7">
<div class="form-group">
<div class="form-line">
<input type="text" id="name" class="form-control" placeholder="Enter your name" name="name" value="{{Auth::user()->name}} {{old('name')}}">
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-5 form-control-label">
<label for="image">{{__('Image')}} : </label>
</div>
<div class="col-lg-10 col-md-10 col-sm-8 col-xs-7">
<div class="form-group">
<div class="form-line">
<input type="file" name="image" >
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-5 form-control-label">
<label for="email_address_2">Email Address</label>
</div>
<div class="col-lg-10 col-md-10 col-sm-8 col-xs-7">
<div class="form-group">
<div class="form-line">
<input type="text" id="email_address_2" class="form-control" value="{{Auth::user()->email}} {{old('email')}}" placeholder="Enter your email address" name="email" ">
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-lg-offset-2 col-md-offset-2 col-sm-offset-4 col-xs-offset-5">
<button type="submit" class="btn btn-primary m-t-15 waves-effect">UPDATE</button>
</div>
</div>
</form>
It's showing the following errors:
1.Severity: Warning
Message: Missing argument 1 for Super_Admin::edit_category()
Filename: controllers/super_admin.php
Severity: Notice
Message: Undefined variable: category_id
Filename: controllers/super_admin.php
Below is Model's code :
<?php
class Super_Admin_Model extends CI_Model
{
public function select_catgory_info_by_id($category_id)
{
$this->db->select('*');
$this->db->from('tbl_category');
$this->db->where('category_id', $category_id);
$query_result = $this->db->get();
$result = $query_result->row();
return $result;
}
public function update_category_by_id($category_id, $data)
{
$this->db->where('category_id', $category_id);
$this->db->update('tbl_category', $data);
}
}
?>
Below is controller's code :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Super_Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
$admin_id = $this->session->userdata('admin_id');
if ($admin_id == NULL)
{
redirect('admin_login', 'refresh');
}
}
public function edit_category($category_id)
{
$data = array();
$data['category_info'] = $this->super_admin_model->select_catgory_info_by_id($category_id);
$data['maincontent'] = $this->load->view('admin/edit_category', $data, true);
$data['title'] = 'Edit Category';
$this->load->view('admin/admin_master', $data);
}
public function update_category()
{
$data = array();
$category_id = $this->input->post('category_id', TRUE);
$data['category_name'] = $this->input->post('category_name', TRUE);
$data['category_description'] = $this->input->post('category_description', TRUE);
$data['publication_status'] = $this->input->post('publication_status', TRUE);
$this->super_admin_model->update_category_by_id($category_id, $data);
$sdata['message'] = "Updated successfully";
$this->session->set_userdata($sdata);
redirect('super_admin/edit_category');
}
}
?>
This is my view page:
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Category Info</h1>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
Edit Category
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<form action="<?php echo base_url(); ?>super_admin/update_category" method="post" >
<div>
<?php
$message = $this->session->userdata('message');
if ($message)
{
echo $message;
$this->session->unset_userdata('message');
}
?>
</div>
<div class="form-group">
<label>Category Name</label>
<input class="form-control" name="category_name" value="<?php echo $category_info->category_name; ?>">
<input type="hidden" class="form-control" name="category_id" value="<?php echo $category_info->category_id; ?>">
<!-- <p class="help-block">Example block-level help text here.</p> -->
</div>
<div class="form-group">
<label>Category Description</label>
<textarea class="form-control" rows="3" name="category_description" ><?php echo $category_info->category_description; ?> </textarea>
</div>
<div class="form-group">
<label>Category Status</label>
<div class="radio">
<?php
if ($category_info->publication_status == 1)
{
?>
<label>
<input type="radio" name="publication_status" id="optionsRadios1" value="1" checked>Published
</label>
<?php
}
else
{
?>
<label>
<input type="radio" name="publication_status" id="optionsRadios1" value="1" >Published
</label>
<?php
}
?>
</div>
<div class="radio">
<?php
if ($category_info->publication_status == 0)
{
?>
<label>
<input type="radio" name="publication_status" id="optionsRadios2" value="0" checked>Unpublished
</label>
<?php
}
else
{
?>
<label>
<input type="radio" name="publication_status" id="optionsRadios2" value="0">Unpublished
</label>
<?php
}
?>
</div>
</div>
<button type="submit" class="btn btn-default">Submit </button>
<button type="reset" class="btn btn-default">Reset </button>
</form>
</div>
</div>
<!-- /.row (nested) -->
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
At the end of Super_Admin::update_category() you call redirect('super_admin/edit_category');. This makes an HTTP redirection to the URL /super_admin/edit_category that is handled by method Super_Admin::edit_category().
This method requires an argument but no one is given in the URL (this is the warning). The cause of the notice is obvious: since edit_category() is called without any argument, its parameter $category_id is not set.
You should call redirect() like this (for example):
redirect('super_admin/edit_category/'.$category_id);
I'm currently working on codeigniter. I want to insert records in database. But when I click submit button, the records in database has not been saved.
Please help me. Thank you.
Here is my view (payroll_add.php):
<?php echo form_open('home/saveEmpPayroll',array('class'=>'form-horizontal'));?>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Select Employee</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<?php echo form_dropdown('empid', $dropdown, '', 'class="form-control" id="empid"'); ?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Basic Salary</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" id="emp_salary" name="emp_salary" class="form-control">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Total Bus Income for the week</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" id="total_income" name="total_income" class="form-control">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">SSS Bracket</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<select class="form-control" id="emp_SSS">
<option value="<?php if (isset ($_POST['SSS_bracket'])) {
echo $_POST ['$SSS_bracket'];}?>"></option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">SSS Deduction</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" id="SSSdeduction" name="SSSdeduction" class="form-control" >
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Philhealth Bracket</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<select class="form-control" id="emp_Philhealth">
<option value="<?php if (isset ($_POST['Philhealth_bracket'])) {
echo $_POST ['$Philhealth_bracket'];}?>"></option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Philhealth Deduction</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" id="Philhealthdeduction" name="Philhealthdeduction" class="form-control" >
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Type of Allowance</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" class="form-control" readonly="readonly" value="Meal Allowance">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Total Allowance</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" id="emp_allowance" name="emp_allowance" class="form-control" >
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
<button type="submit" class="btn btn-success" name="emp_submit" id="emp_submit">Submit</button>
</div>
</div>
</form>
Here is my controller: (home.php)
public function viewAddEmployeePayrollForm() {
$this->load->model('Model_payroll');
$data = array();
$data['dropdown'] = $this->Model_payroll->get_dropdown();
$this->load->view('imports/header');
$this->load->view('imports/menu');
$this->load->view('payroll/payroll_add', $data);
}
public function saveEmpPayroll() {
$this->load->model('Model_payroll');
$p = new Model_payroll();
$p->emp_id = $this->input->post('empid');
$p->basic_salary = $this->input->post('emp_salary');
$p->meal_allowance = $this->input->post('emp_allowance');
$p->SSS_bracket = $this->input->post('emp_SSS');
$p->SSS_deduction = $this->input->post('SSSdeduction');
$p->Philhealth_bracket = $this->input->post('emp_Philhealth');
$p->Philhealth_deduction = $this->input->post('Philhealthdeduction');
$p->bus_income = $this->input->post('total_income');
$result = $p->saveEmployeePayroll();
if (!$result) {
echo mysqli_error($result);
}
else {
redirect('home/goViewEmpPayroll', 'refresh');
}
}
Here is my model (model_payroll.php):
<?php
class Model_payroll extends CI_Model {
public $emp_id;
public $basic_salary;
public $meal_allowance;
public $SSS_bracket;
public $SSS_deduction;
public $Philhealth_bracket;
public $Philhealth_deduction;
public $ot_rate;
public $ot_total;
public $bus_income;
public function getEmployeePayroll() {
$this->db->select("*");
$this->db->from('tbl_payroll');
$this->db->join('employees', 'employees.empnum = tbl_payroll.emp_id');
$query = $this->db->get();
return $query->result();
}
public function addEmployeePayroll() {
$this->db->where('emp_id', $this->emp_id);
$query = $this->db->insert('tbl_payroll', $this);
return $query;
}
public function saveEmployeePayroll() {
if (isset($this->emp_id)) {
$query = $this->updateEmployeePayroll();
}
else {
$query = $this->addEmployeePayroll();
}
return $query;
}
public function updateEmployeePayroll() {
$this->db->where('emp_id', $this->emp_id);
$query = $this->db->update('tbl_payroll', $this);
return $query;
}
public function get_dropdown() {
$result = $this->db->select('empnum, name')->get('employees')->result_array();
$dropdown = array();
foreach($result as $r) {
$dropdown[$r['empnum']] = $r['name'];
}
return $dropdown;
}
}
But there's nothing records has been saved in mysql database after clicking submit button. What am I doing wrong?
In your method
public function updateEmployeePayroll() {
$this->db->where('emp_id', $this->emp_id);
$query = $this->db->update('tbl_payroll', $this);
return $query;
}
$query = $this->db->update('tbl_payroll', $this);
$this contain CI data as well. please do not use like that please see below for example.
https://ellislab.com/codeigniter/user-guide/database/active_record.html
$this->db->update();
Generates an update string and runs the query based on the data you supply. You can pass an array or an object to the function. Here is an example using an array:
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
// Produces:
// UPDATE mytable
// SET title = '{$title}', name = '{$name}', date = '{$date}'
// WHERE id = $id
You must have to form_validation:
public function saveEmpPayroll() {
if($this->form_validation->run()){
$this->load->model('Model_payroll');
$p = new Model_payroll();
$p->emp_id = $this->input->post('empid');
$p->basic_salary = $this->input->post('emp_salary');
$p->meal_allowance = $this->input->post('emp_allowance');
$p->SSS_bracket = $this->input->post('emp_SSS');
$p->SSS_deduction = $this->input->post('SSSdeduction');
$p->Philhealth_bracket = $this->input->post('emp_Philhealth');
$p->Philhealth_deduction = $this->input->post('Philhealthdeduction');
$p->bus_income = $this->input->post('total_income');
$result = $p->saveEmployeePayroll();
if (!$result) {
echo mysqli_error($result);
}
else {
redirect('home/goViewEmpPayroll', 'refresh');
}
}
else{
redirect('controller/viewAddEmployeePayrollForm');
}
}