我试图将数据库的一个寄存器传递给我的页面标题,在这个模型中我得到了这个错误。
这是我的usercontroller.php:
<?php
namespace App\Http\Controllers;
use Exception;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
class UserController extends Controller{
public function index()
{
$user = User::all();
return view('user.index', ['title' => $user->name, 'user' => $user]);
}
我正在传递 'title' => $user->name 中的值,正如我在 html 代码中使用 $title 定义的那样:
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<title>{{ $title }}</title>
</head>
我将变量设置为可填充在我的用户模型中:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
protected $fillable = [
'name',
'email',
'password',
];
我之前遇到语法错误,当我修复它时,得到了这个新人。
您必须获取唯一的用户