jcadima
8/23/2017 - 10:17 PM

Get Row Count

Get Row Count


https://laracasts.com/discuss/channels/general-discussion/eloquent-how-to-get-the-row-count

<?php

public function store(Request $request) {
  // get row count from Tasks
  $tasks_count = Task::count() ;
  // if we have less than 20 tasks, store a new task and redirect
  if ( $tasks_count < 20  ) { 
    // dd( $request->all()  ) ;
    $this->validate( $request, [
        'task'       => 'required',
        'project_id' => 'required'
    ]) ;

    $post = Task::create([
        'project_id' => $request->project_id,
        'task'       => $request->task,
        'priority'   => $request->priority
    ]);

    // ->route is defined in web.php
    Session::flash('success', 'Task Created') ;
    return redirect()->route('task.show') ; 
  }
  // else redirect with Toastr message 
  else {
    Session::flash('success', 'Please delete some tasks, Demo max tasks: 20') ;
    return redirect()->route('task.show') ;	        
  }
 

}