Laravel paginate() method doesn't work in index controller method - php

I am a newbie to Laravel development. I have checked the documentation of Laravel for using database pagination. I found that I can use paginate(numbreOfLines) to paginate.
Whenever I write in my controller:
public function index()
{
$users= User::paginate(3);
}
It doesn't work. I don't know why. I am using Laravel 5.4

I believe that you have defined table name in Model User.php file
public function index()
{
$users= User::paginate(3);
return $users;
}

You need to return the results to the view , that's why you get nothing.
public function index()
{
$users= User::paginate(3);
return view('view.blade.php')->with('users', $users);
}

Related

UsersController `User::all()` working but `show` returning `null`

I want to fetch user based on id but it's returning null but User::all() is working correctly.
index and show methods in UsersController :-
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use DB;
class UsersController extends Controller
{
public function __construct()
{
$this->middleware('auth',['except'=>['index','show']]);
}
public function index()
{
$users=User::all();
return view('dev.index')->with('users',$users);
}
public function show(User $user)
{
return view('dev.show')->with('user',$user);
}
}
Route:-
Route::resource('devs','UsersController');
On view I have {{dd($user->name)}} and it's returning null on url public/devs/{dev}.
but working fine on index on url public/devs
This is because you are defining your base route like this:
Route::resource('devs', 'UserController');
This means that laravel will format the show method as follows:
Route::get('devs/{dev}', 'UserController#show');
Laravel will try to solve this dependency doind Implicit Model Binding and given that {dev} doesn't match any of your defined model classes, it will indeed return null.
So to solve it, define this match explicitly doing Explicit Binding. To accomplish this, go to your:
App/Providers/RouteServiceProvider.php
public function boot()
{
parent::boot();
Route::model('dev', App\User::class);
}
So now wherever Laravel reads the {dev} route parameter, will match it with the User model.
You don't initialize users for your entire controller.
Each function uses their own variables
First of all, I would reconfigure your route like so:
Route::get('devs', 'UsersController#show')->name('showUsers')
In your function show I would do the following
public static function show(){
$id = 1;
$users = User::where('id', $id')
return view('dev.show', compact('users'));
}

Laravel Controller and helper file: Undefined variable in view

I have an issue trying to use in my controller a function located in an helper file which is auto-loaded. I already used helper functions in some controllers but this function doesn't work and I don't understand why. I have different resources that use a similar code for the controller index() function for example. So my aim is to make functions that I can use in different controllers in this way.
Here's the error I get: "Undefined variable: articles" in the view file ArticlesIndex.blade.php
Helper function:
function res_index($collection,$viewName,$varName) {
if(!$collection->isEmpty()) {
$collection->take(10);
return view($viewName, compact($varName));
} else {
return 'Nothing';
}
}
And here's my index() function located in the controller:
public function index()
{
$articles = Article::all();
return res_index($articles,'ArticlesIndex','articles');
}
Thanks a lot !
Return value, returned by res_index:
return res_index($articles,'ArticlesIndex','articles');

Edit page with ID in GET does not exist resourcefull controller

I cant seem to get my edit function to work in my resourceful controller. This is my controller:
class UserController extends Controller{
public function index()
{
return view('testindex');
}
public function test(){
return 'test';
}
public function edit(User $user){
return 'test2';
}
public function create(){
return 'test3';
}
}
And my routes:
Route::post('test','UserController#test');
Route::resource('/','UserController');
Which mean that edit should be in the resource controller.
Create works but edit isn't, it gives me a
NotFoundHttpException
This is the form:
Edit
And yes, the variable $id works and is showing in the url.
What am I doing wrong here?
This is because you're not naming the resource i.e.
Route::resource('user', 'UserController');
To get around this you will need to change you route to be:
Route::resource('/', 'UserController', ['parameters' => [
'' => 'user'
]]);
(The above will allow you to keep your urls the same).
Please note that you will have to keep this Route at the bottom of your file.
Hope this helps!

Laravel - Call to undefined method Illuminate\Database\Query\Builder::user()

I am busy with Laravel From Scratch: Updating Records and Eager Loading. I have followed the tut but I am getting this error when trying to add user data in CardsController. I am assuming that I've missed a step in the card user relationship somewhere but I've watched the video 3 times and my database queries for User, Card & Note matches the video exactly.
Is there another step I need to perform after creating the Users table via the migration perhaps?
Error
BadMethodCallException in Builder.php line 2345:
Call to undefined method Illuminate\Database\Query\Builder::user()
CardsController code
<?php
namespace App\Http\Controllers;
use App\Card;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class CardsController extends Controller
{
public function index()
{
$cards = Card::all();
return view('cards.index', compact('cards'));
}
public function show(Card $card)
{
$card = Card::with('notes.user')->get();
return $card;
return view('cards.show', compact('card'));
}
}
Your note model is lacking the relationship definition for it's associated user, it looks like.
You should just be able to add the relationship in the Notes model like this:
public function user()
{
return $this->belongsTo(User::class);
}
That video does have some problems, so I also encountered the same problem.
You should just be able to add the relationship in the Note model like this:
public function user()
{
//return $this->belongsTo(User::class);
return $this->belongsTo('App\User');
}
and in the User model like this:
public function notes()
{
return $this->hasMany(Note::class);
//return $this->belongsTo('App\Note');
}
bless you !

Calling Model Function From Controller in Laravel

I'm new on laravel.
I have functions on my model php. I want to use them in controller and send to view.
This is my example function.
public function select()
{
$users = DB::table('garanti')->get();
}
now I need to use this on controller and view.
In codeigniter I handle it like this:
$data['kategori'] = $this->model->select_s();
$this->load->view('admin/kategori', $data);
If you do
class Post extends Eloquent {
public function select()
{
return DB::table('garanti')->get();
}
}
You can use it in your controller:
$data['kategori'] = with(new Post)->select();
return View::make('admin/kategori')->with('data', $data);
There are in fact other ways of doing this, but static functions are not really testable, so I wouldn't use them in this case.
This is a very good LIVE example about using MVC concept in Laravel. in this scenario the Controller is calling a function from the Model class then the Controller handle the view.Take a look.
http://runnable.com/UnFiFHVGrQh1AAA_/mvc-in-laravel-for-php

Categories