4

我有一个控制器

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

如何使用$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
4

8 回答 8

4

您也可以使用以下语法发送变量

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

您可以使用重定向方法发送数据。这些数据将存储在会话类中。

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

像下面

Session::get('variableName');
Session::get('message');
于 2018-05-21T09:53:25.373 回答
3

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

return redirect('folder.name', compact('variableName');
return redirect()->route('folder.name', [$image]);
于 2018-05-21T09:44:36.053 回答
3

你可以试试下面的代码

        return view('settings/photos')->with(['image' =>  $image_]);
于 2018-05-21T09:48:09.637 回答
2

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

return view('folder.viewfile', array(
    'image_' => $image_,
    'someother_variable' => $somevar,
));
于 2018-05-21T09:42:53.643 回答
2

你应该试试这个:

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

更新的答案

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_);
于 2018-05-21T09:44:25.223 回答
2

为什么你使用像这样的变量 $image_ ,你可以像这样使用它 $image 或 $whatEver

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

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

于 2018-05-21T09:48:20.950 回答
2

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

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

简单地说:

{{Session::get('order_id')}}
于 2020-04-04T18:50:13.337 回答
1

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

在一个完整的示例中,mi 代码的结尾如下所示:

mi FormCreate.php view recive an id from the previus page ,所以没有掩码的 url 是“FormumariosCreate /16" 在此示例中,其中 id 为 = 16 :

形式:

<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>

web.php 中的规则:

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

和控制器:

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

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

于 2019-08-15T16:09:02.093 回答