Laravel- 使用变量重定向并将其显示在视图文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50445945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:43:53  来源:igfitidea点击:

Laravel- redirect with variable and display it in the view file

phplaravellaravel-5

提问by Adokiye Iruene

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

如何使用$image_变量重定向并将其显示在我的视图文件中

@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

采纳答案by AddWeb Solution Pvt Ltd

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

回答by bgh

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

您可以使用 compact 函数将其传递给您的视图并按名称引用它。

return redirect('folder.name', compact('variableName');
return redirect()->route('folder.name', [$image]);

回答by Sibasankar Bhoi

you can try with below code

你可以试试下面的代码

        return view('settings/photos')->with(['image' =>  $image_]);

回答by Rajesh kumar

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

为什么要使用这样的变量 $image_ ,您可以像 $image 或 $whatEver 一样简单地使用它

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

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

现在您可以在视图文件中将此变量用作 $image。

回答by Sourav

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

回答by Saurabh

Send an array of variables to your view:

将变量数组发送到您的视图:

return view('folder.viewfile', array(
    'image_' => $image_,
    'someother_variable' => $somevar,
));

回答by claus vargas

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 :

我刚刚发现自己遇到了同样的问题,并在你们的帮助下解决了:)

在一个完整的示例中,mi 代码以这样结束:

mi FormCreate.php 视图从 previus 页面接收一个 id,所以没有掩码的 url 是“FormumariosCreate /16" 在这个例子中,id 是 = 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:

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

然后它重定向到带有 id 的规则“FormulariosCreate”作为带有 id 的普通链接,我希望它可以为答案添加一些内容

回答by Tahasin

Do that in your controller function:

在您的控制器功能中执行此操作:

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

And in view simply:

并简单地认为:

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