CRUD-概述

时间:2018-10-29 10:44:35

标签: php laravel

我成功完成了步骤“创建”(除非概述)。 我的录音保存在PhpMyAdmin上,但未保存在页面上index.blade.php我不明白为什么?即使如此,我也做了几番提神。

这是我的文件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');
    }

}

在我的文件夹VIEWS中,另一个文件夹是Student,其中有2个文件:

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

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

请问您有个主意吗?我真的被困住了。

2 个答案:

答案 0 :(得分:3)

first()返回类Student的实例,您无法在其上分页。改变

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 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 :(得分:0)

在Laravel中,当您使用paginate()时,它还带有links()函数。因此,您所需要做的就是在控制器中传递变量,如下所示:

public function index(){
   $students = Student::get()->paginate(5);
    return view('student.index', compact('students'));
}

现在,在index.blade.php中,只需引用links()函数:

{{ $students->links() }}