5

I have a controller

 public function store(Request $request)
{if ($request->input('asc')){
                $image = PropertyUser::where('user_id', '=', Auth::user()->id)->get();
                foreach($image as $property)
                {
                    $id = $property->property_id;
                }
                $image_ = Image::where('property_id', $id)->sortBy('description')->get();
                return redirect('settings/photos');

How can i redirect with the $image_ variable and display it in my view file

@foreach ($image_ as $images)
            <div class="image-warp"><img src="{{$images->filename}}"
                                         style="width:100px;height:100px;"><br/><span style="color: #1b1e21">{{$images->description}}</span>
            </div>
        @endforeach
1

8 Answers 8

4

You can use the compact function to pass it to your view and reference it by the name.

return redirect('folder.name', compact('variableName');
return redirect()->route('folder.name', [$image]);
2
  • it returns back with a blank page May 21, 2018 at 10:01
  • Sorry try pointing to a route return redirect()->route('/images', [$image]);
    – bgh
    May 22, 2018 at 13:04
4

You can also send variable using below syntax also

return view('viewfile')->with('card',$card)->with('another',$another);

You can send data using redirect method. Those data will store inside Session Class.

return redirect('url')->with('message',$message);

like below

Session::get('variableName');
Session::get('message');
3

You should try this:

public function store(Request $request)
{if ($request->input('asc')){
                $image = PropertyUser::where('user_id', '=', Auth::user()->id)->get();
                foreach($image as $property)
                {
                    $id = $property->property_id;
                }
                $image_ = Image::where('property_id', $id)->sortBy('description')->get();
                return view('yourfolder.yourviewfile',compact('image_'));

Updated Answer

use Redirect;

public function store(Request $request)
{if ($request->input('asc')){
                $image = PropertyUser::where('user_id', '=', Auth::user()->id)->get();
                foreach($image as $property)
                {
                    $id = $property->property_id;
                }
                $image_ = Image::where('property_id', $id)->sortBy('description')->get();

                Redirect::to('settings/photos?image_='. $image_);
11
  • it returns back with a blank page May 21, 2018 at 10:01
  • please display your code after update with my answer May 21, 2018 at 10:04
  • if ($request->input('asc')){ $image = PropertyUser::where('user_id', '=', Auth::user()->id)->get(); foreach($image as $property) { $id = $property->property_id; } $image_ = Image::where('property_id', $id)->sortBy('description')->get(); return redirect('settings.photos', compact('image_')); May 21, 2018 at 10:06
  • @AdokiyeIruene: Please replace return redirect('settings.photos', compact('image_')); to return view('settings.photos', compact('image_')); May 21, 2018 at 10:10
  • it's still blank May 21, 2018 at 10:19
3

you can try with below code

        return view('settings/photos')->with(['image' =>  $image_]);
2
  • it returns back with a blank page May 21, 2018 at 9:59
  • from above line I have given here a demo page address. place your page address instead of ('settings/photos'). then in that page check with <?php dd($image);?> you may get all images data May 21, 2018 at 10:02
2

Send an array of variables to your view:

return view('folder.viewfile', array(
    'image_' => $image_,
    'someother_variable' => $somevar,
));
2
  • it returns back with a blank page May 21, 2018 at 10:02
  • What is the name of your view? And where is it stored? Kindly replace the path in the above code. In the example above, the path is /resources/views/folder/viewfile.blade.php so in the controller it becomes folder.viewfile
    – Saurabh
    May 21, 2018 at 11:41
2

Why you're using variable like this $image_ , you can use it simply like this $image or $whatEver

return view('folder.viewfile', compact('image'));

And now you can use this variable on view file as $image.

2
  • It returns back with a blank page May 21, 2018 at 9:58
  • Hey Adokiye lruene, I think your query Image::where('property_id', $id)->sortBy('description')->get() It is not working properly, Can you please show me, what its return? May 21, 2018 at 10:41
2

Do that in your controller function:

$request->session()->flash('order_id', $order_id);

And in view simply:

{{Session::get('order_id')}}
1

i just found myself in the same proble and solve with the help of you guys :)

on a complet example mi code ended like this:

mi FormCreate.php view recive an id from the previus page , so the url with out mask is "FormumariosCreate/16" in this example where the id is = 16 :

form:

<form method="post" action="{{ route('FormulariosStore2', $Formularios->idFormularios ) }}"> // is importan add the id on the rute 
          @csrf

<input type="text" name="textoPregunta" required="required" />

<button href="" class="btn btn-primary pull-right">Crear Nueva Pregunta</button>
</form>

rute in web.php:

Route::get('/FormulariosStore2/{idFormularios}', 'HomeController@FormulariosStore2')->name('FormulariosStore2');
Route::post('/FormulariosStore2/{idFormularios}', 'HomeController@FormulariosStore2')->name('FormulariosStore2');

and controler:

   public function FormulariosStore2(Request $request,$id )
    {
    $validatedData = $request->validate([
'textoPregunta' => 'required|max:255',
     ]);

$ProyectoPreguntasF=ProyectoPreguntas::create($validatedData);

$Formularios = Formularios::findOrFail($id); 

return redirect()->route('FormulariosCreate', $Formularios)->with('success','la operacion fue correcta.');
    } 

then it redirect to the rute "FormulariosCreate" with the id as a normal link with the id ,i hope it can add some content to the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.