codeigniter model problem - codeigniter

I updated to CI 2.0 now my models stopped working.I used
parent::__construct();
And i defined the class correctly extends CI_Model but even using a simple get all
$query = $this->db->get('mytable');
Returns an error
Call to a member function get() on a non-object in models\crudModel.php on line 13
I'm sure its related to the update since they worked fine before.Thanks.

It sounds very much like you are not loading the database library - have you got it in your config/autoload.php? It is sensible to autoload it if you use the database library in all your controllers:
$autoload['libraries'] = array( 'database' );

Related

Call to undefined method Illuminate\Database\Query\Builder::remember()

I was trying to cache the db query with built in function remember(). But it doesn't seem to be working fine. Here is fine snippets.
$categories = Category::orderBy('rank', 'asc')
->select('id', 'name', 'rank')
->where('parentid', '=', 0)
->where('id', '<>', 4)
->remember(300)
->get();
This is the reference link, which I was following. I am getting the following error messa
Call to undefined method Illuminate\Database\Query\Builder::remember()
Category.php
<?php
namespace App;
use Eloquent;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
//
}
This functionality was removed in Laravel 5. However, you can still bring it back by following the tutorial behind this link. It's using the dwightwatson/rememberable package.
A better and future proof way of resolving this issue, is using the Cache method. This functionality is available from Laravel 4.2 onwards.

laravel 5.2 - Accessing Model in Controller Method

I have added the line use App\Post; in the header of my PostController class.
When I try $post = new App\Post; in a controller method, I get the following error message
Class 'App\Http\Controllers\App\Post' not found
What are some possibilities for why I am getting this error message?
Since you already included the Post class, you don't have to reference the path again.
$post = new Post();
This should work.
u can use facades..
$post = Post::all(); << return all row
I am assuming you used artisan to generate the Model. You may use eloquent as:
use App\Post as Post;
...
$post = Post::all();
The reason you are not able to access the model via App\Post is because the file you are attempting to do this in already has a namespace, shown in the error: App\Http\Controllers\App\Post, which implies the namespace of the file is App\Http\Controllers.
Since you are not referencing the model with an absolute namespace (\ at the beginning), PHP is looking for that class relative to the current namespace.
<?php
namespace App\Http\Controllers;
...
$post = App\Post::find(1); // App\Http\Controllers\App\Post
$post = \App\Post::find(1); // App\Post
That explains the error. However, as mentioned by others, you have already used the model in your file and can access it simply with Post.
$post = Post::find(1);

Laravel 5 : Couldn't instancing a model using tinker

