Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent Accessing Original

I trying to get a value from an object I get from one of my model. It only returns me the attributes which is not what I want because it does not correspond to what is in my table. I want to access the original array.

I did:

$entries = Model::where('A', $A)->where('B', $B)->get();

@Foreach ($entries as $entry) 

$entry->id
$entry->name

@Endforeach

I tried to add ->original but it either doesn't work.

Here's partially the first entry of my var_dump($entries)

(
    [items:protected] => Array
        (
            [0] => App\Models\TableA Object
                (
                    [table:protected] => Table A
                    [primaryKey] => id
                    [connection:protected] => 
                    [perPage:protected] => 15
                    [incrementing] => 1
                    [timestamps] => 1
                    [attributes:protected] => Array
                        (
                            [id] => 1
                            [name] => 2

                        )

                    [original:protected] => Array
                        (
                            [id] => 1
                            [name] => 1

                        )
like image 653
Wistar Avatar asked Nov 08 '13 03:11

Wistar


People also ask

What is accessors and mutators in Laravel?

Accessors and mutators allow you to format Eloquent attributes when retrieving them from a model or setting their value. For example, you may want to use the Laravel encrypter to encrypt a value while it is stored in the database, and then automatically decrypt the attribute when you access it on an Eloquent model.

How do I search in Laravel eloquent?

Searching Eloquent models Imagine you need to provide a search for users. Using Eloquent you can perform a search like this: User::query() ->where('name', 'LIKE', "%{$searchTerm}%") ->orWhere('email', 'LIKE', "%{$searchTerm}%") ->get();

What is advantage of eloquent in Laravel?

Eloquent is an ORM, which means can automatically handle the relationships of your models for you. You can retrieve related models without writing complex queries. You can even retrieve database information without any kind of database knowledge at all.

What is polymorphic relationship in Laravel?

A one-to-one polymorphic relationship is a situation where one model can belong to more than one type of model but on only one association. A typical example of this is featured images on a post and an avatar for a user. The only thing that changes however is how we get the associated model by using morphOne instead.


1 Answers

When retrieving the original value of an Eloquent model attribute, you can use getOriginal($key)

Reference:

  • Laravel 4.2
  • Laravel 5.0
like image 116
Rob Gordijn Avatar answered Sep 18 '22 11:09

Rob Gordijn