douhuang1973
douhuang1973
2018-10-29 10:44

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 duankua3620 3年前

    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);
    }
    
    点赞 评论 复制链接分享
  • doujuan2688 doujuan2688 3年前

    In Laravel, when you use paginate(), it also comes with links() function. So all you needed to do was to pass the variable in the controller, as below:

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

    Now, in index.blade.php, just reference the links() function:

    {{ $students->links() }}
    
    点赞 评论 复制链接分享

相关推荐