I'm very new to laravel and PHP MVC frameworks, it's my first use of Eloquent. I'm trying to following a video tutorial.
So after creating a table by making new migration and migrate with artisan.
I created a model.Then I tried to instance it using tinker tool like so :
>>> $task = new App\Task;
I got this error :
=> App\Task {#672}
this my model class :
class Task extends Model
{
protected $table = 'tasks';
}
I did many research before asking this question, some people recommends using homestead, but I want to understand what I'm doing wrong.
This is not an error. It shows that an Object of App\Task Class has been created.
Now you can assign values to this $task Object. Like we have a field 'name' in tasks table then the following part of code can save the $task in database with value of 'name' field.
$task = new App\Task;
$task->name = 'My first task';
$task->save();

Laravel model all() function is not working as intended

I have a model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model {
protected $table = 'contacts';
//
}
And in the controller action
$c= Contact::all();
I am getting the "Whoops, looks like something went wrong." error.
Error detail:
FatalErrorException in ContactController.php line 9:
Class 'App\Http\Controllers\Contact' not found
The table "contacts" exists in the database.
What thing I am missing? Whats wrong here?
Tell your controller where your model is if it is in route of your project then on the top of your controller add
use App\Contact;
Or you may also define it every time
$c = App\Contact::all();
also in your model no need to define a table until it is not different from the plural model name. If your model name is Contact, laravel on its own query contacts table, you should define the table name if the model name is Contact and the table name is somethingElse.
Don't use
$c= Contact::all();
Instead use
$c = \App\Contact::all()
or
$c = new \App\Contact;
$c->all();
This is all that you have to use.
UPDATE: Just after digging in the concepts of OOP, I found this:
<?php
use \App\Contact;
var_dump(Contact::all());

Using Sentry with Codesleeve Stapler?

Update #1
So I think I found out where the problem is but since I lack the knowledge, there's no way for me to solve it.
When using Sentry, I've tried extending both 'Model' which is default with the Sentry package and Laravel's 'Eloquent' under Illuminate\Database\Eloquent\Model. Neither worked. But while using Laravel's Eloquent on its own (no Sentry package addons), Stapler works nicely.
So I guess there is some compatibilty problem between Stapler and Sentry's User model even though it uses Eloquent (its extra functionalities might be conflicting with stapler's traits or the construct function, I don't know). Too bad since Sentry is a great package for managing authentication and throttling and is widely used as far as I know.
Is there any easy way to use stapler's agnostic version with sentry or will I have to rewrite all my code to use laravel's auth?
Main question
I'm building an app which uses Cartalyst's Sentry for managing users. However, I want users and other models to be able to have associated avatars, so I'm using Codesleeve's Stapler too.
I've read both laravel-stapler's readme and most of stapler's standalone version documentation but couldn't figure out how to make them work with Sentry. Whenever I try to create an user through this function:
$user = Sentry::createUser(array(
'username' => Input::get('username'),
'email' => Input::get('email'),
'password' => Input::get('password'),
'activated' => false,
'avatar' => Input::file('avatar')
));
return 'success';
I get:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'avatar' in 'field list'
So it seems Sentry is querying as if the avatar is a string and Stapler is not working.
Here is my Sentry user model (which I'm extending in Laravel's):
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Database\Eloquent\Model;
use Codesleeve\Stapler\ORM\StaplerableInterface;
use Codesleeve\Stapler\ORM\EloquentTrait;
use Cartalyst\Sentry\Groups\GroupInterface;
use Cartalyst\Sentry\Hashing\HasherInterface;
use Cartalyst\Sentry\Users\LoginRequiredException;
use Cartalyst\Sentry\Users\PasswordRequiredException;
use Cartalyst\Sentry\Users\UserAlreadyActivatedException;
use Cartalyst\Sentry\Users\UserExistsException;
use Cartalyst\Sentry\Users\UserInterface;
class User extends Model implements UserInterface, StaplerableInterface {
use SoftDeletingTrait, EloquentTrait;
protected $fillabe = array('username','email','password','password_temp','code','active','avatar');
public function __construct(array $attributes = array()) {
$this->hasAttachedFile('avatar', [
'styles' => [
'medium' => '300x300',
'thumb' => '100x100'
]
]);
parent::__construct($attributes);
}
protected $dates = ['deleted_at'];
And inside Sentry's config:
'model' => 'Cartalyst\Sentry\Users\Eloquent\User',
This might be simple to solve since I'm pretty new into Laravel, Sentry and Stapler but I couldn't find any info which would make me solve the problem.
Thanks in advance!
Yes, it can be done. Based on what's in your OP, the root cause appears to be that your user model doesn't know that avatar is a Stapler-provided column.
Theory: the Sentry User Provider manufactures a user model, right here. That user model must be Stapler-aware, per the Stapler docs. Fortunately, Sentry is configurable and you can tell it to use your custom model with setModel().
Rough outline of solution: First, you need a model that is Stapler-aware. You might already have this. Note that it extends the Sentry-provided model class and also imports the Stapler trait. Both are important.
<?php // app/models/UserModel.php
namespace App\Model;
use Codesleeve\Stapler\ORM\StaplerableInterface;
use Codesleeve\Stapler\ORM\EloquentTrait;
use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;
class UserModel extends SentryUserModel implements StaplerableInterface {
use EloquentTrait;
}
Second, you need to instruct Sentry to use this. Below I use run-time configuration changes, as is my preference. But you can also change the Sentry configuration file:
<?php // app/bootstrap.php
\Sentry::getUserProvider()->setModel('App\Model\UserModel');
I'm doing this from memory on Laravel 4, so there may need to be some tweaking involved.

Resources