douhuang1973 2018-10-29 10:44
浏览 8
已采纳

CRUD - 概述

I succeeded the step "create" barring the overview. My recordings are saved on PhpMyAdmin but not on my page index.blade.php I don't understand why ? I made several refreshments even so.

Here is my file AdminController.php

class AdminController extends Controller
{
    public function index(){
        /*$devis = Devis::all()->sortByDesc("created_at");
        return view('admin.home', compact('devis'));*/
        $students = Student::first()->paginate(5);
        return view('student.index', compact('students'))
                  ->with('i', (request()->input('page',1)-1)*5);
    }

    public function create()
    {
        return view('student.create');

    }

    public function store(Request $request)
    {
        $request->validate([
                'firstname' => 'required',
                'lastname' => 'required'
        ]);
        Student::create($request->all());
        return redirect()->route('student.index')
                    ->with('success', 'save');
    }

}

In my folder VIEWS with another folder which is Student which has 2 files:

Index.blade.php

@extends('student.template.master')

@section('left_menu')
    @include('student.template.left_menu', array('current' => 'home'))
@endsection

@section('content')
  <div class="px-content">
    <div class="page-header">
      <div class="row">
        <div class="col-md-4 text-xs-center text-md-left text-nowrap">
          <h1><i class="px-nav-icon ion-android-apps"></i>List </h1>
        </div>
        <hr class="page-wide-block visible-xs visible-sm">
        <!-- Spacer -->
        <div class="m-b-2 visible-xs visible-sm clearfix"></div>
      </div>
    </div>
    <div class="row">
      <div class="panel">
        <div class="panel-body">
          <div class="table-responsive">
            <table class="table">
              <a class="btn btn-sm btn-success" href="{{ route('student.create') }}">Create</a>
              <thead>
                <tr>
                  <th>Firstname</th>
                  <th>Lastname</th>
                </tr>
                </thead>
                @foreach($students as $item)
                <tr>
                   <td> {{$item->firstname}}</td>
                   <td> {{$item->lastname}} </td>
                   <td> <a href="/valider/{{$item->id}}" class="btn btn-danger"><span class="left ion-close"></span></a></td>
                </tr>
                @endforeach
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
@endsection

And create.blade.php

@extends('student.template.master')

@section('left_menu')
    @include('student.template.left_menu', array('current' => 'home'))
@endsection

@section('content')
  <div class="px-content">
    <div class="page-header">
      <div class="row">
        <div class="col-md-4 text-xs-center text-md-left text-nowrap">
          <h1><i class="px-nav-icon ion-android-apps"></i>Devis</h1>
        </div>
        <hr class="page-wide-block visible-xs visible-sm">
        <!-- Spacer -->
        <div class="m-b-2 visible-xs visible-sm clearfix"></div>
      </div>
    </div>
    <div class="row">
      <div class="panel">
        <div class="panel-body">
          <div class="table-responsive">
            <form class="panel-body" action="{{route('student.store')}}" method="POST">
              @csrf
              <fieldset class="form-group">
                <label for="form-group-input-1">Firstname</label>
                <input type="text" name="firstname" class="form-control" id="form-group-input-1">
              </fieldset>
              <fieldset class="form-group">
                <label for="form-group-input-1">Lastname</label>
                <input type="text" name="lastname" class="form-control" id="form-group-input-1" >
              </fieldset>

              <a href="{{route('student.index')}}" class="btn btn-primary pull-right">Back</a>
              <button type="submit" class="btn btn-sm btn-primary">Submit</button>

            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
@endsection

Do you have an idea please ? I am really stuck.

  • 写回答

2条回答 默认 最新

  • duankua3620 2018-10-29 10:52
    关注

    first() returns an instance of the class Student, you can't paginate on it. change

    public function index(){
        /*$devis = Devis::all()->sortByDesc("created_at");
        return view('admin.home', compact('devis'));*/
        $students = Student::first()->paginate(5);
        return view('student.index', compact('students'))
                  ->with('i', (request()->input('page',1)-1)*5);
    }
    

    to

    public function index(){
        /*$devis = Devis::all()->sortByDesc("created_at");
        return view('admin.home', compact('devis'));*/
        $students = Student::paginate(5);
        return view('student.index', compact('students'))
                  ->with('i', (request()->input('page',1)-1)*5);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境