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

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

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.

将变量从控制器传递到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.

controller.php

public function show()
    {
        $uploads = media::with(&#39;user&#39;)
                -&gt;where(&#39;user_id&#39;, &#39;=&#39;, auth()-&gt;user()-&gt;id)
                -&gt;latest()
                -&gt;get();

        return view(&#39;dashboard&#39;, compact(&#39;uploads&#39;));
        
    }
dashboard.blade.php

@foreach ($uploads as $media)
    &lt;div class=&quot;p-6 flex space-x-2&quot;&gt;
        &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;

    &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;

       &lt;/svg&gt;
     &lt;div class=&quot;flex-1&quot;&gt;
        &lt;div class=&quot;flex justify-between items-center&quot;&gt;
     &lt;div&gt;
     &lt;span class=&quot;text-gray-800&quot;&gt;{{ $media-&gt;user-&gt;name }}&lt;/span&gt;

     &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;
     &lt;/div&gt;
    &lt;/div&gt;
  &lt;p class=&quot;mt-4 text-lg text-gray-900&quot;&gt;{{ $media-&gt;media }}&lt;/p&gt;
&lt;/div&gt;
   &lt;/div&gt;
@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:

确定