多供应商网站供应商登录SQL列不存在错误求助
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_shopsin theshopstable, but your table should actually useuser_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_idtries to reference a dynamic variable instead of the actual user ID propertyid.
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(notuser_shops). If you addeduser_shopsearlier by mistake, you can safely remove it since it's not needed. Make sureuser_idexists 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:clearandphp artisan config:clearif 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_shopsinstead ofuser_id.
内容的提问来源于stack exchange,提问作者Nahid

