英文:
How to pass variable form controller to blade templete in laravel 9
问题
我想在前端显示用户已上传的所有媒体文件,但在 Blade 模板中出现了一个错误“未定义变量”,我尽力按我的知识正确解决了这个问题,但未能解决。
我尝试过 compact() 方法,但它无法解决问题。我想从数据库获取数据并在前端显示,但是从控制器传递到 Blade 模板的变量未能正确传递。
controller.php
public function show()
{
    $uploads = media::with('user')
        ->where('user_id', '=', auth()->user()->id)
        ->latest()
        ->get();
    return view('dashboard', compact('uploads'));
}
dashboard.blade.php
@foreach ($uploads as $media)
    <div class="p-6 flex space-x-2">
        <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-gray-600 -scale-x-100" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
            <path stroke-linecap="round" stroke-linejoin="round" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" />
        </svg>
        <div class="flex-1">
            <div class="flex justify between items-center">
                <div>
                    <span class="text-gray-800">{{ $media->user->name }}</span>
                    <small class="ml-2 text-sm text-gray-600">{{ $media->created_at->format('j M Y, g:i a') }}</small>
                </div>
            </div>
            <p class="mt-4 text-lg text-gray-900">{{ $media->media }}</p>
        </div>
    </div>
@endforeach
英文:
I want to show all the media files that the user have been uploaded in frontend but in blade template this shows an error Undefined variable, I have done everything right in my knowledge to solve this but it's not resolving.
I have tried compact() method but that can't solve the problem. I want the data from database and show in frontend but the variable from controller to blade template is not passing properly.
controller.php
public function show()
    {
        $uploads = media::with('user')
                ->where('user_id', '=', auth()->user()->id)
                ->latest()
                ->get();
        return view('dashboard', compact('uploads'));
        
    }
dashboard.blade.php
@foreach ($uploads as $media)
    <div class="p-6 flex space-x-2">
        <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-gray-600 -scale-x-100" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
    <path stroke-linecap="round" stroke-linejoin="round" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" />
       </svg>
     <div class="flex-1">
        <div class="flex justify-between items-center">
     <div>
     <span class="text-gray-800">{{ $media->user->name }}</span>
     <small class="ml-2 text-sm text-gray-600">{{ $media->created_at->format('j M Y, g:i a') }}</small>
     </div>
    </div>
  <p class="mt-4 text-lg text-gray-900">{{ $media->media }}</p>
</div>
   </div>
@endforeach
答案1
得分: 1
- 可能是媒体模型处于大写状态:
$uploades = Media::with('users')... 
或者它的引用未在您的控制器中导入:
use App\Models\Media;
- 在控制器中的 
return view(...)之前写上:
dd($uploades);
你看到什么结果? 
英文:
- It may be that the media model is in uppercase :
$uploades = Media::with('users')... 
or its reference is not imported in your controller :
use App\Models\Media;
- Write before 
return view(...)in controller :
dd($uploades);
What result do you see? 
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论