Laravel Method paginate does not exist trying controller - laravel

$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.

Related

How to echo the last query after eloquent model in laravel 7?

I am deleting an item like this. The delete method is excuting well but I can't echo the last executed query.
public function deleteItems(Request $request){
$query=null;
switch($request->category){
case('categorylist'):
$category = category::find($request->id);
$query=$category->delete()->toSql();
break;
default:
}
echo json_encode(array('status'=>2, 'msg'=>'Successfully deleted', 'query'=>$query->toSql()));
}
The toSql() method works only on instances of Illuminate\Database\Query\Builder ( query builder as it is usually called ). Methods like where(), orderBy() etc return an instance of query builder, hence toSql() works on them. The delete() method does not return an instance of query builder ( I think it returns Boolean or the id it deleted. Can't remember ). So the toSql() method won't work.
If you just want to get the last executed query, then you could use DB::getQueryLog(). For example
DB::enableQueryLog();
$category->delete();
$queryLog = DB::getQueryLog();
// Now $queryLog will contain all the SQL queries which were executed after the DB::enableQueryLog()
$last = end($queryLog);
$query = $last['query'];
$bindings = $last['bindings'];

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.

Eloquent relationship - whereNotNull does not exist

I have a relationship in Eloquent that I'm trying to query.
$deliveryOverride = $product->days->whereNotNull('margin')
->where('price_id', $order['price_id'])
->where('product_id', $product->id)
->where('sale_at', date('Y-m-d', strtotime($day)))
->first();
I keep getting the the error
Method whereNotNull does not exist.
Any ideas?
When you call a relation property directly on an Eloquent object, the query is executed and a Collection is returned. There is no WhereNotNull function on collections.
If you want to query the relation using that function, you will have to call the relation function directly. This will also be better for performance as the query will happen on the database.
$deliveryOverride = $product->days() // Call relation function here
->whereNotNull('margin')
->where('price_id', $order['price_id'])
->where('product_id', $product->id)
->where('sale_at', date('Y-m-d', strtotime($day)))
->first();
More information about this can be found in the documentation right here: https://laravel.com/docs/5.7/eloquent-relationships#querying-relations

Trying to get property of non-object in a custom delete

I have an query, where it returns 1 or more results. After this query, I execute a delete on these tuples.
I think it's my coding failure, but what I'm trying to do is get this select and send it by ID parameter to Delete.
public function TestDelet(AcademicoRequest $request)
{
$academico = new Academico();
$res = $request->all();
$academico->member_id = $res->member_id;
if($res->member_id){
$dados_academicos[] = DB::table('academicos')->where('member_id', $res->member_id)->orderby('id')->get();
$this-> destroy($dados_academicos->Id);
}
}
List I try on postman:
{
"member_id": "1",
}
But, return this is error: Trying to get property of non-object
Hope these suggestions help:
$request->all();
the all() method is returning array format, so you need to change from
$res->member_id
into
$res['member_id']
I'm assuming you are trying to delete one/many rows from database at academicos table
if($res['member_id']){
Academico::where('member_id', $res['member_id'])->delete();
}
Please check values of object array:
dd($dados_academicos)
If you are using laravel with mysql by default it is id not Id and with mongodb it is _id. so use
$this-> destroy($dados_academicos->id);
OR
$this-> destroy($dados_academicos->_id);
OR
$this-> destroy($dados_academicos['Id']);

Method Paginate Does not exist in laravel 5.5

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.

Resources