vibin
vibin

Reputation: 29

How to insert values to table 'users' in laravel 5.2?

I want to insert employee details username,area to the table 'users'.

I have the following codes of CreateEmployeeController and

createemployee.blade.php view file.

When I click on the menu Create Employee is will shows the following error

QueryException in Connection.php line 662: SQLSTATE[42000]: Syntax error or access violation: 1066 Table/alias: 'users' non unique (SQL: select * from users inner join users on users.id = users.users_id where users.deleted_at is null)

Controller file :

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\AdminController;
use App\CreateEmployee;
use App\Employee;
use App\Users;




class CreateEmployeeController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

 public function addemployee()
    {

        $employee = CreateEmployee::all();

$employee =CreateEmployee::join('users','users.id','=','users.users_id')->get();

        return view('app.admin.employee.employee',compact('employee','users'));
    }


    public function employeesave(Request $request)
    {

        $title = 'Add Employee';  

        $employee = new Employee();
        $employee->name=$request->employee_name;
        $employee ->area = $request->area;
        $employee->save();
        Session::flash('flash_notification', array('level' => 'success', 'message' => 'employee created successfully'));
        return Redirect::action('Admin\CreateEmployeeController@addemployee');

        }


    public function updateemployee(Request $request)
    {
        Employee::where('id',$request->id)->update(array('name'=>$request->employee_name,'area'=>$request->area));

        Session::flash('flash_notification', array('level' => 'success', 'message' => 'shop details updated successfully'));
        return Redirect::action('Admin\CreateEmployeeController@addemployee',array('id' => $request->id));
    }

 public function editemployee($id)
    {
        $employee = Employee::where('id',$id)->get();


       return view('app.admin.employee.editemployee',compact('employee'));
   }
       public function deleteemployee($id)
    {

        $employee =  Employee::where('id',$id)->get();

        return view('app.admin.employee.delete',compact('employee'));
    }


    public function deleteconfirms($id)
    {



         $employee = Employee::where('id',$id)->delete();






         Session::flash('flash_notification', array('level' => 'success', 'message' => 'customer deleted successfully'));
         return Redirect::action('Admin\CreateEmployeeController@addemployee');



    }

    public function destroy($id)
    {
        //
    }
}
//view file
@extends('app.admin.layouts.default')


{{-- Web site Title --}}
@section('title') {{{ trans('site/user.register') }}} :: @parent @stop

@section ('styles')
@parent 
<style type="text/css">
</style>
 @stop

{{-- Content --}}
@section('main')
@include('utils.vendor.flash.message')
    <div class="row">
        <div class="page-header">
            <h2>Add Employee</h2>
        </div>
    </div>

    <div class="container-fluid">
        <div class="row">


                        @include('utils.errors.list')


                        <form class="form-horizontal" role="form" method="POST" action="{{ URL::to('admin/addemployee') }}">
                            <input type="hidden" name="_token" value="{{ csrf_token() }}">




                                <div class="col-sm-12">
                             <div class="form-group">
                                <label class="col-md-2 control-label">Employee Name</label>

                                <div class="col-md-2">
                                    <input type="text" class="form-control" name="employee_name"
                                          required>
                                </div>
                            </div>
                            </div>


                               <div class="col-sm-12">

                                 <div class="form-group">


                                            <label class="col-md-2 control-label" for="religion">Password</label>

                                            <div class="col-md-2"> 

                                                <input type="password" class="form-control"   placeholder="Password"  name="password" id="password" data-parsley-trigger="change"  data-parsley-required="true" data-parsley-minlength="6" data-parsley-maxlength="14" required> 


                            {!! $errors->first('cpassword', '<label class="control-label" for="cpassword">:message</label>')!!}



                                            </div>

                                        </div>

                             </div>

                             </div> 



                             <div class="row">

                                 <div class="col-sm-12">

                                     <div class="form-group">

                                            <label class="col-md-2 control-label" for="caste">Confirm Password</label>

                                            <div class="col-md-2"> 

                                                 <input type="password" class="form-control" placeholder="Confirm Password" name="password_confirmation" id="password_confirmation"  data-parsley-trigger="change"  data-parsley-required="true"  data-parsley-equalto="#password" data-parsley-minlength="6" data-parsley-maxlength="14" required>

                             {!! $errors->first('password_confirmation', '<label class="control-label" for="password_confirmation">:message</label>')!!}

                                            </div>

                                        </div>

                                 </div>

                             </div>                           

                             <div class="row">

                                 <div class="col-sm-12">


                                 <div class="form-group">
                                <label class="col-md-2 control-label">Area</label>

                                <div class="col-md-2">
                                    <input type="text" class="form-control" name="area"
                                          required  placeholder="Area">
                                </div>
                            </div>
                            </div>
                            </div>



                            <div class="form-group">
                                <div class="col-md-2 col-md-offset-2">
                                    <button type="submit" class="btn btn-primary">
                                        Add
                                    </button>
                                </div>
                            </div>

                        </form>
                        </div>
                        </div>

            <div class="invoice-content">

             <div class="table-responsive">

           <div class="col-md-offset-2">

            <table class="table table-invoice">

                <thead>

                    <tr>

                       <th>Employee Name</th>
                       <th>Area</th>  


                    </tr>

                </thead>

                </div>
                </div>

                <tbody>




                    @foreach($addemployee as $employee)

                    <tr>
                        <td>{{$employee->employee_name}}</td>
                        <td>{{$employee->area}}</td> 

                        <td>
                        <a href="employee/edit/{{$employee->id}}">Edit</a>
                        <a onclick="return confirm('Are you Sure you want to do this Action!'); style.backgroundColor='#84DFC1'; "  href="employee/delete/{{$employee->id}}">delete</a>

                        </td>
                   </tr>

                    @endforeach  

                    @if(!count($employee))

                    <tr><td>NO data found </td></tr>



                    @endif



                </tbody>




                </tbody>



            </table>
                    {{--</div>--}}
                {{--</div>--}}
            {{--</div>--}}
        </div>
    </div>
@endsection

Upvotes: 0

Views: 136

Answers (1)

Saumya Rastogi
Saumya Rastogi

Reputation: 13703

I can't get the full idea about your problem, but as upto me I understand that - You are using same table/column names in your join statement, you can do this as:

$employee = CreateEmployee::join('users', 'create_employees.id','=', 'users.employee_id')->get();

Hope this helps!

Upvotes: 1

Related Questions