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

After adding Paginate function I'm getting this error
$sub_categories = SubCategory::where('id', $id)->first();
$products = Products::where('subcategory_id', $sub_categories->id)->get()->paginate(10);

$sub_categories = SubCategory::where('id', $id)->first(), this will only give you null or one record, not a collection, so you can not use ->paginate(10); chain it. You will only get one record at the most, why you need to paginate?
Update
so first for the sub_categories you do not need to paginate, as you just want one record. so the code should be like this.
$sub_categories = SubCategory::where('id', $id)->first();
second, if you want to paginate $products you should do this,
if ($sub_categories)
{
$products = Products::where('subcategory_id', $sub_categories->id)->paginate(10);
}

Related

Error Method Illuminate\\Database\\Eloquent\\Collection::save does not exist. in Laravel

I want to change the status at $ result, to get the data at $ result I use query builder, but there is an error like that
$results = ClientVendor::where('client_id','=', $request->client_id)
->where('vendor_id','=',$request->vendor_id)
->get();
$results->status = $request->status;
$results->save();
return response()->json($results);
You cant do this because you call whole collection where is many elements. Call just single record, then you can update it.
When you use get() you call collection
When you use first() or find($id) then you get single record that you can update.
Look at example:
$results = ClientVendor::where('client_id', $request->client_id)
->where('vendor_id',$request->vendor_id)
->first(); // this point is the most important to change
$results->status = $request->status;
$results->save();
return response()->json($results);;
Good luck!
You can try this one too.
$results = ClientVendor::where('client_id','=', $request->client_id)
->where('vendor_id','=',$request->vendor_id)
->update([
'status' => $request->status
]);
Try this:
$results = ClientVendor::where('client_id', $request->client_id)
->where('vendor_id',$request->vendor_id)
->first();
$results->status = $request->status;
$results->save();
return response()->json($results);
It depends on your needs, if you want to :
Get and update one record, you should use first() or firstOrFail() instead of get(). Should be look like this :
$results = ClientVendor::where('client_id', $request->client_id)
->where('vendor_id',$request->vendor_id)
->first();
$results->status = $request->status;
$results->save();
return response()->json($results);
Get and update multiple records, yes you can use get(), but you should do foreach and then update the single record one by one. just like this :
$results = ClientVendor::where('client_id', $request->client_id)
->where('vendor_id',$request->vendor_id)
->get();
foreach($results as $result){
$result->status = $request->status;
$result->save();
}
return response()->json($results);
Many have suggested using first() instead of get(). I think fistOrFail() would be a better option to handle null results.
Otherwise, in cases where the result is null, you'd get Call to a member function save() on null error.
Eg:
ClientVendor does not have a record with either client_id or vendor_id matching $request->client_id or $request->client_id respectively.
$result = ClientVendor::where('client_id', $request->client_id)
->where('vendor_id',$request->vendor_id)
->first();
$result->status = $request->status; //produces error Creating default object from empty value
$result->save(); //produces error "Call to a member function save() on null" because $result will be empty
The above code produces 2 exceptions: 1. "Creating default object from empty value" on lines $result->sataus = .. and 2."Call to a member function save() on null" on line $result->save because $result will be empty.
To fix that, you would call firstOrFail() like so.
$result = ClientVendor::where('client_id', $request->client_id)
->where('vendor_id',$request->vendor_id)
->firstOrFail();
$result->status = $request->status;
$result->save();
This fix, firstOrFail, when there are no results would produce a 404, a handled error, and the subsequent lines would not be executed.
For situations where you query using find($id), it is better to use findOrFail($id)
If you need to update one item use find with it's primary key & then update.Or without primary key then use where & first.then update as like this Just Use
$results = ClientVendor::where('client_id', $request->client_id)
->where('vendor_id',$request->vendor_id)
->first();
$results->update([
'status'=>$request->status
]);
return response()->json($results);

Operetion on child collection

I have code like this
$tag = Tag::where('slug' = $slug)->first();
$posts = $tag->posts;
It works correctly but I want to use limit, orderBy, offset and other operation on posts. So it works
$posts = $tag->posts->where('accept', 1);
But it doesn't works
$posts-> $tag->posts->orderBy('created_at', 'desc');
//or
$posts-> $tag->posts
->offset($offset)
->limit($limit);
I must use offset and limit into query from var.
How I can do that?
When you set up your initial query Tag::where('slug' = $slug)->first(); you're using Query Builder and it's methods. But when Laravel returns the results, they're returned as a collction object -- those have very similar but slightly different methods available. https://laravel.com/docs/5.8/collections#available-methods
On a collection or its children, instead of orderBy() you would use sortBy() or sortByDesc(). Those will return an instance of the collection, sorted by your specified key. $results = $posts->sortBy($sorting);
The same idea with limit, in this case you can use the splice method. (Collections are basically php arrays on steroids) Splice accepts two parameters, a starting index and a limit. So, to get only the first 10 items, you could do this: $results = $posts->splice(0, 10);
And of course, you can also chain those togeather as $results = $tag->posts->sortBy('id')->splice(0, 10);
When you use child, Eloquent create another subquery, then result is added to parent, thats way its not sorting properly.
A solution could be join tables:
$tags = Tag::where('tags.slug' = $slug)
->join('tags', 'tag.post_id', '=', 'posts.id')
->orderBy('posts.created_at', 'desc')
->select('tags.*')
->get();

Method orderBy does not exist in Laravel Eloquent?

