我制作了Post A Ad页面,其中我成功了。但我也希望在另一个视图中看到这些广告ad.blade.php。我愿意在post.blade.php中创建所有帖子,然后在ad.blade.php中访问其数据。
但是当我去ad.blade.php时。它给出了错误 的 ErrorException 未定义的变量:帖子(查看: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
答案 0 :(得分: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')); }