即时通讯收到此错误,但我不确定是由于关系还是其他原因?
错误
ErrorException in HasRelationships.php line 487:
Class 'Company' not found
Run Code Online (Sandbox Code Playgroud)
User.php
public function company(){
$this->belongsTo('Company', 'user_id');
}
Company.php
public function user(){
$this->belongsTo('User') ;
}
现在,我的目标是在导航栏中隐藏“创建列表”按钮(如果用户与companies表没有关系)。我知道我可以使用角色或中间件来实现,但是我的朋友给我发送了类似的信息,并告诉我它更容易实现。
if(count($user->company) > 0)
Run Code Online (Sandbox Code Playgroud)
因此,现在我试图找出方法,但仍然无法弄清楚如何解决该错误。
导航视图
@inject('user', 'App\User')
@if(count($user->company) > 0)
<li><a href="{{route('listings.create', [$area])}}">Add listing</a></li>
@endif
Run Code Online (Sandbox Code Playgroud)
///更新
它没有找到类'Company',因为我没有在关系中使用完整的名称空间,但是现在我遇到了这个新错误。
错误
ErrorException in HasAttributes.php line 403:
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
(View: /Users/username/Desktop/laravel/resources/views/layouts/partials/_navigation.blade.php)
(View: /Users/username/Desktop/laravel/resources/views/layouts/partials/_navigation.blade.php)
(View: /Users/username/Desktop/laravel/resources/views/layouts/partials/_navigation.blade.php)
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试为我的用户配置文件更改密码功能,我的所有输入都提交给控制器,但我认为我的功能逻辑可能有问题?
尝试对函数进行转储请求并成功返回转储。但是在围绕验证过程包装验证变量时,不会返回转储。请求重定向回带有表单数据的个人资料页面。
控制器
public function updatePassword(Request $request)
{
$this->validate($request, [
'old_password' => 'required',
'new_password' => 'required|confirmed',
'password_confirm' => 'required'
]);
$user = User::find(Auth::id());
if (!Hash::check($request->current, $user->password)) {
return response()->json(['errors' =>
['current' => ['Current password does not match']]], 422);
}
$user->password = Hash::make($request->password);
$user->save();
return $user;
}
Run Code Online (Sandbox Code Playgroud)
看法
<form method="POST" action="{{ route('update-password') }}">
@csrf
@method('PUT')
<div class="form-group row">
<label for="old_password" class="col-md-2 col-form-label">{{ __('Current password') }}</label>
<div class="col-md-6">
<input id="old_password" name="old_password" type="password" class="form-control" required autofocus>
</div>
</div>
<div class="form-group row">
<label for="new_password" …Run Code Online (Sandbox Code Playgroud)