【问题标题】:Passing data from one view to another view using Form in Laravel使用 Laravel 中的 Form 将数据从一个视图传递到另一个视图
【发布时间】:2017-09-19 16:00:07
【问题描述】:

我已经成功发布了一个广告页面。但我也想在另一个视图 ad.blade.php 中看到这些广告。我愿意在 post.blade.php 中创建所有帖子,然后在 ad.blade.php 中访问其数据。

但是当我转到 ad.blade.php 时。它给出了错误 错误异常 未定义变量:posts(查看:E:\mobile\resources\views\ad\ad.blade.php)

Ad.blade.php

> @extends('layout.main') 
> 
> 
> @section('content')
> 
>     <!-- products listing -->
>     <!-- Latest SHirts -->
> 
>     <div class="row">
> 
>     @forelse($posts->chunk(20) as $chunk)
> 
>         @foreach($chunk as $post)
> 
>                 <div class="small-3 columns">
>                     <div class="item-wrapper">
>                         <div class="item2">
>                             <div class="img-wrapper">
>                                 <a href="{{route('show.post',$post->id)}}" 
>                       class="button expanded >                                            add-to-cart">
>                                     <span>Add to Cart </span>
>                                 </a>
> 
>                             </div>
> 
> 
>                             <a href="{{route('post', [$post->id])}}">
> 
>                         <span title = "Click her for details" style="font-size: 80%;"> <h3>
>                             {{$post->name}}
>                         </h3></span>
>                             </a>
>                             <h5>
>                                 ${{$post->title}}
> 
>                             </h5>
>                             <p>
>                                 {{$post->price}}
>                             </p>
>                         </div>
>                     </div>
>                 </div>
>                 @endforeach
>                 @empty
>                 <h3>No Shirts</h3>
> 
>                 @endforelse
> 
> 
>     </div>
> 
> 
> 
>     <br>
> 
>     @endsection

FrontController.php

>     public function post(){
> 
> 
>         return view('front.post');
>     }
> 
>     public function StorePost(Request $request)
>     {  
>         $this->validate($request, [
>             'title' => 'required',
>             'name' => 'required',
>             'email' => 'required|email',
>             'contact' => 'required',
>             'model' => 'required',
>             'city' => 'required',
>              'description' => 'required',   ]);
> 
> 
> 
>          $destinationPath = 'public/dist/pictures';
> 
>         if (Input::hasFile('image'))
>         {
>             $file = Input::file('image');
>             $file->move('public/dist/pictures', 
                              $file->getClientOriginalName());
>         }
> 

>         Post::create($request->all());
> 
>         return view('ad.ad',compact('name'));
> 
> 
>     }
> 
> 
>     public function ShowPost($id)
>     {
>         $post=Post::find($id);
> 
>         Post::add($id,$post->name,$post->title,$post->price);
>         return back();
>     }

web.php

> Route::get('post', 'FrontController@post')->name('posts');
> 
>  Route::get('/post/show-post/{id}', 'FrontController@ShowPost')
> ->name('show.post'); 
> 
> Route::post('post', 'FrontController@StorePost')->name('post.store');

Post.blade.php

