Laravel OrderBy Method not working with paginate() method - php

Friends I am working on a Laravel Project. I have came across a problem where I am trying to use an OrderBy() method. The error message I get is
* BadMethodCallException*
Method orderBy does not exist.
Here my controller code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Episode;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Carbon;
class EpisodeController extends Controller
{
public function __construct()
{
$this->middleware('admincheck');
}
public function index()
{
$episodes = Episode::paginate(10)->orderBy('episode_no','desc');
return view('episode.index',compact('episodes'));
}
In the index() I have used paginate function and then orderBy function which I think is the source of error but I don't know why? If I use only paginate function I don't get error. And also If I use other functions like latest function after paginate I also get same kind of error. I don't Know what is wrong with my code so please guys help me. thank you.

Use it as
$episodes = Episode::orderBy('episode_no', 'DESC')->paginate(10);

You might need to put "orderby()" in front of the "paginate()" in terms of no function "orderby" for the paginate object.

Related

'Why im still getting this error on laravel web, it says about non-statically method.'

here is the error
Non-static method App\Models\ISModel::DisplayAllUserList() cannot be called statically
here is my code in controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\ISModel;
class ISController extends Controller
{
public function display_all_user_list(){
echo $result = ISModel::DisplayAllUserList();
}
}
Please answer my problem, Thank you!
Welcome to Laravel, eloquent models do not work like constructors you cannot just call a method on a Model instance, you have to define exactly what you want so since you want to return all models you do it like this
ISModel::all()->DisplayAllUserList();
I still think this may cause problems since you are calling a method to a collection but you return all the ISModels like this.

SQL queries from database with Laravel

I'm using Laravel and trying to do an SQL query from my Controller in a public function, but I'm really confused where I would put my table in the argument and if quotes go around the argument. Here is my code
public function selectMethod(){
$results = DB::select('select firstname from people where id = 1');
print_r($results);
return view('pages.selectMethod');
}
table is called people
My .env is configured to my database correctly and I get this error
FatalErrorException in AboutController.php line 90:
Class 'App\Http\Controllers\DB' not found
Thanks !
you should add use Illuminate\Support\Facades\DB;
at the top of your page
for example :
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Show a list of all of the application's users.
*
* #return Response
*/
public function index()
{
$users = DB::table('users')->get();
return view('user.index', ['users' => $users]);
}
}
Your error clearly states: Class 'App\Http\Controllers\DB' not found
Hence just use DB in your class. Add:
use DB;
At the top of the file just below the namespace line.
Also, I would suggest you to use Eloquent for your queries. It will make your life a lot easier and your code a lot beautiful.

Call to undefined method SocialiteProviders\Manager\ServiceProvider::driver()

I am working on api part in Laravel 5.2 and trying to fetch the details from Strava. As mentioned in this link http://socialiteproviders.github.io/providers/strava I have done all the steps. In the controller I wrote a function to pass the access token of the user. This is the function which I am using public function
<?php
namespace App\Http\Controllers\Api\v1;
use Illuminate\Http\Request;
use Auth;
use Socialite;
use App\Models\UserTrackerSettings;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class StravaController extends Controller
{
/**
* Get the user by token
*
* #return \Illuminate\Http\Response
*/
public function getTrackerAccess()
{
$trackerDriver = Socialite::driver('strava');
$getToken = UserTrackerSettings::where('tracker_source_name', 'strava')
->where('user_id', Auth::user()->id)->first();
$access_token = $getToken->access_token;
return Socialite::getUserByToken($access_token);
}
}
But when I run the the link in postman I am getting this error
FatalErrorException in StravaController.php line 23:
Call to undefined method SocialiteProviders\Manager\ServiceProvider::driver()
It would be great if someone could help me. Thanks in advance!
Make sure that the actual driver is in there. Afterwards, since I don't see any class inclusions in there, you might need to call that class like this \App\Socialite::driver('strava') or just \Socialite::driver('strava'), I just don't know if that is the full code for your controller or not.

Cakephp2: How to call and use the function in a Model in Controller

I'm new to Cakephp2 and I wanted to ask a question to the pros.
How to call a function in model in a controller in cakephp2? I've read the documentation but
it's not clear to me since I'm a newbie.It would be great if I can attain clear explanation! Thank you!
For example I have a model named Android and in that model there is a function
public function _checkUaType($ua) {
}
Should I call the function like $this->Android->_checkUaType($ua) ? Or are there a better way to use a function in a controller?
You need to just load the model in the controller and call Model function as below example.
In the Controller use below code:
function YourFunctionName {
$this->loadModel('Android');
$this->Android->_checkUaType($ua);
}

Laravel 4: Using views in a package

I've created a very basic app in Laravel 4, it's something I'll be reusing a lot in various projects so it made sense to convert it to a package before i got too far, but I'm struggling to make the changes to get it working, which I think is largely due to figuring out how to access the various objects that are normally available in an app, eg View::make
I had the following code working in an app:
class PageController extends BaseController {
public function showPage($id)
{
//do stuff
return View::make('page/showPage')
->with('id', $id)
->with('page', $page);
}
for the package I have the following:
use Illuminate\Routing\Controllers\Controller;
use Illuminate\Support\Facades\View;
class PageController extends Controller {
public function showPage($id)
{
//do stuff
return View::make('page/showPage')
->with('id', $id)
->with('page', $page);
}
However this does not load the blade template which is located at:
workbench/packagenamespace/package/src/views/page/showPage.blade.php
nor does this work:
return View::make('packagenamespace/package/src/page/showPage')
Also, I am wondering if what i have done with the use statements where I use the facade object correct, to me it seems like there should be a neater way to access things like the View object?
You should read the docs: http://four.laravel.com/docs/packages
Specifically the part explaining loading views from packages ;)
return View::make('package::view.name');
If you don' want to use:
use Illuminate\Support\Facades\View;
Just do:
use View;
Or even without the use statement:
\View::make('package::view.name');
// Serviceprovider.php
$this->loadViewsFrom(__DIR__.'/resources/views', 'laratour');
// Controller
<?php
namespace Mprince\Laratour\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class LaratourController extends Controller
{
public function index()
{
return view('laratour::index');
// return view('packageName::bladeFile');
}
//END
}

Categories