Method Paginate Does not exist in laravel 5.5 - laravel

public function index(Tag $tag){
//$posts=$tag->posts;
$posts=$tag->posts->paginate(10);
return view('posts.index' , compact('posts'));
}

Use relationship, not property:
$tag->posts()->paginate(10);
When you're using property, the query is already executed and you can't use the paginate() method on a collection.

Related

Laravel Method paginate does not exist trying controller

$article = Article::all()->sortBy("order")->paginate(3);
return view('nieuws' , compact('article'));
wont work this is the error I get back:
Method Illuminate\Database\Eloquent\Collection::paginate does not exist.
When you're using all() you get all the rows from the table and get a collection. You can only invoke "paginate" on a Query, not on a Collection. :
$article = Article::orderBy("order")->paginate(3);
There is no sortBy() method on the query builder, it should be orderBy. You can only invoke "sortBy" on a Collection, not on a Query.

How to add scope query in Laravel scout to retrieve record in model

I have a model Post has method scope:
public function scopeABC($query) {
return $query->where('status', 'publish') //
}
And I want to after search by Laravel Scout, the results of Post will apply this scope
$posts = Post::searchable('zzzzz')->ABC()->paginate();
But Laravel fire exception Laravel\Scout\Builder has not method ABC. Hope you help me. Thanks!
It's not documented feature but you can pass a callback to query method
https://github.com/matchish/laravel-scout-elasticsearch/issues/18#issuecomment-505977823
$posts = Post::searchable('zzzzz')->query(function($query) {
return $query->ABC();
})->paginate();
I cannot find solution for you but i think your searchable(() cannot working with paginate(), your problem is not from scope function

Laravel isNotEmpty() not working on Eloquent model

I'm probably confused by Eloquent (again...) but I thought that this should work:
$test_row = Test::
where('status', 'active')
->where('condition2', 'value')
->orderBy('order', 'asc')
->first();
if($test_row->isNotEmpty())
return $test_row;
But is it throwing the following error: Call to undefined method App\Test::isNotEmpty().
By using first() it will return an Eloquent model right? And isNotEmpty(), as well as isEmpty(), should be able to be used on the returned model?
You are trying to call isNotEmpty() on a model object and not a collection, when you use first() it returns an object not a collection.
Use
if($test_row)
{
return $test_row
}
The method isNotEmpty() is actually returned by Laravels Collection class and not by an Eloquent model. Since you're not asking for multiple results, only the model is returned instead of a collection.
Simply use
if ($test_row) {
return $test_row;
}
to check if the query had any results and the model exists.

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

I am trying this Query in Laravel 5.2
User::with(['posts'])
->withCount('post_images')
->orderBy('post_images_count', 'desc')
->take(8)
->get();
After that I got this error Call to undefined method Illuminate\Database\Query\Builder::post_images()
I did not understand what mistake is here.
Here users table has relation with posts Table and Posts table has relation with post_images table
public function posts()
{
return $this->hasMany(Post::class);
}
public function postimages()
{
return $this->hasManyThrough(PostImage::class, Post::class);
}
Please guide How can I fix this.
As #linktoahref said, withCount is taking a relation method names as argument, not a table nor a column name.
withCount('postimages')should fix your problem

Laravel Eloquent Relations: ->latest()

What is the function of latest() in laravel?
Example:
public function activity()
{
return $this->hasMany('App\Activity')
->with(['user', 'subject'])
->latest();
}
From Build an activity feed in Laravel on line 44.
I've been looking in the laravel documentation, but I couldn't find it...
latest() is a function defined in Illuminate\Database\Query\Builder Class. It's job is very simple. This is how it is defined.
public function latest($column = 'created_at')
{
return $this->orderBy($column, 'desc');
}
So, It will just orderBy with the column you provide in descending order with the default column will be created_at.
->latest() fetches the most recent set of data from the Database. In short, it sorts the data fetched, using the 'created_at' column to chronologically order the data.
Add ->get() on your code like this:
public function activity()
{
return $this->hasMany('App\Activity')
->with(['user', 'subject'])
->latest()->get();
}
latest() function in Laravel used to get latest records from database using default column created_at.
latest()is the equivalent to orderBy('created_at', 'desc')

Resources