【发布时间】: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']);
【问题讨论】: