Method Illuminate\Database\Eloquent\Collection::sortable does not exist - php

Here is my code in controller, am trying to sort columns in view file by just clicking on corresponding column name
<?php
namespace App\Http\Controllers;
use App\Http\Controllers;
use Illuminate\Http\Request;
use App\Service;
use DB;
use JsValidator;
use Validator;
use Session;
use Redirect;
use Carbon\Carbon;
use App\Libraries\GlobalHelpers;
use App\Libraries\ImageHelpers;
use Auth;
use Response;
class ServicesController extends Controller
{
protected $serviceValidationRules = [
'service_name' => 'required'
];
public function index()
{
$services = Service::all()->sortable()->get();
return view('services.index')->with('services', $services);
}
?>
Am getting following error
Method Illuminate\Database\Eloquent\Collection::sortable does not exist.
Kindly help me to resolve

Related

Laravel 5.3 Trait not found

Hello i am trying to use a trait from controller in my register controller but it can't seem to find it
the error message:
Trait 'MailVerification' not found
The class in which i want to use the trait
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
use MailVerification;
Here i call the function
protected function create(array $data)
{
$mail = $data['email'];
$this->sendVerification($mail);
Here is the trait in the class i am trying to import it from
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Mail\TestMail;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
use Session;
trait MailVerification
{
public function sendVerification($mail)
{
$verification_code = str_random(30);
Mail::send('mail.verify', ['verification_code' => $verification_code, 'mail' => $mail], function ($message) use ($mail)
{
$message->from('test#laravel.com');
$message->to($mail);
});
Session::flash('message', "Please check you're email to verify your account");
return redirect('/');
}
}
class MailController extends Controller
{
I have the trait outside of my class i don't know if this is correct but it was giving me an error while it was inside the class.
The namespace of your controller RegisterController and your trait MailVerification is different...
So, you'll have to add this line to your RegisterController
use App\Http\Controllers\MailVerification;
Also, I suggest you to put all your traits inside App\Traits folder instead of your controller. Try following a simpler way if possible :)
Edit --
This is how you register controller should look like
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Controllers\MailVerification;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers, MailVerification;
//Your code here....
}

Laravel: Even though I added use Input; I get "Class 'Input' not found"

This is my AuthorController.php
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use App\Models\Author;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Illuminate\Support\Facades\Redirect;
use View;
use Input;
class AuthorController extends Controller {
public function index()
{
$author_name = Input::get('name');
$author_slug = Input::get('slug');
$author_bio = Input::get('bio');
$new_author = Author::create(array('name' => $author_name, 'slug' => $author_slug, 'bio' => $author_bio));
return Redirect::to('/');
}
}
?>
This is what's in my Routes file
Route::post('/create-author', [
'as' => 'create-author',
'uses' => 'AuthorController#index'
]);
I'm not quite sure what's wrong, I tried hardcoding something into the database with $new_author = Author::create(array('name' => 'John)); and it worked, would it be the directory in which the Input class is in relative to my AuthorController.php, which is in App\Controllers
Use this in your controller,
use Illuminate\Support\Facades\Input;
Each time when you want to use predefined class , you can see the alias defined for it in config/app.php. you should use via facade only,
use Illuminate\Support\Facades\Input;

Class 'app\models\ not found in Laravel 5

I'm having a trouble using a model in a controller in Laravel 5.0. I created the model in a folder model under App. The code of the model id described as:
<?php namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Observacion extends Model{
protected $table = 'obs_usuarios';
protected $fillable = ['observaciones', 'usuario_id','autor_id','tipo'];
}
Part of the code of the controller where I pretend to use it, is:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\models\Contrato;
use app\models\Observacion;
use App\models\Perfil;
use Illuminate\Http\Request;
use App\models\Configuracion;
use App\models\Usuario;
use App\models\Categ_profesores;
use Carbon\Carbon;
use Illuminate\Support\Facades\Input;
use Auth;
use Closure;
use Illuminate\Contracts\Auth\Guard;
class SecretariaController extends Controller {
public function observacion($msg,$user_id,$autor_id,$type){
$observacion = new Observacion();
$observacion->observaciones=$msg;
$observacion->usuario_id=$user_id;
$observacion->autor_id=$autor_id;
$observacion->tipo=$type;
$observacion->save();
}
I even made dump-autoload but I get this error:
FatalErrorException in SecretariaController.php line 155:
Class 'app\models\Observacion' not found
The line 155 is where I do: $observacion = new Observacion();
So, I don't know what else to check.
Change:
use app\models\Observacion;
to:
use App\models\Observacion;
The first letter is a capital. In PHP names are case-sensitive.

PHP Undefined Class

I am using Laravel and I have a function in my Controller as follows:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Clans;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Helpers\DataTables;
class ClansController extends Controller
{
public function indexload(){
$tableData = new DataTables();
return $tableData->get('clans', 'id', array('id', 'clanid', 'name', 'location', 'level', 'exp', 'warslost', 'warstied', 'warwinpercent', 'warswon','playercount', 'score'));
}
}
DataTables is a Class in \app\Helpers\
The page is live at: http://clashdata.tk/clans/load
You can see that the JSON is being displayed like it should but then after the JSON there is a Laravel error saying the class couldn't be found and this is causing problems for my script. How come it says the class can't be found?
The Class is available here: http://pastebin.com/Wpn9u64U
Thanks!
Your namespace in your DataTables class is off.
You currently have namespace App\Helpers\DataTables;, but it should be: namespace App\Helpers;

How to pass Variable to Views in Laravel Controller

I have a controller in Laravel 5.0 like this-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CustomersController extends CustomerLayoutController
{
public function getDashboard()
{
return $this->view('layouts.customer.dashboard', []);
}
public function getTest()
{
return $this->view('layouts.admin.webinar', ['qustions' => DB::table('qustions')->get()]);
}
}
So, I want to pass DB::table('qustions')->get() as a parameter to my views, but I m getting error.
What I am doing wrong?
You're getting an error because Laravel is searching the class is the wrong namespace (it's "appending" the class to the current class's namespace, if you note).
You either import the DB class with the use keywords:
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
or let it know that DB resides in the "global" application namespace, so call it with a backslash:
return $this->view('layouts.admin.webinar', ['questions' => \DB::table('qustions')->get()]);

Categories