> @extends('layout.main')
> 
> @section('title','Post An Ad') @section('content')
> 
>     <html>
>     <head>
>         {{--<title>Post An Ad</title>--}}
>         {{--<link rel="stylesheet" href="{{ 
>             asset('public/css/contactUS.css') }}">--}}
>         <link rel="stylesheet" type="text/css" href="{{ 
>            url('/css/contactUS.css') }}" />
> 
>     </head>
>     <body>
> 
>     <div class="row2">
>         <div class="small-6 small-centered columns">
>             <h3 ><b>Post An Ad</b></h3>
> 
>             {!! Form::open(['route'=>'post.store','method'=>'post'])!!}
>             {!! csrf_field() !!}
> 
>             <div class="form-group" >
> 
>                 <div style="display: inline-block;width: 100px;">
>               {{ Form::label('title','Title:')}}</div>
>                 <div style="display: inline-block;width: 420px; 
>                  " class="form-group">
>                {{Form::text('title',null,array('class'=>'form-control'))}}
>                </div>
> 
>             </div>
> 
>             <div class="form-group" >
> 
>                 <div style="display: inline-block;width: 100px;">
>              {{ Form::label('name','Name:')}}</div>
>                  <div style="display: inline-block;width: 420px;" 
>                   class="form-group">
>              {{Form::text('name',null,array('class'=>'form-control'))}}</div>
> 
>             </div>
> 
> 
>             <div class="form-group">
> 
>                 <div style="display: inline-block;width: 100px;">
>               {{ Form::label('email','Email:')}}</div>
>                 <div style="display: inline-block;width: 420px;">
>             {{ Form::text('email',null,array('class'=>'form-control'))}}</div>
> 
>             </div>
> 
> 
>             <div class="form-group">
> 
>                 <div style="display: inline-block;width: 100px;">
>             {{ Form::label('contact','Contact No:')}}</div>
>                 <div style="display: inline-block;width: 420px;">
>           {{ Form::text('contact',null,array('class'=>'form-control'))}}</div>
> 
>             </div>
> 
> 
>             <div class="form-group">
> 
>                 <div style="display: inline-block;width: 100px;">
>             {{ Form::label('model','Model:')}}</div>
>                 <div style="display: inline-block;width: 420px;">
>           {{ Form::text('model',null,array('class'=>'form-control'))}}</div>
> 
>             </div>
> 
> 
>             <div class="form-group">
> 
>                 <div style="display: inline-block;width: 100px;">
>             {{ Form::label('color','Color:')}}</div>
>                 <div style="display: inline-block;width: 420px;">
>             {{ Form::text('color',null,array('class'=>'form-control'))}}</div>
> 
>             </div>
> 
>             <div class="form-group">
> 
>                 <div style="display: inline-block;width: 100px;">
>             {{ Form::label('city','City:')}}</div>
>                 <div style="display: inline-block;width: 420px;">
>            {{ Form::text('city',null,array('class'=>'form-control'))}}</div>
> 
>             </div>
> 
>             <div class="form-group">
> 
>                 <div style="display: inline-block;width: 100px;">
>               {{ Form::label('price','Price:')}}</div>
>                 <div style="display: inline-block;width: 420px;">
>               {{ Form::text('price',null,array('class'=>'form-control'))}}</div>
> 
>             </div>
> 
>             <div class="form-group">
> 
>                 <div style="display: inline-block;width: 100px;">{{ Form::label('description','Description:')}}</div>
>                 <div style="display: inline-block;width: 420px;">{{ Form::textarea('description',null,array('class'=>'form-control'))}}</div>
> 
> 
>                 <div class="form-group" enctype="multipart/form-data">
> 
>                     <div style="display: inline-block;width: 100px;">{{ Form::label('image','Image:')}}</div>
>                     <div style="display: inline-block;width: 420px;">{{ Form::file('image',null,array('class'=>'form-control'
> ))}}</div>
> 
>                 </div>
> 
> 
> 
>                 <div class="form-group">
>                     <button class="btn btn-success">Post</button>
>                 </div>
> 
> 
>             {!! Form::close() !!}
> 
>         </div>
> 
> 
>         </div>
>     </div>
> 
>     </body>
>     </html>
> 
> @endsection

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    您忘记将 Posts 变量传递给您的 ad 视图,请在您的控制器中进行:

        public function StorePost(Request $request)
    {  
        $this->validate($request, [
            'title' => 'required',
            'name' => 'required',
            'email' => 'required|email',
            'contact' => 'required',
            'model' => 'required',
            'city' => 'required',
             'description' => 'required',   ]);
    
    
    
         $destinationPath = 'public/dist/pictures';
    
        if (Input::hasFile('image'))
        {
            $file = Input::file('image');
            $file->move('public/dist/pictures', 
                              $file->getClientOriginalName());
        }
    
    
        Post::create($request->all());
        $posts = Post::all();
    
        return view('ad.ad',compact('name','posts'));
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-23
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 2015-10-27
      相关资源
      最近更新 更多