You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.1 KiB

3 months ago
<?php
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
3 months ago
use App\Models\User;
3 months ago
use Illuminate\Http\Request;
3 months ago
use Illuminate\Support\Facades\Validator;
3 months ago
class CompanyController extends Controller
{
/**
3 months ago
* 显示用户列表页面(有公司名称并且报名审核通过的用户)
3 months ago
*/
public function search()
{
3 months ago
// 查询有公司名称并且报名审核通过的用户
$users = User::whereNotNull('company_name')
->where('company_name', '!=', '')
->whereHas('courseSigns', function ($query) {
$query->where('status', 1); // 审核通过
})
3 months ago
->select('id', 'username', 'company_name', 'company_id')
3 months ago
->orderBy('id', 'desc')
->get();
3 months ago
$totalCount = $users->count();
return view('company_search', compact('users', 'totalCount'));
3 months ago
}
/**
* 更新用户公司信息
*/
public function updateUserCompany(Request $request)
{
$validator = Validator::make($request->all(), [
'user_id' => 'required|integer|exists:users,id',
'company_name' => 'required|string',
]);
if ($validator->fails()) {
return response()->json([
'code' => 400,
'msg' => $validator->errors()->first()
], 400);
}
try {
$user = User::find($request->user_id);
if (!$user) {
return response()->json([
'code' => 404,
'msg' => '用户不存在'
], 404);
}
$user->company_name = $request->company_name;
$user->company_id = null; // 设置 company_id 为 null
$user->save();
return response()->json([
'code' => 200,
'msg' => '更新成功',
'data' => $user
]);
} catch (\Exception $e) {
return response()->json([
'code' => 500,
'msg' => '更新失败: ' . $e->getMessage()
], 500);
}
3 months ago
}
}