可以在 blade 视图中使用 eloquent 模型吗?
can use eloquent model in blade view?
例如,这是我们称为仪表板的简单 blade 视图,我们的 class 模型是“用户”
像下面的代码一样在 blade 中使用它是否有效?第 1,12 行
{{use App\Models\User;
$user= User::where('id', '1234')->first();}}
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Dashboard') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
{{ $user->name }}
</div>
</div>
</div>
</div>
你把事情弄糊涂了,你应该将变量传递给控制器中的视图,然后在视图中使用它们(从第 12 行开始)
要在视图中使用该变量,您应该在控制器中传递它:即:
publix function index(){
$user = User::find(1);
return view('index',['user'=>$user]); //'user' is the name of the variable in the view and $user the value that variable will take
}
那么你可以使用{{$user->id}}
“使用 App\Models\User;”语法只能用于控制器或 PHP 类,如果您确实需要在视图中使用实际模型(例如在循环中),您也可以实现!
@foreach(App\Models\User::all() as $user)
<!-- html code -->
@endforeach
<!-- OR -->
{{ App\Models\User::find(104)->name }}
希望对您有所帮助!
您可以像在任何其他 .php 文件中一样在 .blade.php 文件中导入 类:
@php
use App\Models\User;
$user= User::where('id', '1234')->first();
@endphp
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Dashboard') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
{{ $user->name }}
</div>
</div>
</div>
</div>
在这种特殊情况下,虽然这样做没有什么好处,但在视图代码中进行数据库查询通常不是一个好主意,因为视图不应该负责检索模型。这是控制器或从控制器调用的助手的责任。
您应该在控制器和模型中定义所有逻辑。
并将变量中的数据传递给 blade 文件。
在那里你可以使用所有数据
@foreach(....)
@endforeach
例如,这是我们称为仪表板的简单 blade 视图,我们的 class 模型是“用户” 像下面的代码一样在 blade 中使用它是否有效?第 1,12 行
{{use App\Models\User;
$user= User::where('id', '1234')->first();}}
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Dashboard') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
{{ $user->name }}
</div>
</div>
</div>
</div>
你把事情弄糊涂了,你应该将变量传递给控制器中的视图,然后在视图中使用它们(从第 12 行开始)
要在视图中使用该变量,您应该在控制器中传递它:即:
publix function index(){
$user = User::find(1);
return view('index',['user'=>$user]); //'user' is the name of the variable in the view and $user the value that variable will take
}
那么你可以使用{{$user->id}}
“使用 App\Models\User;”语法只能用于控制器或 PHP 类,如果您确实需要在视图中使用实际模型(例如在循环中),您也可以实现!
@foreach(App\Models\User::all() as $user)
<!-- html code -->
@endforeach
<!-- OR -->
{{ App\Models\User::find(104)->name }}
希望对您有所帮助!
您可以像在任何其他 .php 文件中一样在 .blade.php 文件中导入 类:
@php
use App\Models\User;
$user= User::where('id', '1234')->first();
@endphp
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Dashboard') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
{{ $user->name }}
</div>
</div>
</div>
</div>
在这种特殊情况下,虽然这样做没有什么好处,但在视图代码中进行数据库查询通常不是一个好主意,因为视图不应该负责检索模型。这是控制器或从控制器调用的助手的责任。
您应该在控制器和模型中定义所有逻辑。 并将变量中的数据传递给 blade 文件。
在那里你可以使用所有数据
@foreach(....)
@endforeach