1

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 }}     Present     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');
}

}

7
  • Is your is_present an array from the view side ? Commented May 11, 2020 at 9:53
  • 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> Commented May 11, 2020 at 10:33
  • See my below answer it will work for you. But you have values like Absent and Present So check the conditions accordingly. Commented May 11, 2020 at 10:36
  • its throwing an error:undefined error :return Commented May 11, 2020 at 10:37
  • Are you marking absent or present each at a time or first you check or uncheck then submit it at once ? Commented May 11, 2020 at 10:42

2 Answers 2

1

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');
}
Sign up to request clarification or add additional context in comments.

3 Comments

ErrorException Undefined variable: return
I have just showed a simple logic for the loop. You have to insert $data2 into a table below.
$data2 ? into the table
1

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();
    }

1 Comment

If you remember I gave you that answer too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.