首页 > 解决方案 > 来自公共类助手的 Laravel 更新表不起作用

问题描述

我想在我的公开课上更新我的表。但它抛出错误方法更新不存在。这是我的课

public static function Update($id, $city){
    $hotel_name= hotel_info::all()->where('id', $id)->pluck('hotel_name');
    $user_id = auth()->user()->id;
    $plan_hotel_db = plan_hotel::where('travel_id', $user_id)->where('plan_city_id', $city)->get();

    $plan_hotel_db->hotel_name = $hotel_name;

    $plan_hotel_db->update();

    return $plan_hotel_db;
}

标签: laravellaravel-5eloquent

解决方案


你应该得到你的第一个元素$plan_hotel_db,因为它将返回一个数据数组。要解决此问题,请将您的代码更改为此。

public static function Update($id, $city){
    $hotel_name= hotel_info::all()->where('id', $id)->pluck('hotel_name');
    $user_id = auth()->user()->id;
    $plan_hotel_db = plan_hotel::where('travel_id', $user_id)->where('plan_city_id', $city)->first();

    $plan_hotel_db->hotel_name = $hotel_name;

    $plan_hotel_db->update();

    return $plan_hotel_db;
}

或者如果你想更新它的所有记录,那么你应该迭代$plan_hotel_db.


推荐阅读