我想要一个简单的程序,显示登录后的用户配置文件。我使用了会话和中间件。但这意味着name属性为null。请帮我解决这个错误。我用过用户模型。属性的值不会从数据库中检索。
刀片代码:
<h1>Profile</h1>
<h2>Welcome Mr/Ms {{$user->name}}</h2>
<a href="{{route('logout')}}">Logout</a>型号:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Models\PremiumModel;
class User extends Authenticatable implements MustVerifyEmail
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $table = 'users';
protected $primaryKey = 'user_id';
protected $timestamp = false;
protected $fillable = [
'name',
'email',
'password',
'type',
'email_verified_at',
'pro_pic',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
function admin()
{
return $this->hasOne('App\Models\admin', 'user_id', 'admin_id');
}
function premium()
{
return $this->hasMany(PremiumModel::class, 'user_id', 'user_id');
}
}控制器代码:
function profile()
{
$user = User::where('user_id',session()->get('logged'))->first();
return view('premium.profile')->with('user',$user);
}在这里,logged是中间件。
发布于 2022-07-01 05:34:28
您可以在用户模型中尝试hasOne:
public function premium()
{
return $this->hasOne(PremiumModel::class, 'user_id', 'user_id');
}因为hasMany返回了一个数组,但是hasOne只返回了一个对象
https://stackoverflow.com/questions/72823978
复制相似问题