I want to do pagination, but its not working Method Illuminate\Database\Eloquent\Collection::links does not exist - laravel

Controller:
this is the whole controller code
<?php
namespace App\Http\Controllers;
use App\Exam_sched;
use App\Subject;
use App\Batch;
use Session;
use Illuminate\Http\Request;
class ExamSchedController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
class ExamSchedController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$exam_scheds= Exam_sched::paginate(3);
return view('examschedule',compact('exam_scheds'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$exam_scheds=Exam_sched::all();
$subjects=Subject::all();
$batches=Batch::all();
return view('examschedule', compact('exam_scheds','subjects','batches'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
"date"=>"required",
"subject_id"=>"required",
"batch_id"=>"required",
"roomNo"=>"required",
"startTime"=>"required",
"endTime"=>"required"
]);
// $rules= array(
// "date"=>"required",
// "subject_id"=>"required",
// "batch_id"=>"required",
// "roomNo"=>"required",
// "startTime"=>"required",
// "endTime"=>"required"
// );
// $this->validate($request, $rules);
$exam_sched= new Exam_sched;
$exam_sched->date=$request->date;
$exam_sched->subject_id=$request->subject_id;
$exam_sched->batch_id=$request->batch_id;
$exam_sched->roomNo=$request->roomNo;
$exam_sched->startTime=$request->startTime;
$exam_sched->endTime=$request->endTime;
$exam_sched->save();
Session::flash("message","New Schedule has been added");
return redirect('/examschedule');
}
/**
* Display the specified resource.
*
* #param \App\Exam_sched $exam_sched
* #return \Illuminate\Http\Response
*/
public function show(Exam_sched $exam_sched)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Exam_sched $exam_sched
* #return \Illuminate\Http\Response
*/
public function edit($id)
// public function edit()
{
$exam_sched = Exam_sched::find($id);
$subjects=Subject::all();
$batches=Batch::all();
return view('editschedule',compact('exam_sched','subjects','batches'));
// return view('examschedule');
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Exam_sched $exam_sched
* #return \Illuminate\Http\Response
*/
// public function update(Request $request, Exam_sched $exam_sched)
public function update(Request $request, $id)
{
$exam_sched= Exam_sched::find($id);
$rules= array(
"date"=>"required",
"subject_id"=>"required",
"batch_id"=>"required",
"roomNo"=>"required",
"startTime"=>"required",
"endTime"=>"required"
);
$this->validate($request, $rules);
$exam_sched= Exam_sched::find($id);
$exam_sched->date=$request->date;
$exam_sched->batch_id=$request->batch_id;
$exam_sched->subject_id=$request->subject_id;
$exam_sched->roomNo=$request->roomNo;
$exam_sched->startTime=$request->startTime;
$exam_sched->endTime=$request->endTime;
$exam_sched->save();
Session::flash("message","Schedule has been updated!");
return redirect('/examschedule');
}
/**
* Remove the specified resource from storage.
*
* #param \App\Exam_sched $exam_sched
* #return \Illuminate\Http\Response
*/
public function delete($id)
{ $exam_sched= Exam_sched::find($id);
$schedToRemove=Exam_sched::find($id);
$schedToRemove->delete();
// Session::flash("message","Successfully Deleted!");
return redirect('/examschedule')->with('success','Data Deleted');
}
}
Route:
Route::get('/examschedule', 'ExamSchedController#index');
View Blade:
<tbody>
#if($exam_scheds->count())
#foreach($exam_scheds as $exam_sched)
<tr class="tbody">
<td>{{$exam_sched->date}}</td>
<td>{{$exam_sched->batch->name}}</td>
<td>{{$exam_sched->subject->name}}</td>
<td>{{$exam_sched->roomNo}}</td>
<td>{{$exam_sched->startTime}}</td>
<td>{{$exam_sched->endTime}}</td>
<td>{{$exam_sched->created_at->diffForHumans()}}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
{{ $exam_scheds->links() }}
Method Illuminate\Database\Eloquent\Collection::links does not exist.
Please help me ,I'm new in laravel
thanks in advance

Ok, now I see the problem.
In index method of controller you use
$exam_scheds= Exam_sched::paginate(3);
but in create method you use:
$exam_scheds=Exam_sched::all();
and you use same view in those 2 methods.
Of course when you use all() method there is no pagination so you cannot use links then in view.
So probably you should change in create method to also paginate same as in index method.

Related

When I submit my data to the text field it is not submitting

This is my Postcontroller code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index($id)
{
return "It's working".$id;
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
return $request->all();
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
return "Show Controller ".$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)
{
//
}
public function contact(){
$people=['Snehal','Swarna','Rhitu','Mashuk','Sajid'];
return view('contact',compact('people'));
}
public function show_post($id,$name,$password){
return view('post',compact('id','name','password'));
}
}
myroute https://i.stack.imgur.com/qLr0b.jpg
My Create view https://i.stack.imgur.com/DfIft.jpg
For your form action, it's suggested to use route() function, like below:
<form method="POST" action="{{ route('posts.store') }}">
You are using resource for /posts. You should rewrite the action form to Ahmad Karimi's answer.
Additionally, you can use dd($request->all()); to see if your form field is submitting to the controller.

