1
votes

Hi i am new to laravel i am having trouble in inserting data into database and throwing error as

"count(): Parameter must be an array or an object that implements Countable"

i want to add attendence details of all the registred employees in the databse

controller

    public function Attendence() 
    {
        $data=employee_data::all();
        return view('attendence',compact('data'));
    }   

    public function mark_attendence(Request $request)
    {
        $request->validate([
        'date'  =>  'required',
        'is_present'  =>  'required'

        ]);


        $data=$request->all();
        $last_id=employee_data::create($data)->id;
        if (count($request->is_present) >0 )
         {
            # code...
            foreach ($return->is_present as $item => $v)
            {
                $data2=array(
                    'is_present' =>$request->is_present[$item],
                    'date'=> $request->date[$item],
                    'user_id'=>$last_id
                );

            }
        //$data2->save();

    //$employee->save();
    //$employee->employee_data()->create([]);



        return redirect('/index')->with('succ','Attendence Added Successfully');
    }

Blade output:

enter image description here

Submit Id First Name Last Name DateOfJoining post Remark @foreach( $data as $row ) {{ $row->id }} {{ $row->first_name }} {{ $row->last_name }} {{ $row->date_joining }} {{ $row->post }} &nbsp &nbsp Present &nbsp &nbsp Absent @endforeach Id First Name Last Name DateOfJoining post Remark

Model class employee_attendence extends Model { //

protected $fillable  = array('is_present' ,'date', 'user_id' );
//protected $fillable=[];
public $timemstamps= false ;

public function employee_data(){
        //return $this->hasOne(employee_data::class,'App/employee_data');
    return $this->hasOne('App\employee_data');

}

}

Model2

namespace App;

use Illuminate\Database\Eloquent\Model;

class employee_data extends Model { //protected $fillabel=['first_name','last_name','contact_no','date_joining','post'];

protected $fillable  = array('first_name', 'last_name', 'contact_no' ,'date_joining','post' );
//protected $fillable=[];
public $timemstamps= false ;

public function employee_attendence()
{
        //return $this->hasOne( employee_attendence::class, 'App/employee_attendence');
        return $this->belongsTo('App\employee_attendence');
}

}

2
Is your is_present an array from the view side ? - Akhtar Munir
no <input type="date" value="CURDATE()" name="date[]" style="text-align: left;float: inline-end; margin-left: 850px;" > <td> <label class="form-check-label" for="radio2"> <input type="checkbox" class="form-check-input" id="Present" name="is_present[]" value="Present">&nbsp &nbsp Present </label> <label class="form-check-label" for="radio2"> <input type="checkbox" class="form-check-input" id="Absent" name="is_present[]" value="Absent">&nbsp &nbsp Absent </label></td> - Shruti Menkudle
See my below answer it will work for you. But you have values like Absent and Present So check the conditions accordingly. - Akhtar Munir
its throwing an error:undefined error :return - Shruti Menkudle
Are you marking absent or present each at a time or first you check or uncheck then submit it at once ? - Akhtar Munir

2 Answers

1
votes

As far as I understand. You might have is_present will be an array format from view like name="is_present[]". If this your case then the below code will work fine. if not then you can't use count() if input is not an object or array

public function mark_attendence(Request $request)
{
    for($i = 0; $i < sizeof($request->is_present); $i++)
    {
        $emp = new employee_attendence();

        if($request->is_present[$i] == "Present")
        $emp->is_present = "Present";
        if($request->is_present[$i] == "Absent")
        $emp->is_present = "Absent";

        $emp->date = now();
        $emp->user_id = $request->user_id[$i];
        $emp->save();
    }
    return redirect('/index')->with('succ','Attendence Added Successfully');
}
1
votes

In your Add Attendance method try this this worked for me

for($i = 0; $i < sizeof($request->is_present); $i++)
    {
        $emp = new employee_attendence();

        if($request->is_present[$i] == "Present")
            $emp->is_present = "Present";
        if($request->is_present[$i] == "Absent")
            $emp->is_present = "Absent";

            $emp->date = $request->date; 
            $emp->user_id = $request->user_id[$i];
            $emp->save();
    }