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

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.

Related

Run a command in Laravel on every page load

I want to load a command with Laravel on each page load;
$mail_count = mail::where('to_id', '=', Auth::user()->id)->where('read', '=', '0')->count('read');
What would be the best way to do this? This then needs to output the result in the master template for the page.
Use a view composer for master template, for example:
// app/providers/ComposerServiceProvider.php
public function boot()
{
view()->composer(
'layouts.master', 'App\Http\ViewComposers\MasterComposer'
);
}
Then create the Composer class:
namespace App\Http\ViewComposers;
use Auth;
use App\Mail;
use Illuminate\View\View;
class MasterComposer
{
public function compose(View $view)
{
$mail_count = Mail::where('to_id', Auth::user()->id)
->where('read', 0)
->count('read');
$view->with('mail_count', $mail_count);
}
}
Finally, you can use {{ $mail_count }} in your master view to print out the result. So, in this case, what it's doing is, whenever your views\layouts\master.blade.php will be rendered the compose method will be called and $mail_count will be attached into the view. Make sure to use the exact name for the view, I've used layouts.master (views/layouts/master.blade.php) for this example.
You can use it in a laravel provider
go to the AppServiceProvider.php inside the boot function paste variable
$mail_count = mail::where('to_id', '=', Auth::user()->id)->where('read', '=', '0')->count('read');
then you can do it with one of the options:
1.
view()->composer('*', function($view) use($mail_count){
$view->with('mail_count', $mail_count);
});
2.
view()->share('mail_count', $mail_count);

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 4 - Method [where] does not exist

I am trying to get row from the table with "->find(X)" or "->where" but i get error in return. i guess i am missing something but i can't tell what.
<?php
class SnippetsController extends BaseController {
public $restful = true;
public function index($id)
{
$snippet = SnippetsController::where('id', '=', 11)->get();
i tried it with find but noting helps.
$snippet = SnippetsController::find(11);
with "->get" and without. but noting works. i do have the "11" id in the table itself and the error seems to have noting to do with it.
maybe i need to extend the class to Eloquent? if so, how do i keep the BaseController on top of it?
Thanks!
error:
BadMethodCallException Method [find] does not exist.
You must have a Model called Snippet, right? So this is how you use it:
$snippet = Snippet::find(11);
Your SnippetsController is not tied to a database (Eloquent ORM), so you must create (if you don't have already) a model:
class Snippet extends Eloquent {
}
Just edit the code $snippet = SnippetsController::where('id', '=', 11)->get(); with $snippet = Snippets::where('id', '=', 11)->get();. May be your model name is Snippets it may solve your problem.

Laravel 3 Eloquent ORM usage

I have the following code which works but doesn't seem to follow the laravel eloquent way:
Article::left_join('images', 'articles.id', '=', 'images.article_id')
->join('article_category', 'articles.id', '=', 'article_category.article_id')
->where('article_category.category_id', '=', $category_id)
->get();
I have 4 tables; articles and categories which have a many to many relationship with each other, a pivot table article_category table which holds the article id and category id and an image table which has one to one relationship with an article.
I setup my models as:
class Category extends Eloquent {
public static function get_articles($category_id) {
return static::find($category_id)->has_many_and_belongs_to('Article');
}
class Article extends Eloquent {
public function categories() {
return $this->has_many_and_belongs_to('Category');
}
public function image() {
return $this->has_one('Image');
}
However I can't seem to get all three bits of info together. I can do:
Category::get_articles($current_category)->get();
To get all articles in a given category but I can't seem to get the image for the article, there seems to be nothing I can chain onto? Unless I'm doing it incorrectly? Is there a trick I'm missing?
I even tried the stripped down version from the docs:
foreach (Article::with('image')->get() as $article) {
echo $article->image->foo;
}
However I get an error: Trying to get property of non-object, even though var_dump shows $article->image is an object! Weird.
Thanks
If you have not setup a model for the image table, do that. The ORM needs the model there so it knows what 'Image' refers to.
Can you get the category information using the ::with method or is that troublesome too?

codeigniter model problem

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' );

Resources