You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多供应商网站供应商登录SQL列不存在错误求助

Fixing SQLSTATE[42S22] Error for Vendor/Shop Owner Login

Hey there, let's break down and fix this issue step by step! The error only popping up for vendor/shop owners (not regular users) gives us clear clues about where things are going wrong.

1. Root Causes of the Error

From your description and code snippets, two critical issues are triggering this SQL error:

  • Non-existent column in the query: The generated SQL is searching for user_shops in the shops table, but your table should actually use user_id (based on the intended correct code).
  • Syntax mistake in the Login Controller: The line $model = Shop::where('user_id',Auth::user()->$user_id)->first(); has an incorrect property access—Auth::user()->$user_id tries to reference a dynamic variable instead of the actual user ID property id.

2. Step-by-Step Fixes

Correct the Login Controller Code

Replace the problematic line with the proper property reference. You don't need the $user_id variable here; just directly access the authenticated user's id property:

// Wrong code
$model = Shop::where('user_id', Auth::user()->$user_id)->first();

// Corrected code
$model = Shop::where('user_id', Auth::user()->id)->first();

This ensures you're passing the real authenticated user ID to the where clause, and the query will use the valid user_id column instead of the non-existent user_shops.

Verify Your Database Schema

Double-check your shops table:

  • Confirm the column linking shops to users is named user_id (not user_shops). If you added user_shops earlier by mistake, you can safely remove it since it's not needed. Make sure user_id exists and is populated with the correct user IDs for each shop.

Test the Fix

After applying these changes:

  • Log in as a vendor/shop owner to confirm the error is gone.
  • Verify that the system correctly pulls up the associated shop record for the logged-in user.

Extra Checks If Issues Persist

If the error still lingers, try these steps:

  • Clear your app's cache (run php artisan cache:clear and php artisan config:clear if you're using Laravel) to eliminate any cached old query logic.
  • Check middleware or route logic that applies specifically to vendor/shop owner logins—make sure nothing is overriding the query to use user_shops instead of user_id.

内容的提问来源于stack exchange,提问作者Nahid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:11:32