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;
Related
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
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.
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()]);
So, in L5 I created folder like app/Models/Blog where is file Posts.php which looks like:
<?php namespace App\Models\Blog;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model {
protected $table = 'posts';
}
After it I executed composer dump and then in my controller:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Models\Blog\Posts as Posts;
class BlogController extends Controller {
public function index()
{
$post = Posts::all()->toArray();
dd($post);
}
}
It throws me an error:
FatalErrorException in BlogController.php line 14: Class 'Models\Blog\Posts' not found
Try changing this:
use Models\Blog\Posts as Posts;
To this:
use App\Models\Blog\Posts;
In Laravel 5.2 it's just:
use App\Blog;
or
use App\Blog\Posts;
Change the following
class Posts extends Model {
to
class Posts extends \Eloquent {
You need to check two points :
the namespace have to be in first
the using must be use App\Models\Blog in your case
Like this :
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Blog;
class BlogController extends Controller {
public function index()
{
$post = Posts::all()->toArray();
dd($post);
}
}
(tested with Laravel 5.4)
I am trying to store my models in a custom namespace and directory structure as shown here:
I have:
namespace Modules\Core;
use App\Http\Controllers\Controller;
class TestController extends Controller {
public function index(){
$user = new User;
return view('core::test');
}
}
But I am getting:
FatalErrorException in TestController.php line 8:
Class 'Modules\Core\User' not found
Which is wrong anyway, so I thought it must be 'Modules\Core\Models\User'. I tried this and I still got the same error (just with a different class name).
My model:
namespace Modules\Core;
use Illuminate\Database\Eloquent\Model as Eloquent;
class User Extends Eloquent {
protected $table = 'users';
}
How can I get access to this model in the TestController?
Route::group(array('namespace' => 'Modules\Core\Controllers'), function() {
Route::get('/test', ['uses' => 'TestController#index']);
});
If your controller is stored in Modules/Core/Controllers, the namespace should be namespace Modules\Core\Controllers;
And likewise, if the model is stored in Modules/Core/Models, its namespace is namespace Modules\Core\Models;
Then, in the controller, import it before using it:
<?php namespace Modules\Core\Controllers;
use Modules\Core\Models\User;
use App\Http\Controllers\Controller;
class TestController extends Controller {
public function index(){
$user = new User;
return view('core::test');
}
}
I had the same problem as above. In my case I had the following namespace:
namespace Modules\XMLApi;
I got the same error as above. When I changed the namespace to the following:
namespace Modules\XmlApi;
Then run the following command:
composer dump-autoload
Then it worked.
You should edit your routes.php file:
Route::group(array('namespace' => 'Modules\Core\Controllers'), function() {
Route::get('/test', ['uses' => '\Modules\Core\TestController#index']);
});
to use full together with namespace