Laravel, Show(), Edit (), update functions not working

In the code below methods show, edit update are not working.
<?php
namespace App\Http\Controllers\admins;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\contact;
use Image;
use Auth;
use Storage;
use File;
class ContactController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$contact = Contact::orderby('created_at', 'desc')->paginate(5);
//$agent=Agent::orderby('id','desc')->paginate(5);
return view('admin.messages.index', ['contacts' => $contact]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return back()->with('success', 'Message can only be created by Users end.');
}
/** * Display the specified resource.
*
* #param \App\contact $contact
* #return \Illuminate\Http\Response
*/
public function show(contact $contact)
{
dd(['contact' => $contact]);
//return back()->with('success','Message Contents Are Already Shown');
}
/**
* Show the form for editing the specified resource.
*
* #param \App\contact $contact
* #return \Illuminate\Http\Response
*/
public function edit(contact $contact)
{
return view('admin.messages.edit', compact('contact'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\contact $contact
* #return \Illuminate\Http\Response
*/
public function update(Request $request, contact $contact)
{
dd($request);
}
/**
* Remove the specified resource from storage.
*
* #param \App\contact $contact
* #return \Illuminate\Http\Response
*/
public function destroy(contact $contact)
{
return back()->with('success', 'Message history can not be Deleted. ');
}
}
Assuming you are using a slug in a route like contacts/{ slugĀ }
public function show(contact $contact)
{
dd(['contact' => $contact]);
//return back()->with('success','Message Contents Are Already Shown');
}
Receives an id not a contact... you are initializing/declaring in the function parameter as contact thats why it somehow gets casted to a contact... but it's an id you should do something like:
public function show($id)
{
$contact = Contact::findOrFail($id);
dd(['contact' => $contact]);
//return back()->with('success','Message Contents Are Already Shown');
}

Laravel 5.5, Default route for index not working for resource controller

I have a resource Controller named CmsPagesController,
class CmsPagesController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return 'something';
// $pages=CmsPages::all();
// return view('backend.pages.cms.list')->with('pages',$pages);
}
public function list(){
return '123';
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('backend.pages.cms.add');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'slug' => 'required',
'description'=> 'required'
]);
$Page=new CmsPages;
$Page->name= $request->input('name');
$Page->slug= $request->input('slug');
$Page->description= $request->input('description');
$Page->copyright= $request->input('copyright');
$Page->keywords= $request->input('keywords');
$Page->save();
return redirect('/admin/pages')->with('success','Page Added Successfully');
}
/**
* 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)
{
//
}
}
My Add function and store function is working perfect, I can render add page and can store data to database but my index function is not working, I have placed a sample return statement but that is not executed as well. what am I missing
Following is my route
Route::resource('pages','CmsPagesController');
I was facing the same issue, that is index function wasn't returning anything, not even any error.
I had a UsersController made as a resource.
And route as:
Route::resource('users', 'UsersController');
When you try to hit the following URL
...public/users/index
it won't return anything.
It worked for me when I tried
...public/users
the index function is automatically called. You don't have have to write /index in the the url.

Laravel - empty result by trying to show a specific thread

My database is filled with data and showing all data works. But if I try to show a specific thread only, I get an empty result and I can't figure out why.
web.php
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/threads', 'ThreadsController#index');
Route::get('/threads/{threads}', 'ThreadsController#show');
ThreadsController#index works, but ThreadsController#show doesn't work.
ThreadsController.php
<?php
namespace App\Http\Controllers;
use App\Thread;
use Illuminate\Http\Request;
class ThreadsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$threads = Thread::latest()->get();
return view('threads.index', compact('threads'));
//return $threads;
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param \App\Thread $thread
* #return \Illuminate\Http\Response
*/
public function show(Thread $thread)
{
//return view('threads.show', compact('thread'));
return $thread;
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Thread $thread
* #return \Illuminate\Http\Response
*/
public function edit(Thread $thread)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Thread $thread
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Thread $thread)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Thread $thread
* #return \Illuminate\Http\Response
*/
public function destroy(Thread $thread)
{
//
}
}
This is an example from laracast tutorial: link
Why this example doesn't work? I can't find any typo in my code.
My version:
php artisan --version
Laravel Framework 5.4.32
Rename your show route to the following:
Route::get('/threads/{thread}', 'ThreadsController#show');
You are loading a Thread object, and not a Threads object.

ERR_TOO_MANY_REDIRECTS in Laravel

I am learning Laravel. I have a problem with the very basics. I get a ERR_TOO_MANY_REDIRECTS.
my routes.php:
Route::get('/','WelcomeController#index');
index function:
public function index(){
return view('welcome');
}
The URL gets an additional part, I don't know why.
From http://localhost/dev.todoparrot.com/public to http://localhost/dev.todoparrot.com/public/home
<?php
namespace todoparrot\Http\Controllers;
use Illuminate\Http\Request;
use todoparrot\Http\Requests;
use todoparrot\Http\Controllers\Controller;
class WelcomeController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index(){
return view('welcome');
}
public function contact(){
echo "we are in contacts";
return view('welcome');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* 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)
{
//
}
}

Resources