Laravel isNotEmpty() not working on Eloquent model - laravel

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.

Related

Get data from another model

$objects = Objects::where("id_host", $hostId)
->orderBy("id", "desc")
->get();
In the collection , each object has a type_id field . How can I use this field to get a record in another model and mix them into this object in the response
First thing is first, in order to show the relationship between the records, you'll need to set up a One-to-Many/Many-to-One relationship. This allows you to readily call those relationships from within Laravel and to load them together.
Without being able to see your Type and Object classes, I really can't give specific advice on this, but it should look something like this:
Objects
public function type(): BelongsTo
{
return $this->belongsTo(Type::class);
}
Type
public function objects(): HasMany
{
return $this->hasMany(Objects::class);
}
Once you've done that, you can add a with(...) call to your Eloquent query to eager load the relationship.
$objects = Objects::where("id_host", $hostId)
->orderBy("id", "desc")
->with('type')
->get();
Alternatively, if you don't want to eager load it for some reason, you can call $object->type to get the Type object.

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.

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

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.

Database relation with Laravel Eloquent ORM

I'm new to Laravel and I'm stuck. This is what I am struggling with:
$questions = Question::find($id)->quiz(); // this code retrieves data from
// the table using the primary key
// in the table. The is a parameter
// that is passed via get.
This is what I have right now:
$questions = Question::where('quiz_id', '=', $id)->quiz();
This is the error I get:
Call to undefined method Illuminate\Database\Query\Builder::quiz()
What I want to do:
I want to run a query to get data from my database table using the foreign key in the table not the primary key, I also want to be able to use relations with this as seen from what I tried to do above.
Edit: Added the Question Model
<?php
class Question extends Eloquent{
protected $table = 'quiz_questions';
public function quiz()
{
return $this->belongsTo('Quiz');
}
}
Calling the quiz() function from Question::find($id)->quiz() will return a Query Builder instance allowing you to query the parent of the Question, its not going to return any data at that point until you call ->get() or another method that actually executes the query.
If you're wanting to return all the questions belonging to a certain quiz then you can do it like this.
$questions = Question::where('quiz_id', $id)->get();
This will return an Eloquent\Collection of the results for all questions with a quiz_id that is equal to $id.
If you've setup the relations between the Quiz and Questions then you can also do this using the Laravel relations.
$quiz = Quiz::findOrFail($id);
foreach($quiz->questions as $question)
{
// Do stuff with $question
}
Laravel will automagically pull Questions from the database that belongTo the Quiz you've already got from the database, this is known as eager loading http://laravel.com/docs/4.2/eloquent#eager-loading
Wader is correct, just calling where() will not execute your query. You either call get() and get an iterable result or use first() if you only want one result.
$quiz = Question::where('quiz_id', '=', $id)->first()->quiz();

Resources