I have a piece of code like this:
$products = Product::all()
if ($search_value) {
$products = $products->where('name', 'LIKE', "%$search_value%");
}
$products = $products->orderBy('created_at', 'desc')->skip(10)->take(10)->with('tags')->get();
I got the following error:
BadMethodCallException in Macroable.php line 81:
Method orderBy does not exist.
I guess orderBy need to follow Product:: directly, but I can't save $products = Product::, can I?
Any suggestions? Thanks.
You're trying to use orderBy() method on Eloquent collection. Try to use sortByDesc() instead.
Alternatively, you could change $products = Product::all(); to $products = new Product();. Then all your code will work as you expect.
just use the one line code it will work fine
$product= Product::orderBy('created_at','desc')->get();
use sortByDesc('id') or simple sortBy() inside use the variable through which you wanna sort like i add id
If you want to get the list of all data and grab it in descending order try this:
$post = Post::orderBy('id', 'DESC')->get();
You are first getting all() data and then trying to sort which is wrong. You have to fix this by removing
$products = Product::all()
and changing your code into something like this
if ($search_value) {
$products = Product::where('name', 'LIKE', "%$search_value%");
}
else {
$products = Product::orderBy('created_at', 'desc')->skip(10)->take(10)->with('tags')->get();
}
Hope you get idea to tweak your code.
Your query is wrong.
remove all from $products = Product::all() and then put get() at the end of your query.
$table_Data = DB::table('tbl_product')->orderBy('id','DESC');
You can use this...

How to count and fetch data query in laravel?

How to merge this two queries ?
$data = DB::table('category_to_news')
->where('category_to_news.name', ucwords($category))
->remember(1440)
->count();
and
$data = DB::table('category_to_news')
->where('category_to_news.name', ucwords($category))
->remember(1440)
->get();
So, as far as I understand from your comment, you simply want to get all records from the table category_to_news and you want to know how many records are in there, right?
MySQL's count is an aggregate functions, which means: It takes a set of values, performs a calculation and returns a single value. If you put it into your names-query, you get the same value in each record. I'm not sure if that has anything to do with 'optimization'.
As already said, you simply run your query as usual:
$data = DB::table('category_to_news')
->where('name', ucwords($category))
->remember(1440)
->get(['title']);
$data is now of type Illuminate\Support\Collection which provides handy functions for collections, and one them is count() (not to be confused with the above mentioned aggregate function - you're back in PHP again, not MySQL).
So $data->count() gives you the number of items in the collection (which pretty much is an array on steroids) without even hitting the database.
Hi DB class dont return collection object it give error "call member function on array" but eloquent return collection object. for above code we can use collect helper function to make it collection instance then use count and other collection methods https://laravel.com/docs/5.1/collections#available-methods .
$data = DB::table('category_to_news')
->where('name', ucwords($category))
->remember(1440)
->get();
$data = collect($data);
$data->count();
You my get it using:
$data = DB::table('category_to_news')
->where('name', ucwords($category))
->remember(1440)
->get();
To get the count, try this:
$data->count();
Why you are using DB::table(...), instead you may use Eloquent model like this, create the model in your models directory:
class CategoryToNews extends Eloquent {
protected $table = 'category_to_news';
protected $primaryKey = 'id'; // if different than id then change it here
}
Now, you may easily use:
$data = CategoryToNews::whereName(ucwords($category))->get();
To get the count, use:
$data->count();

Eloquent and Pivot Tables in Laravel 4

I have a Poll table, a Students table, and a pivot table between them that includes a token and their three votes.
public function students()
{
return $this->belongsToMany('Student', 'polls_students')->withPivot('token','first','second','third');
}
While working out saving the poll results, I came across some odd behavior that I don't quite understand. I'm hoping somebody can explain what it is I'm missing:
$poll = Poll::find(Input::get('poll_id'));
foreach($poll->students()->where('students.id', '=', Input::get('student_id'))->get() as $student){
var_dump($student->pivot->token);
}
$student = $poll->students()->where('students.id', '=', Input::get('student_id'))->get();
var_dump($student->pivot->token);
In the above code, the foreach loop will successfully display the token, where the second one throws the exception Undefined property: Illuminate\Database\Eloquent\Collection::$pivot
What am I missing? Are these two calls not logically creating the same object? How is 'pivot' working on the first and not the latter?
You first example:
$poll = Poll::find(Input::get('poll_id'));
foreach($poll->students()->where('students.id', '=', Input::get('student_id'))->get() as $student){
var_dump($student->pivot->token);
}
Here $poll->students() retrieves a collection and because of foreach loop you get a single object in your $student variable and you can use $student->pivot->token
You second example:
$student = $poll->students()->where('students.id', '=', Input::get('student_id'))->get();
var_dump($student->pivot->token);
Here you are doing same thing, using $poll->students() you are getting a collection but this time you are not using a loop and trying to do same thing using $student->pivot->token but it's not working because you didn't define any index from which you want to get the pivot->token, if you try something like this
$student->first()->pivot->token
Or maybe
$student->get(1)->pivot->token
Or maybe you can use first() instead of get() like this
$student = $poll->students()->where('students.id', '=', Input::get('student_id'))->first();
Then you can use
$student->pivot->token
Remember that, get() returns a collection even if there is only one record/model.
$poll = Poll::find(Input::get('poll_id'));
foreach($poll->students as $student){
var_dump($student->pivot->where('student_id',$student->id)->where('poll_id',$poll->id)->first()->token);
}

Categories