【问题标题】:Return view doesn't take variable in Laravel 5.4返回视图在 Laravel 5.4 中不包含变量
【发布时间】:2017-06-14 18:35:45
【问题描述】:

也许是个愚蠢的问题,但我的控制器中有这个,它显示了图像上传的表单,提交表单后应该让我返回另一个视图。

在另一个视图中,我传递了所有图像的变量,但我仍然有

未定义变量:图像

这就是我的控制器中的内容

// Display all Images
public function images()
{
    $images = Images::paginate(3);
    return view('images', compact('images'));
}

// Display image upload form
public function imageCreate()
{
    return view('create');
}

// Submit and store image
public function imageStore( Request $request )
{
    $image = new Images([
      'caption' => $request['caption'],
      'name' => $request['caption'],
      'path' => 'uploads/noimage.png',
      'hits' => 1,
      'added_on' => '2017-08-08 9:00:00'
    ]);

    $image->save();
    return view('images', compact('images'));
}

我认为这是images.blade.php

@foreach($images as $image)

   <a href="{{ URL::to('image/'.$image->slug) }}"><img class="thumbnail block" src="{{ '../'.$image->path }}"></a>

@endforeach

那么,如果我在返回视图语句中发布变量,为什么它是未定义的?

更新:视图中的 dd($image) 返回

Images {#234 ▼
  #primaryKey: "id"
  #table: "images"
  +timestamps: false
  +fillable: array:6 [▶]
  #connection: null
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: true
  #attributes: array:7 [▼
    "caption" => "dasdsadsad"
    "name" => "dasdsadsad"
    "path" => "uploads/noimage.png"
    "hits" => 1
    "added_on" => "2017-08-08 9:00:00"
    "slug" => "dasdsadsad-7"
    "id" => 144
  ]
  #original: array:7 [▶]
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #events: []
  #observables: []
  #relations: []
  #touches: []
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

更新 2:路线

Route::get('images', 'HomeController@images'); 
Route::get('create',['as'=>'create','uses'=>'HomeController@imageCreate']); 
Route::post('create',['as'=>'store','uses'=>'HomeController@imageStore']); 

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    问题就在这里:

    compact('images'));
    

    但你的变量是$image。所以改成:

    compact('image'));
    

    然后再试一次。并更改 foreach() 变量名称,如:

    @foreach($image as $img)
    ...
    $img->slug
    $img->path
    

    解释:

    包含数据的变量是$image,而您从控制器传递的变量是compact('images'))。有一个额外的s

    passing-data-from-controller-to-views

    【讨论】:

    • 但我有@foreach($images as $image) 需要images
    • 那又怎样?包含数据的变量是$image,而您从控制器传递的变量是compact('images'))。变量名中有一个额外的s
    • 等一下。当执行imageStore 并将视图返回到images() 时,images() 不是传递了$images 变量吗?
    • 如果我改成$images = new Images([,我就有Trying to get property of non-object
    • 使用 compact 您必须传递与变量名称相同的文本。
    【解决方案2】:

    此行出错

    return view('images', compact('images'));
    

    你的变量名是$image,你通过$images

    用这个替换上面的行

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

    public function imageStore( Request $request )
    {
        $image = new Images([
          'caption' => $request['caption'],
          'name' => $request['caption'],
          'path' => 'uploads/noimage.png',
          'hits' => 1,
          'added_on' => '2017-08-08 9:00:00'
        ]);
        $imagePath='uploads/noimage.png';
        $image->save();
        return view('images')->with('image',$imagePath);
    }
    

    你会得到这样的路径

    <a href="{{ URL::to('image/'.$image->slug) }}">
     <img class="thumbnail block" src="{{url('/'.$image)}}">
    </a>
    

    【讨论】:

    • 感谢您的回答。已经尝试过了,但我在另一个答案下的 cmets 中提到了错误。
    • @Ivan 我已经更新了我的答案.. 试试这个不起作用然后我会给出另一个想法
    • 你把compact('images')改成compact('image')了吗?
    • 是的,我确实改了。
    • 好的,现在你试试这个{{dd($image[0])}}{{dd($image[0]-&gt;path)}} 在视图中
    【解决方案3】:

    Laravel 假定当您使用紧凑函数时,您传递的变量与您在路由中传递的变量命名相同。例如:

    route::get('/customer/{customer_id}', 'CustomerController@show');

    因此,在您的控制器中,当您使用视图返回数据时,您需要执行以下操作:

    return view('customer', ['customer_name' =&gt; $customer-&gt;name]);

    那么您应该可以在您的视图中引用它,例如:

    {{$customer_name}}

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 2021-09-23
    • 2017-04-08
    • 1970-01-01
    • 2018-11-14
    • 2017-10-28
    相关资源
    最近更新 更多