【问题标题】:variable in controller not passed to view, using with()控制器中的变量未传递给视图,使用 with()
【发布时间】:2019-06-30 15:14:44
【问题描述】:

我正在尝试将 $image 变量传递给我的视图 (gallerij.blade.php),但它不起作用。我收到一条错误消息:“未定义的变量:图像”。我不知道如何解决这个世界。

我已经在 Stack Overflow 上寻找解决方案,但我找不到任何解决方案。我也看过 Laracast 网站。但我在那里找不到答案。最后,我也请了一个程序员同行,看看能不能找到解决办法,但他也不知道是哪里出了问题。

我的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\gallerijs;

class GalerijController extends Controller
{
    public function Retrieve(){
    $g = gallerijs::find(1);
    $image = chunk_split(base64_encode($g->afbeelding));
    return view('gallerij ')->with($image);
}
}

我的看法:

@extends ('Layout')
@section('title')
    Galerij
@endsection

@section('content')


<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

</head>
<body>

<div id="demo" class="carousel slide" data-ride="carousel">

  <!-- Indicators -->
  <ul class="carousel-indicators">
    <li data-target="#demo" data-slide-to="0" class="active"></li>
    <li data-target="#demo" data-slide-to="1"></li>
    <li data-target="#demo" data-slide-to="2"></li>
  </ul>

  <!-- The slideshow -->
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="{{asset('Pictures/'.$image)}}" alt="Los Angeles" width="1100" height="500">
    </div>
    <div class="carousel-item">
      <img src="{{asset('Pictures/loremipsum2.jpg')}}" alt="Chicago" width="1100" height="500">
    </div>
    <div class="carousel-item">
      <img src="{{asset('Pictures/loremipsum3.jpg')}}" alt="New York" width="1100" height="500">
    </div>
  </div>

  <!-- Left and right controls -->
  <a class="carousel-control-prev" href="#demo" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </a>
  <a class="carousel-control-next" href="#demo" data-slide="next">
    <span class="carousel-control-next-icon"></span>
  </a>
</div>

</body>
</html>



@endsection

我尝试在第一个img src 中使用变量。

希望我已经为您提供了足够的信息,以便您可以帮助我找到解决方案。

【问题讨论】:

标签: php laravel


【解决方案1】:

改变:

return view('gallerij ')->with($image);

到:

return view('gallerij ', compact('image'));

我发现这是在保留变量名称的同时将变量传递给视图的最简洁的方法。

您可以使用with(),但您必须重申变量名(即:-&gt;with('image', $image)

或者您可以定义一个变量数组作为第二个参数传递给视图(即:view('view.template', ['image' =&gt; $image])。

那就是紧凑功能派上用场了。它将使用变量名作为键从变量名列表创建一个数组(即:compact('image') 创建['image' =&gt; $image]

【讨论】:

  • 您好车轮制造商,感谢您的回答。我会使用你所说的,但我被指示使用 with() (家庭作业)。既然我在评论你,你是否也知道为什么我的图像没有从数据库中检索到?谢谢你的回答。
  • $g = gallerijs::find(1); 调用之后,您是否尝试过dd($g) 以查看find 的结果是什么?
  • 嗨,它返回以下内容:gallerijs {#232 ▼ #fillable: array:2 [▶] #connection: "mysql" #table: "gallerijs" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:4 [▶] #original: array:4 [▶] #changes: [] #casts: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #hidden: [] #visible: [] #guarded: array:1 [▶] }
  • 在您看来,您似乎只是想将图像文件的名称作为 $image 获取,以便您可以在图像标签中显示它...您的 $gallerijs 对象是否有照片名称与服务器上的图像相关联?你到底在做什么chunk_split(base64_encode($g-&gt;afbeelding));
  • 既然这是另一个问题,那么第一个问题是什么。你可以在这里看到我关于这个主题的新问题:stackoverflow.com/questions/56826863/…。感谢您到现在为止的帮助!
【解决方案2】:

请用户作为图像数组 喜欢

public function Retrieve(){
    $g = gallerijs::find(1);
    $image = chunk_split(base64_encode($g->afbeelding));
    return view('gallerij ')->with('image',$image);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多