Laravel 在null上调用成员函数storeAs()

Laravel 在null上调用成员函数storeAs(),laravel,laravel-5,eloquent,Laravel,Laravel 5,Eloquent,我想将id为的图像存储到表中,并将其保存到文件夹中 我有两个表目录和图像。图像表有img\u id,它是目录表中id的外键,控制器中的代码是: $catalog=new Katalog(); $catalog->name=$name; $catalog->picturePath=$name.'/'; $catalog->description=$desc; $catalog->address=$request->input('address'); $catalog->phone_number=$

我想将id为的图像存储到表中,并将其保存到文件夹中

我有两个表目录图像。图像表有
img\u id
,它是目录表中
id
的外键,控制器中的代码是:

$catalog=new Katalog();
$catalog->name=$name;
$catalog->picturePath=$name.'/';
$catalog->description=$desc;
$catalog->address=$request->input('address');
$catalog->phone_number=$request->input('phone_number');
$catalog->email=$request->input('e_-mail');
$catalog->info_holder=$request->input('type');
$catalog->partner\u logo=$logo\u img\u name;
$catalog->short_news=$request->input('short_news');
$catalog->updated_at=Carbon::now();
$catalog->site=$request->input('site');
$catalog->creator\u id=Auth::user()->id;
$catalog->save();
$id=Katalog::where('name',$name)->first()->id;
如果($request->hasFile('images')){
foreach($request->file('images')作为$image){
$img_name=$image->getClientOriginalName();
$request->image->storeAs(('/images/'.$name),$img_name,'images');
图像::创建([
“img_id”=>$id,
'img_name'=>$img_name
]);
}
}
将数据存储到catalogs表后,它返回一个新的
id
,但我想将图像存储到
images
表中,返回的
id
,但无法执行此操作。错误显示在这里
$request->image->storeAs(“/images/.”name),$img_name,'images')


请帮助我,提前感谢。

您无需查询即可从刚创建的对象(Katalog)获取ID

$id=$catalog->id;
而不是

$id = Katalog::where('name', $name)->first()->id;
因为如果有其他同名的Katalog记录,你可能得不到你想要的

注:你可能有一个打字错误,在末尾加上一个“e”

Image::创建([
“img_id”=>$id,
'img_name'=>$img_name
]);
希望这有帮助