Laravel 5 Eloquent Model Results - php

I am using eloquent model to retrieve data from database.My query
$tests_details = Previous_Mocks::where($data)->orderBy('sno', 'desc')->get();
As of my knowledge $tests_details will return array of objects(results) but when i echo is_array($tests_details) it is returning false which means it's not an array but when i echo count($test_details) it is showing the correct count.See below code
foreach ($tests_details as $td)
{
echo is_object($td);
echo "<br/>";
}
it is returing 1 for is_object($td). When i print $tests_details using print_r function below is the output i am getting
and when is echo $tests_details[0]->edate it is showing proper output and everything working fine. But i want to know why the eloquent returned the data in the format shown in image instead of normal objects. I am new to laravel and currently using laravel 5.0 any explanation is appreciable.

The data return by Previous_Mocks model is a collection object.
All multi-result sets returned by Eloquent are instances of the
Illuminate\Database\Eloquent\Collection object, including results
retrieved via the get method or accessed via a relationship. The
Eloquent collection object extends the Laravel base collection, so it
naturally inherits dozens of methods used to fluently work with the
underlying array of Eloquent models.
However, collections are much more powerful than arrays and expose a variety of map / reduce operations that may be chained using an intuitive interface.
Eloquent Collection Reference

Related

How to get the relationship data from casted toArray Eloquent

I'm trying to get the data without using eager loading in a Eloquent Model but I needed to cast it into Array to use in some part of the code.
In order words, I wanna just get the data like this example:
$invoice->invoice_type_id->name;
Since it's an array what I'm writing is: nfe['invoice_type_id']['name']
But I'm getting this error (because it's an array):
Trying to get property of non object
So, my query is:
Invoice::query()->where('order_id', $order_id)->get()->toArray();
And my model I already have the relationship declared:
public function invoiceType()
{
return $this->belongsTo(InvoiceType::class);
}

Accessing Eloquent Relationship Automatically when loaded

I am working on a project which requires me to get all the list of all information from a table --Just like in a blog, i used the all() method to do this but when i try to get the method i declared in my Model i get an error, saying
the collection instance does not exists
But when i use The
Model::find($id)->relationship()->name;
it works fine. Is there any way to load all relationship with the all() function in laravel.
Thanks for your help..
When you perform Model::find($id)->relationship(); you are actually accesing to the Dynamic relationships Properties
You need to convert it into a collection using Model::find($id)->relationship()->get();
Then you can perform any collection method to get the result you want. After doing this you can access to its attributes like this:
$model_varible = Model::find($id)->relationship()->get();
$model_variable = $model_variable->find($id)->name;
Let me know if this works for you.
You should use relationship without brackets to access the model:
Model::find($id)->relationship->name;
And use "with()" to populate the relationships:
Model::where('published', 1)->with('relationship')

1 to 1 inverse relationship "belongsTo" giving a collection laravel instead of model

In my Profile model I setted this relationship
public function lease()
{
return $this->belongsTo(Lease::class, 'lease_id', 'id');
}
And in my Lease model I seeted this way
public function profile()
{
return $this->hasOne(Profile::class, 'lease_id', id);
}
As longs as I know in laravel you could do
$profile = factory(App\Profile::class)->create();
$profile->lease()->get();
And then responds correctly with the model inside of a collection
And if I do
$profile->lease
Responds correctly directly with the model
It isn't supposed that dynamic propertis execute the query right away like a shortcut of ->lease()->get()? Why it gives different formatted results?
When you are calling get on a builder you are getting a collection always. When you call first on a builder like that you will get a model or null. The dynamic property for the relationship, based upon the relationship object, will either query with get or first respectively when it loads it. Which is why $model->relationship is returning you the result you expect.
The relationships that are singular, cause a find and the ones that are many cause a get.
Laravel 5.4 - Docs - Eloquent - Relations - Relationship Methods vs Dynamic Properties

Laravel Eloquent collection attributes

I'm developing a webapp with Laravel 5.2 but there is an issue that I can't solve.
I have a model which extends Eloquent Model but when I try to output my database table with "where", for example
\App\MyModel::where('id_table', 23)->first();
It return a collection with many informations not useful for me at this moment like 'guarded', 'keytype'... and my table's data are under 'attributes'.
Following laravel's guide I see everybody use simply where like me and then output with $mytable->myvalue, but this cannot work for me of course.
Here a screenshot with the collection I'm talking about:
How can I get always just attributes when I use something like "::where()"?
I've tried getAttributes() but it gives me a 500 error.
If you want to get just couple of attributes do many things. Probably first way will be the best for you:
Use pluck():
\App\MyModel::where('id_table', 23)->pluck('id', 'name');
Use select():
\App\MyModel::select('id', 'name')->where('id_table', 23)->get();
Use get() with parameters:
\App\MyModel::where('id_table', 23)->get(['id', 'name']);
If you want to get Eloquent collection and only then build an array with just attributes, you can use toArray():
\App\MyModel::where('id_table', 23)->get()->toArray();
If you want to get some attributes to a new collection, use pluck() on collection:
$collection = \App\MyModel::where('id_table', 23)->get();
$names = $collection->pluck('name');

Convert Eloquent Builder to Query Builder

It seems that everyone is asking how to convert Query\Builder to Eloquent\Builder. I need the opposite - I have a Model with some scopes defined which I create a query from. The result that it returns is an instance of my model. I want it to be a a plain object.
Fetching a model and the converting to an stdClass doesn't seem right to me.
Is this possible?
Yes. You can build up the query using the Eloquent query builder, but then you can actually execute the query using the base query builder, which will return stdClass objects.
Below is an example. Obviously you can chain all this stuff together into one line, but I broke it out into multiple lines to explain what's going on. Assume you have an active() scope on your User model:
// start an eloquent query but don't execute it yet
$query = User::active();
// add any other conditions...
// get the base builder for the eloquent builder
$baseQuery = $query->getQuery();
// will return a standard array of stdClass objects
$users = $baseQuery->get();
Another example to just return one object:
// will return a stdClass object
$user = User::active()->getQuery()->first();
Be aware, though, that you do lose Eloquent query functionality, such as relationships and eager loading.
For example:
$user = User::with('posts')->active()->getQuery()->get();
This will not run the query for the posts for the users, and the posts will not be attached to the stdClass objects returned.

Categories