【发布时间】:2020-08-26 19:29:15
【问题描述】:
嗨,我是 laravel 的新手,我在将数据插入数据库并抛出错误时遇到了麻烦
"count(): 参数必须是 实现 Countable 的数组或对象”
我想在数据库中添加所有注册员工的出勤详细信息
控制器
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');
}
刀片输出:
提交 ID 名 姓 入职日期 邮政 评论 @foreach( $data 作为 $row ) {{ $row->id }} {{ $row->first_name }} {{ $row->last_name }} {{ $row->date_joining }} {{ $row->post }}     礼物     缺席 @endforeach ID 名 姓 入职日期 邮政 评论型号 类employee_attendence 扩展模型 { //
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');
}
}
模型2
命名空间应用程序;
使用 Illuminate\Database\Eloquent\Model;
classemployee_data 扩展模型 { //保护 $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');
}
}
【问题讨论】:
-
你的
is_present是视图侧的数组吗? -
没有
-
看我下面的答案,它会为你工作。但是您有
Absent和Present这样的值,因此请相应地检查条件。 -
它抛出一个错误:未定义的错误:返回
-
您是一次标记缺席或出席,还是先选中或取消选中然后立即提交?