【问题标题】:Angular - "Object of class Illuminate\Database\Eloquent\Builder could not be converted to stringAngular - “类 Illuminate\Database\Eloquent\Builder 的对象无法转换为字符串
【发布时间】:2019-11-29 06:53:50
【问题描述】:

我正在开发一个以 Angular 7 作为前端,Laravel 5.8 作为后端的项目。我想要的是,当 id = 1 的用户(管理员)登录时,他会看到一切。但是当其他用户登录时,他只能看到有他的 user_id 的数据。

Laravel:后端

   public function indexSmsmo()
{
if(Auth::user()->id == 1)
     $smsmos = Smsmo::all();
else 
 $smsmos = Smsmo::where('user_id',Auth::user()->id);
return $smsmos;               
}

我有用户表和其他表。其他表中有 user_id。

auth.service.ts

import { environment } from '../../environments/environment.prod';
import { User } from '../models/user';

@Injectable({
providedIn: 'root'
})
export class AuthService {

public currentUser: User;
private readonly apiUrl = environment.apiUrl;
private environmentService: EnvironmentService;

constructor(
private http: HttpClient,
private router: Router) 
{     
}

onLogin(user: User): Observable<User> {

const request = JSON.stringify(
  { email: user.email, password: user.password }
);

return this.http.post(this.loginUrl, request, httpOptions)
  .pipe(
   map((response: User) => {
  // Receive jwt token in the response
  const token: string = response['access_token'];
  // If we have a token, proceed
  if (token) {
    this.setToken(token);
    this.getUser().subscribe();
  }
  return response;
  }),
  catchError(error => this.handleError(error))
   );
  }

在这之后我们有登录组件

login.component.ts

  ngOnInit() {
  document.body.className = 'hold-transition login-page';
  $(() => {
  $('input').iCheck({
   checkboxClass: 'icheckbox_square-blue',
   radioClass: 'iradio_square-blue',
   increaseArea: '20%' /* optional */
  });
});

//  Set the return url
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/home';
this.authService.onLogout();
}

onSubmit(loginForm): void {
this.spinnerService.show();
this.authService.onLogin(this.user).subscribe(
  (response) => {      

this.notify.success('Done, you have successfully logged in.', {timeout:2000, position: "rightTop"})        
this.router.navigate([this.returnUrl]);       
},
(error) => {
this.spinnerService.hide();
this.error = error.error;
}
);
 // Clear form fields
 loginForm.reset();
 }  

当我使用 userId 1 登录时,一切正常。但是当我使用其他 userId 登录时,我遇到了这个错误:

httpErrorResponse 消息:“类 Illuminate\Database\Eloquent\Builder 的对象无法转换为字符串

如果用户登录并且 user_id 为 1,他应该看到所有内容,但如果不是 1,他应该看到 user_id 等于登录 ID 的数据

我该如何解决这个问题?

【问题讨论】:

    标签: angular laravel api


    【解决方案1】:

    在您的index 方法中,如果用户是id === 1,您将返回一个集合,因为您使用了all()

    如果用户不是id === 1,你只是返回一个builder 对象,因为你没有完成查询:

    $smsmos = Smsmo::where('user_id',Auth::user()->id);
    

    应该是

    $smsmos = Smsmo::where('user_id',Auth::user()->id)->first(); // or get() if you want a collection
    

    【讨论】:

    • 我想要一个集合,所以我使用了: $smsmos = Smsmo::where('user_id',Auth::user()->id)->all();但是错误还是来了。
    • 抱歉 - 没想到您需要收藏。评论应该是get(),而不是全部。 get() 将为您工作并消除错误。所以$smsmos = Smsmo::where('user_id',Auth::user()-&gt;id)-&gt;get();
    猜你喜欢
    • 2016-05-13
    • 2020-12-01
    • 2020-05-13
    • 2022-01-24
    • 2017-03-15
    • 2016-01-01
    • 2015-09-23
    • 2018-06-16
    • 2019-06-27
    相关资源
    最近更新 更多