将变量从控制器传递到Laravel 9中的Blade模板的方法是:

huangapple go评论80阅读模式
英文:

How to pass variable form controller to blade templete in laravel 9

问题

我想在前端显示用户已上传的所有媒体文件,但在 Blade 模板中出现了一个错误“未定义变量”,我尽力按我的知识正确解决了这个问题,但未能解决。

我尝试过 compact() 方法,但它无法解决问题。我想从数据库获取数据并在前端显示,但是从控制器传递到 Blade 模板的变量未能正确传递。

  1. controller.php
  2. public function show()
  3. {
  4. $uploads = media::with('user')
  5. ->where('user_id', '=', auth()->user()->id)
  6. ->latest()
  7. ->get();
  8. return view('dashboard', compact('uploads'));
  9. }
  1. dashboard.blade.php
  2. @foreach ($uploads as $media)
  3. <div class="p-6 flex space-x-2">
  4. <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">
  5. <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" />
  6. </svg>
  7. <div class="flex-1">
  8. <div class="flex justify between items-center">
  9. <div>
  10. <span class="text-gray-800">{{ $media->user->name }}</span>
  11. <small class="ml-2 text-sm text-gray-600">{{ $media->created_at->format('j M Y, g:i a') }}</small>
  12. </div>
  13. </div>
  14. <p class="mt-4 text-lg text-gray-900">{{ $media->media }}</p>
  15. </div>
  16. </div>
  17. @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.

将变量从控制器传递到Laravel 9中的Blade模板的方法是:

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.

  1. controller.php
  2. public function show()
  3. {
  4. $uploads = media::with(&#39;user&#39;)
  5. -&gt;where(&#39;user_id&#39;, &#39;=&#39;, auth()-&gt;user()-&gt;id)
  6. -&gt;latest()
  7. -&gt;get();
  8. return view(&#39;dashboard&#39;, compact(&#39;uploads&#39;));
  9. }
  1. dashboard.blade.php
  2. @foreach ($uploads as $media)
  3. &lt;div class=&quot;p-6 flex space-x-2&quot;&gt;
  4. &lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; class=&quot;h-6 w-6 text-gray-600 -scale-x-100&quot; fill=&quot;none&quot; viewBox=&quot;0 0 24 24&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot;&gt;
  5. &lt;path stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; d=&quot;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&quot; /&gt;
  6. &lt;/svg&gt;
  7. &lt;div class=&quot;flex-1&quot;&gt;
  8. &lt;div class=&quot;flex justify-between items-center&quot;&gt;
  9. &lt;div&gt;
  10. &lt;span class=&quot;text-gray-800&quot;&gt;{{ $media-&gt;user-&gt;name }}&lt;/span&gt;
  11. &lt;small class=&quot;ml-2 text-sm text-gray-600&quot;&gt;{{ $media-&gt;created_at-&gt;format(&#39;j M Y, g:i a&#39;) }}&lt;/small&gt;
  12. &lt;/div&gt;
  13. &lt;/div&gt;
  14. &lt;p class=&quot;mt-4 text-lg text-gray-900&quot;&gt;{{ $media-&gt;media }}&lt;/p&gt;
  15. &lt;/div&gt;
  16. &lt;/div&gt;
  17. @endforeach

答案1

得分: 1

  1. 可能是媒体模型处于大写状态:
    $uploades = Media::with('users')...

或者它的引用未在您的控制器中导入:
use App\Models\Media;

  1. 在控制器中的 return view(...) 之前写上:
    dd($uploades);
    你看到什么结果?
英文:
  1. It may be that the media model is in uppercase :
    $uploades = Media::with(&#39;users&#39;)...

or its reference is not imported in your controller :

use App\Models\Media;

  1. Write before return view(...) in controller :
    dd($uploades);
    What result do you see?

huangapple
  • 本文由 发表于 2023年2月16日 18:24:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定