Laravel 5 pagination on raw query - laravel

I have a union query from 2 different tables which I have to run through raw query. But I am facing a problem in paginatig the result. Here's what I have done
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Pagination\Paginator;
use Illuminate\Http\Request;
class SiteController extends Controller {
public function index()
{
$result = \DB::select(\DB::raw("UNION query"));
$result_p = Paginator::make($result , count($result), 10);
return view('view_name',compact('result'));
}
}
This gives an error
Call to undefined method Illuminate\Pagination\Paginator::make()
Any help is appreciated.

That class does not contain the method make(). Instead, pass those variables to the constructor.
$result_p = new Paginator($result, $resultsPerPage, $currentPage, $options);

Related

Return value must be of type Laravel 10

I am working on a Laravel 10 project and trying to create a controller that displays records. However, I have run into a problem while attempting to do so :
App\Http\Controllers\StudentController::index(): Return value must be of type Illuminate\Http\Response, Illuminate\View\View returned
I have attached below what I have tried so far:
Student Controller
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\Student;
class StudentController extends Controller
{
public function index(): Response
{
$students = Student::all();
return view ('students.index')->with('students', $students);
}
If I remove the Response class, the code works, but I need to implement the Laravel 10 standard. I am unsure how to solve this issue. Can you please provide a solution?
Routes
Route::resource("/student", StudentController::class);
Laravel utilized all of the type-hinting features available in PHP at the time. However, many new features have been added to PHP in the subsequent years, including additional primitive type-hints, return types, and union types.
Release notes.
If you are using view function, you need to use the Illuminate\View\View class for type-hinting.
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use App\Models\Student;
class StudentController extends Controller
{
public function index(): View
{
$students = Student::all();
return view('students.index')
->with('students', $students);
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\CatalogueModel;
use Illuminate\Contracts\View\View;
class StudentController extends Controller
{
public function index(): View
{
$data['students'] = Student::all();
return view('students.index')->with($data);
}
}

App\Repositories\AdminRepository::getAllAdmins(): Return value must be of type Illuminate\Pagination\Paginator, LengthAwarePaginator returned

I used paginate to display the database data but got the following error
App\Repositories\AdminRepository::getAllAdmins(): Return value must be of type Illuminate\Pagination\Paginator, Illuminate\Pagination\LengthAwarePaginator returned
What namespace should I use to fix it?
This is my AdminRepository.php
<?php
namespace App\Repositories;
use App\Repositories\Interfaces\AdminRepositoryInterface as AdminRepositoryInterface;
use Illuminate\Database\QueryException;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use App\Models\Admin;
use Illuminate\Support\Facades\Hash;
use App\Http\Requests\EditPassword;
use Illuminate\Contracts\View\View;
use App\Http\Requests\CreateAdmin;
use App\Http\Requests\DeleteAdmin;
use App\Http\Requests\EditAdmin;
use App\Services\AdminService;
use Illuminate\Http\Request;
class AdminRepository implements AdminRepositoryInterface
{
public function getAllAdmins(int $count):Illuminate\Pagination\AbstractPaginator
{
return Admin::paginate($count);
}
This is my AdminRepositoryInterface.php
namespace App\Repositories\Interfaces;
use App\Models\Admin;
use Illuminate\Http\RedirectResponse;
interface AdminRepositoryInterface
{
public function getAllAdmins(int $count):Illuminate\Pagination\AbstractPaginator;
The result of the getAllAdmins() function is a type of LengthAwarePaginator because the Laravel paginate function return that type and your function return it. so, you should change the return type to Illuminate\Pagination\LengthAwarePaginator not Paginator.
use Illuminate\Pagination\LengthAwarePaginator;
...
public function getAllAdmins(int $count): LengthAwarePaginator
{
return Admin::paginate($count);
}

Laravel: Error when trying to use Str::limit in views

I am getting this error when trying to use the Str::limit in views
ErrorException
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$body (View: C:\Users\USER\Desktop\laravels\qna\resources\views\questions\index.blade.php)
here is the code
<div class="media-body">
<h3 class="mt-0">{{ $question->title }}</h3>
{{ Str::limit($questions->body, 250) }}
</div>
Here is the controller
namespace App\Http\Controllers;
use App\Models\Question;
use Illuminate\Http\Request;
// use Illuminate\Support\Str;
class QuestionsController extends Controller
{
// use Str;
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$questions = Question::latest()->paginate(5);
return view('questions.index', compact('questions'));
}
...
}
I get his error when "Str" is un-commented
Symfony\Component\ErrorHandler\Error\FatalError
App\Http\Controllers\QuestionsController cannot use Illuminate\Support\Str - it is not a trait
What is the proper method to use Str:: in a view
Write $question->body instead of $questions->body in your view in order to use the object of question not the paginator.
Actually you don't have to use Illuminate\Support\Str in your controller at all , because you use Str class only in your view and it's one of the aliases in laravel , take a look at config/app.php.
By the way … The (use statement) above class only shorten the namespace you must use in your code like so :
use Illuminate\Support\Str;
class QuestionsController extends Controller
{
public function index()
{
Str::limit("Some String");
}
}
But if you don't put this use , your code would be :
class QuestionsController extends Controller
{
public function index()
{
\Illuminate\Support\Str::limit("Some String");
}
}
whereas when we put use statement inside class , it means we are trying to use trait in our class
https://www.php.net/manual/en/language.oop5.traits.php
I don't think you need to add it in your controller, so just add
use Illuminate\Support\Str;
to your model and that should allow you to use it anywhere. And then in your blade you can use
\Illuminate\Support\Str::limit($questions->body, 250)
This is a Laravel solution but I do recommend looking at this thread for a pure PHP answer Limit String Length
string limit with the end of three dots
\Illuminate\Support\Str::limit($clientName,16,'...');

Update error in Laravel: Class 'App\Http\Controllers\App\Category' not found

I am new to laravel and I am trying to update my table using the following code. But receiving the following error
Class 'App\Http\Controllers\App\Category' not found
My Code is
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use App\Category;
class CategoryController extends Controller
{
public function UpdateCategory()
{
$category = App\Category::find(3);
$category->name = 'Women';
$category->save();
}
}
use App\Category;
you already include your model in controller. So you dont need to include once again
$category = Category::find(3);
simply use this
Try this
$category = Category::find(3);
Change
$category = App\Category::find(3);
To
$category = Category::find(3);

Laravel FatalErrorException

I created a new view, route and controller in laravel and when i go to access the view i get an error, all other views are fine.
Error
FatalErrorException in shopsalescontroller.php line 11: syntax error, unexpected 'class' (T_CLASS), expecting ',' or ';'
this is my shop sales controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\shopsales
class shopsalescontroller extends Controller : (line 11)
{
public function index()
{
$storeNum = request('storeNum');
$results = shopsales::where('StoreNumber','=',$storeNum)->get();
return view('shopSales',compact('results'));
}
}
PHP version 5.6.3
Zend engine v2.6.0
Laravel framework version 5.2.45
You missed a semicolon on this line:
use App\shopsales;
Just remove the : (line 11) text from your code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\shopsales
class shopsalescontroller extends Controller {
public function index() {
$storeNum = request('storeNum');
$results = shopsales::where('StoreNumber', '=', $storeNum)->get();
return view('shopSales', compact('results'));
}
}

Resources