一尘不染

在 null 上调用成员函数 count()

php

当我要添加数据时,会出现错误“调用成员函数 count() on null”

看法

                @foreach ($data as $row)
                <tr>
                  <td>{{ $row->customer}}</td>
                  <td>{{ $row->vessel}}</td>
                  <td>{{ $row->scopejob}}</td>
                  <td>{{ $row->pergantian_sparepart}}</td>
                  <td>{{ $row->teknisi}}</td>
                  <td>{{ $row->tahun}}</td>
                  <td>{{ $row->keterangan}}</td>
                  <td>{{ $row->images->count()}}</td>
                  <td>{{ $row->files->count()}}</td>
                  <td>
                    <a href={{route('data.images',$row->id)}} type="button" class="btn btn-info m-1">FOTO</a>
                    <a href={{route('data.files',$row->id)}} type="button" class="btn btn-info m-1">FILE</a>
                    <a href="/show_rekap/{{ $row->id}}" class="btn btn-warning m-1">EDIT</a>
                    <a href="#" class="btn btn-danger delete m-1" data-id="{{ $row->id}}" data-customer="{{ $row->customer}}" >DELETE</a>
                  </td>
                </tr>
                @endforeach

控制器rekap

    public function insert_rekap(Request $request) {
        // dd($request->all());
        $this -> validate($request,[
            'customer'=>'required',
            'vessel'=>'required',
            'scopejob'=>'required',
            'pergantian_sparepart'=>'required',
            'teknisi'=>'required',
            'tahun'=>'required',
            'keterangan'=>'required'
        ]);
        $data = rekap::create($request->all());
        if($request->hasFile("images")){
            $images=$request->file("images");
            foreach($images as $image){
                $imageName=time().'_'.$image->getClientOriginalName();
                $request['rekap_id']=$data->id;
                $request['image']=$imageName;
                $image->move(public_path("/images_rekap"),$imageName);
                Image_rekap::create($request->all());

            }if($request->hasFile("files")){
                $files=$request->file("files");
                foreach($files as $file){
                    $fileName=time().'_'.$file->getClientOriginalName();
                    $request['rekap_file_id']=$data->id;
                    $request['file']=$fileName;
                    $file->move(\public_path("/rekap_file"),$fileName);
                    file_rekap::create($request->all());
                }
            }
        }
        return redirect()->route('rekap')->with('success','Data Berhasil Ditambahkan');
    }

但是数据成功输入数据库,我应该采取什么步骤来纠正错误


阅读 95

收藏
2022-07-23

共1个答案

一尘不染

错误告诉您imagesfiles两个变量都为空,看起来您没有使用急切加载

快速而肮脏的方法是使用可选的助手

<td>{{ optional($row->images)->count()}}</td>
<td>{{ optional($row->files)->count()}}</td>

如果您只需要计数,那么更好的方法是使用雄辩的计数相关模型

// controller
$data = rekap::withCount(['images', 'files'])
  // other selecting rules
  //->where ...
  ->get();
return view('view_name', ['data'=>$data]);

现在在 $data 的每 $row 中,您都有字段images_countfiles_count

<td>{{ $row->images_count }}</td>
<td>{{ $row->files_count }}</td>

您也可以重命名计数变量,例如rekap::withCount(['images as imagesCount', 'files as filesCount'])

<td>{{ $row->imagesCount }}</td>
<td>{{ $row->filesCount }}</td>
2022-07-23