简体   繁体   中英

Return view doesn't take variable in Laravel 5.4

Maybe is silly question but I have this in my Controller which is showing the form for image upload and after form is submitted should return me to another view.

On this another view I have passing variable with all images but still I've got

Undefined variable: images

So this is what I have in my Controller

// 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'));
}

And this in my view images.blade.php

@foreach($images as $image)

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

@endforeach

So, why variable is undefined if I posting it in the return view statement?

Update: dd($image) in the view return

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 [▶]
}

Update 2: routes

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

The issue is here:

compact('images'));

but your variable is $image . So change it to:

compact('image'));

and try again. And also change the foreach() variable name like:

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

Explanation:

The variable that contains the data is $image and the one you are passing from controller is compact('images')) . An extra s is there.

passing-data-from-controller-to-views

error int this line

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

your variable name is $image and you pass $images

replace above line with this

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);
}

you get path like this

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

Laravel assumes when you use the compact function that the variable you are passing is named the same as the variable you are passing in the route. For instance:

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

Therefore in your controller when returning data with your view you need to do something like:

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

Then you should be able to reference it in your view like:

{{$customer_name}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM