【问题标题】:Laravel Relation : add an id from other tablesLaravel 关系:从其他表中添加一个 id
【发布时间】:2020-10-30 18:05:37
【问题描述】:
public function show(Request $request, $slug = null)
{
    $products = Food::where("slug_food", $slug)->get();
    $relateds = Food::orderBy("created_at", "desc")->paginate(5);
    $ratings = Rating::where("food_id", $products[0]->id)->count();
    $comments = Rating::where("food_id", $products[0]->id)->get();

    if(count($products)>0){
        return view("user.product", compact("products", "relateds", "ratings", "comments"));
    }else{
        abort(404);
    }
    
}

public function addComment(Request $request, $slug = null)
{    
    $products = Food::where("id", $slug)->get();
    $ratings = new Rating();
    $ratings->comment = $request->input('comment');
    $ratings->food_id = $products;
    Auth::user()->ratings()->save($ratings);
       
}

错误代码: 无效的日期时间格式:1366 不正确的整数值:'[]' 列 efoodies.ratings.food_id 在第 1 行(SQL:插入到 ratingscommentfood_iduser_idupdated_at, created_at) 值 (dsa, [], 2, 2020-10-30 09:07:37, 2020-10-30 09:07:37))

我想插入一个餐桌食品的 id 并将其插入到评级表中

【问题讨论】:

    标签: laravel


    【解决方案1】:

    根据您的查询,我建议 FOOD 中的 ID 是您的 slug。

    如果定义中是这种情况,您可以直接设置 Food 实体本身

    
    public function addComment(Request $request, Food $food)
    {    
        $ratings = new Rating();
        $ratings->comment = $request->input('comment');
        $ratings->food_id = $food->id;
        Auth::user()->ratings()->save($ratings);
           
    }
    

    如果不是..

    
    public function addComment(Request $request, $slug = null)
    {    
        $food = Food::where("slug", $slug)->firstOrFail();
        $ratings = new Rating();
        $ratings->comment = $request->input('comment');
        $ratings->food_id = $food->id;
        Auth::user()->ratings()->save($ratings);
           
    }
    

    PS.. 使用最后一行 Auth::user()->ratings()->save($ratings) 听起来不太好,而是我会设置

    public function addComment(Request $request, $slug = null)
    {    
        $food = Food::where("slug", $slug)->firstOrFail();
        $ratings = new Rating();
        $ratings->comment = $request->input('comment');
        $ratings->food_id = $food->id;
        $ratings->user_id = Auth::user()->id;
        $ratings->save();
    }
    
    

    在你的路线上小心使用?

    Route::post('welcome/product/{slug?}', [LoginController::class, 'addComment'])->name("addComment"); 
    

    这个蛞蝓?可能会导致空 slug 以及逻辑如何失败。 它必须没有?以确保只有有效的请求才会包含该 $slug。

    Route::post('welcome/product/{slug}', [LoginController::class, 'addComment'])->name("addComment"); 
    

    如果这个 $slug 等于不是“slug”的 Food 字段,只需在我的答案的第一行用正确的名称替换“slug”。

    【讨论】:

    • 我使用你的第一个答案,我得到一个错误,即 food_id 列不能为空,当我使用你的第二个答案时,我得到一个错误 undifined offset 0
    • @RizziqIbrahim 您可以尝试使用第二个 + 我为替换 Auth 用户行所做的 PS 评论。如果 Rating 表具有与用户的关系的 user_id ,这将起作用?
    • 确定 $slug 属性是 ID 、 slug 或 slug_food 的名称 + 你收到了什么错误
    • SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列'slug'(SQL:select * from food where slug is null limit 1)
    • 你能告诉我你的路线从通往那个行动的路线吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    相关资源
    最近更新 更多