Laravel 5.2控制器索引返回空白页面

时间:2016-08-03 15:46:57

标签: laravel

我创建了一个简单联系页面的路线。我使用控制器将数据保存在数据库中并将它们显示在同一页面上。当我提交表单时,我得到一个空白页面,但我希望用户留在联系人。我试图传递索引视图,但后来我得到错误。

文件是

route.php

Route::get('contact', 'ContactController@index');

Route::post('contact', 'ContactController@create');

ContactController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Contact;

class ContactController extends Controller
{
   public function index() {
         $contacts = Contact::orderBy('created_at', 'asc')->get();
         return view('/contact', [ 'contacts' => $contacts ]);
}
  public function create(Request $request) {
    $name = $request->input('name');
    $contact = new Contact;
    $contact->name = $name; 
    $contact->save();
    #return view('/contact');
}
}

Contact.php

<?php

Namespace App;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    protected $fillable = ['name'];
}

?>

contact.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Contact page</div>

                <div class="panel-body">
                    {!! Form::open(array('url' => 'contact')) !!}
            {!! Form::label('name', 'Name') !!}
            {!! Form::text('name'); !!}
            {!! Form::submit('Submit'); !!}
            {!! Form::close() !!}
            @if (count($contacts) > 0)
             @foreach ($contacts as $contact) 
            {{ $contact->name }}
                @endforeach
                @endif
        </div>
            </div>
        </div>
    </div>
</div>
@endsection

1 个答案:

答案 0 :(得分:0)

尝试将view('contact', [...])放在views文件夹中并使用view('/contact');代替return back(),您不需要使用斜杠并将create添加到public function create(Request $request) { $name = $request->input('name'); $contact = new Contact; $contact->name = $name; $contact->save(); return back(); } 方法:

$ git pull