【问题标题】:student/create route shows blank page ,not displaying the insert form学生/创建路线显示空白页,不显示插入表单
【发布时间】:2019-12-27 14:23:19
【问题描述】:

q) 我想在 la-ravel 中插入一个表格,我的版本是 P hp 7 和 la ravel 5.8。我编写了所有代码并运行,但它没有显示表单或任何内容,它显示空白页,并且还创建了迁移数据库和表,但我无法插入,因为表单没有显示在 URL 中.. 这是 URL HTTP://localhost:8000/student/create 你能帮帮我吗?

1) 我试过HTTP://localhost:8000/student/create 但它不起作用

这是视图文件,resources/views/student/create.blade.php

@extends('master')
@section('content')
<div class="row">
    <div class="col-md-12">
        <br />
        <h3 align="center">add data</h3>
        <br />

        @if(count($errors) > 0)
        <div class="alert alert-danger">
            <ul>

                @foreach($errors->all() as $error)
                    <li>{{$error}}</li>
                    @endforeach
            </ul>
        </div>
        @endif
        @if(\Session::has('success'))
        <div class="alert alert-success">
            <p>{{ \Session::get('success')}}</p>
        </div>
        @endif

        <form method="post" action="{{url('student')}}">
            {{csrf_field()}}
            <div class="form-group">
                <input type="text" name="first_name" class="form-control"
                 placeholder="enter the first name" />
            </div>

            <div class="form-group">
                <input type="text" name="last_name" class="form-control"
                 placeholder="enter the last name" />
            </div>

             <div class="form-group">
                <input type="submit" class="btn btn-primary" />
            </div>

        </form>
    </div>
</div>

@endsection

2) 这是控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\student;
use Session;

class StudentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('student.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'first_name' => 'required',
            'last_name' =>  'required'
            ]);
        $student=new student([
            'first_name' => $request->get('first_name'),
            'last_name'  => $request->get('last_name')
            ]);
        $student->save();
        return redirect()->route('student.create')->with('success','data added');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

3) 这是路由/web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/first', function () {
    return view('first');
});


Route::get('/user', function () {
    return view('user');
});


Route::get('/{name}', function ($name) {
    return $name;
});


Route::get('user/{name?}', function ($name="default brace") {
    return $name;
});
//Route::redirect("/Home","/user");


Route::get('/site{site}','youtube@index');

//Route::get("/site","youtube@index");


Route::resource('student/create','StudentController');





output its shows nothing ,only blank page only..

【问题讨论】:

  • 请添加此
    。也请编辑你的 routes/web.php Route::resource('student','StudentController');
  • 请避免提出多个具有相同问题的问题。澄清为什么这个问题与另一个问题不同。

标签: laravel laravel-5.8


【解决方案1】:

改变这一行:

Route::resource('student/create','StudentController');

到这里:

Route::resource('students','StudentController');

并访问此地址:

localhost:8000/students/create

请参阅 Laravel 文档 here

你的master.blade.php也有错别字,一定是@yield('content')(注意ie的位置)。

在您看来,像这样更改表单标签:

<form method="post" action="{{route('students.store')}}">

【讨论】:

  • 我试过了,但是表格不出来。它显示@yeild('content')。仅此而已
  • 您能否将master.blade.php 代码添加到您的问题中?
  • master.blade.php
  • Laravel CRUD
【解决方案2】:

您错误地定义了学生路线资源。你目前有这样的:

Route::resource('student/create','StudentController');

所以当你检查php artisan route:list时,你会得到以下输出:

| GET|HEAD  | student/create/create        | create.create    | App\Http\Controllers\StudentController@create    

如您所见,您已经在路线名称中创建了两次,并且您正在尝试查找student/create

将您的路线资源更改为:

Route::resource('students','StudentController');

现在,当您转到php artisan route:list 时,您的路线将如下所示:

| GET|HEAD  | student/create         | student.create   | App\Http\Controllers\StudentController@create  

现在您可以通过转到student/create 来调用该路由,这将显示您的表单。

【讨论】:

    【解决方案3】:

    问题出在您的资源路径中。它具有索引、创建、存储、显示、编辑、更新和删除路由。当您使用 127.0.0.1:8000/student/create 时,它​​会转到控制器的索引方法,并且由于索引方法中没有任何内容,因此您将得到一个空白页。所以改变你的路线像

    Route::resource('student','StudentController');
    

    现在您可以访问该页面了

    127.0.0.1:8000/student/create
    

    并将您的表单操作更改为

    <form method="post" action="{{route('student.store')}}">
    

    【讨论】:

    • 我的错,我在定义路线时使用了students。应该是student
    • @linda 您在主刀片中拼错了yield。这是yield 而不是yeild
    • 好的,谢谢,现在它显示表单但提交后,显示 404|notfond 并且未在表中插入数据
    • 您是否像我在回答中所说的那样更改了表单操作?
    • 路线 [student.create] 未定义。
    【解决方案4】:

    请添加这个

    还请编辑您的路线/web.php
    Route::resource('student','StudentController');

    更多详情请访问此链接 Laravel - Route::resource vs Route::controller

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 2020-04-27
      • 2018-01-07
      • 2022-09-27
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      相关资源
      最近更新 更多