Laravel Getting attributes data - php

I am new in laravel using Laravel 5.3.
I am creating a check() function in laravel model for user login
here i get all data form database useing default $this->all(); this return me a large multidymentional
array .
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\wn_users Object
(
[table:protected] => wn_users
[timestamps] =>
[fillable:protected] => Array
(
[0] => role_id
[1] => firstname
[2] => lastname
[3] => username
[4] => email
[5] => password
[6] => companyname
[7] => country_id
[8] => description
[9] => ip
[10] => update_date
[11] => status
)
[connection:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[user_id] => 1
[role_id] => 1
[firstname] => Aman kumar
[lastname] => --
[username] => Aman kumar
[email] => aman.imaxtechnologies#gmail.com
[password] => e10adc3949ba59abbe56e057f20f883e
[companyname] => Imax
[country_id] => 123
[description] => Testing
[ip] => 192.168.1.1
[update_date] => 2017-03-20
[status] => 0
[created_at] =>
[updated_at] =>
)
[original:protected] => Array
(
[user_id] => 1
[role_id] => 1
[firstname] => Aman kumar
[lastname] => --
[username] => Aman kumar
[email] => aman.imaxtechnologies#gmail.com
[password] => e10adc3949ba59abbe56e057f20f883e
[companyname] => Imax
[country_id] => 123
[description] => Testing
[ip] => 192.168.1.1
[update_date] => 2017-03-20
[status] => 0
[created_at] =>
[updated_at] =>
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
)
)
But i want to get only 'attributes:protected' Form whole array in laravel . I have already tried
echo $data = $this->getAttributes()['firstname']; but this returns error
Undefined index: firstname
Please help me to resolve my issue
Thanks in Advance for your help and time .

A very simple way:
$arr = $this->all()->toArray();
var_dump($arr); // oh~ array data!

So you have a collection with properties. And you can access them just like $collection->first()->firstname Or if you want to do some operation with all items use each method:
$collection = $collection->each(function ($item, $key) {
$item->firstname .= ' Smith';
});

Related

how to fetch data from json? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
i have data in json something like that:
stdClass Object
(
[contacts] => stdClass Object
(
[14] => stdClass Object
(
[data] => stdClass Object
(
[email] => veer#gmail.com
[first_name] => veer
[last_name] =>
[user_id] => 16
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 14
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => veer
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 92
[1] => 13
[2] => 12
[3] => 9
)
[files] => Array
(
)
)
[9] => stdClass Object
(
[data] => stdClass Object
(
[email] => singh.pratibha1432#gmail.com
[first_name] => Pratibha
[last_name] => Singh
[user_id] => 8
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 9
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => pratibha
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 94
[1] => 93
[2] => 92
[3] => 91
[4] => 82
[5] => 15
[6] => 14
[7] => 13
[8] => 9
)
[files] => Array
(
)
)
[4] => stdClass Object
(
[data] => stdClass Object
(
[email] => singh.dev1432#gmail.com
[first_name] => Devesh
[last_name] => Singh
[user_id] => 7
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 4
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => devesh
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 93
[1] => 92
[2] => 15
[3] => 12
[4] => 11
[5] => 9
)
[files] => Array
(
)
)
[2] => stdClass Object
(
[data] => stdClass Object
(
[email] => admin#gmail.com
[first_name] => veronica
[last_name] =>
[user_id] => 1
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 2
[gravatar] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => admin
[profile_picture] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 94
[1] => 92
[2] => 15
[3] => 14
[4] => 13
[5] => 9
)
[files] => Array
(
)
)
)
[status] => success
)
and now I am trying to fetch "email" from the contacts->id->data->email so for that i am using this code. I am trying to loop the things by which i can fetch email from all the ids present. and in the ids section, there is data and inside data the email is present so how can i fetch all emails from all the ids.
foreach ((Array)$body->contacts as $id => $values) {
foreach ($values as $data => $value) {
$emails = array('email'=>$value->email);
return $emails;
}
}
but it return only single data:
Array
(
[email] => veer#gmail.com
)
i want to fetch all the email like that:
Array
(
[0] => Array
(
[emails] => veer#gmail.com
)
[1] => Array
(
[emails] => singh.pratibha1432#gmail.com
)
[2] => Array
(
[emails] => singh.dev1432#gmail.com
)
[3] => Array
(
[emails] => admin#gmail.com
)
)
how can I achieve this?
Define null array and in foreach run store every email in that variable.
$emails = array();
foreach ((Array)$body->contacts as $id => $values) {
foreach ($values as $data => $value) {
$emails[] = array('emails'=>$contact->data->email;);
}
}
return $emails;

merge() is replacing elements from the first collection

I want to keep ALL elements of each collection. How do I do this?
I've tried:
$fields = Field::all();
$custom_fields = CustomField::all();
$merged_fields = $fields->merge($custom_fields);
But this code appears to be replacing the first element of $fields as $custom_fields only has 1 value right now.
Here is the dump of the collections:
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Field Object
(
[fillable:protected] => Array
(
[0] => label
[1] => name
[2] => input_type
[3] => input_options
[4] => show_in_tables
[5] => sort
)
[hidden:protected] => Array
(
[0] => pivot
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 1
[label] => First Name
[name] => first_name
[input_type] => 4
[input_options] =>
[show_in_tables] => 1
[sort] => 1
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[original:protected] => Array
(
[id] => 1
[label] => First Name
[name] => first_name
[input_type] => 4
[input_options] =>
[show_in_tables] => 1
[sort] => 1
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
[1] => App\Field Object
(
[fillable:protected] => Array
(
[0] => label
[1] => name
[2] => input_type
[3] => input_options
[4] => show_in_tables
[5] => sort
)
[hidden:protected] => Array
(
[0] => pivot
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 2
[label] => Last Name
[name] => last_name
[input_type] => 4
[input_options] =>
[show_in_tables] => 1
[sort] => 2
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[original:protected] => Array
(
[id] => 2
[label] => Last Name
[name] => last_name
[input_type] => 4
[input_options] =>
[show_in_tables] => 1
[sort] => 2
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
[2] => App\Field Object
(
[fillable:protected] => Array
(
[0] => label
[1] => name
[2] => input_type
[3] => input_options
[4] => show_in_tables
[5] => sort
)
[hidden:protected] => Array
(
[0] => pivot
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 3
[label] => Email
[name] => email
[input_type] => 4
[input_options] =>
[show_in_tables] => 1
[sort] => 3
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[original:protected] => Array
(
[id] => 3
[label] => Email
[name] => email
[input_type] => 4
[input_options] =>
[show_in_tables] => 1
[sort] => 3
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
[3] => App\Field Object
(
[fillable:protected] => Array
(
[0] => label
[1] => name
[2] => input_type
[3] => input_options
[4] => show_in_tables
[5] => sort
)
[hidden:protected] => Array
(
[0] => pivot
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 4
[label] => Phone
[name] => phone
[input_type] => 4
[input_options] =>
[show_in_tables] => 0
[sort] => 4
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[original:protected] => Array
(
[id] => 4
[label] => Phone
[name] => phone
[input_type] => 4
[input_options] =>
[show_in_tables] => 0
[sort] => 4
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
[4] => App\Field Object
(
[fillable:protected] => Array
(
[0] => label
[1] => name
[2] => input_type
[3] => input_options
[4] => show_in_tables
[5] => sort
)
[hidden:protected] => Array
(
[0] => pivot
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 5
[label] => Postal Code
[name] => postal_code
[input_type] => 4
[input_options] =>
[show_in_tables] => 0
[sort] => 5
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[original:protected] => Array
(
[id] => 5
[label] => Postal Code
[name] => postal_code
[input_type] => 4
[input_options] =>
[show_in_tables] => 0
[sort] => 5
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
[5] => App\Field Object
(
[fillable:protected] => Array
(
[0] => label
[1] => name
[2] => input_type
[3] => input_options
[4] => show_in_tables
[5] => sort
)
[hidden:protected] => Array
(
[0] => pivot
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 6
[label] => Likes Cats
[name] => likes_cats
[input_type] => 1
[input_options] => Yes
No
Maybe
[show_in_tables] => 1
[sort] => 6
[created_at] => 2017-07-17 06:21:42
[updated_at] => 2017-07-17 06:21:42
)
[original:protected] => Array
(
[id] => 6
[label] => Likes Cats
[name] => likes_cats
[input_type] => 1
[input_options] => Yes
No
Maybe
[show_in_tables] => 1
[sort] => 6
[created_at] => 2017-07-17 06:21:42
[updated_at] => 2017-07-17 06:21:42
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
)
)
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\CustomField Object
(
[fillable:protected] => Array
(
[0] => label
[1] => name
[2] => php_code
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 1
[label] => Is Kevin
[name] => is_kevin
[php_code] => if ($lead->data('first_name') == 'Kevin') {
return 'Rules';
}
else {
return 'Drools';
}
[created_at] => 2017-07-17 05:18:27
[updated_at] => 2017-07-17 06:29:27
)
[original:protected] => Array
(
[id] => 1
[label] => Is Kevin
[name] => is_kevin
[php_code] => if ($lead->data('first_name') == 'Kevin') {
return 'Rules';
}
else {
return 'Drools';
}
[created_at] => 2017-07-17 05:18:27
[updated_at] => 2017-07-17 06:29:27
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
)
)

laravel assocation issue with user model

I have the association ads table with users how I can show the users
data. It shows me error trying to get property of non object
association.
Tables:
ads
users (default Laravel)
In User Model:
public function ads()
{
return $this->hasMany('App\Ad','user_id');
}
In Ad Model
public function user()
{
return $this->belongsTo('App\User');
}
in Routes
Route::get('/ads',function(){
$ads = Ad::get();
//print_r($ads);
foreach ($ads as $ad) {
echo $ad->user->name;// issue here
}
});
It prints this array when I print_r($ad->user);
App\User Object ( [fillable:protected] => Array ( [0] => name [1] => phone [2] => email [3] => password [4] => role ) [hidden:protected] => Array ( [0] => password [1] => remember_token ) [connection:protected] => [table:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [id] => 1 [name] => irfan [email] => irfan0786#gmail.com [phone] => 43434 [password] => $2y$10$dnUuC9yxQwSHcpWy1oZlpuxaU33eBz1VYCPFQgfJYtncDivmDfqym [role] => User [remember_token] => x3l3x1tkXUB3I7KiRggPRclnCR5JGnDLlzCxZxeAmfpGCYFR0ylSrKNwSSuy [created_at] => 2016-06-12 17:42:36 [updated_at] => 2016-06-22 03:26:28 ) [original:protected] => Array ( [id] => 1 [name] => irfan [email] => irfan0786#gmail.com [phone] => 43434 [password] => $2y$10$dnUuC9yxQwSHcpWy1oZlpuxaU33eBz1VYCPFQgfJYtncDivmDfqym [role] => User [remember_token] => x3l3x1tkXUB3I7KiRggPRclnCR5JGnDLlzCxZxeAmfpGCYFR0ylSrKNwSSuy [created_at] => 2016-06-12 17:42:36 [updated_at] => 2016-06-22 03:26:28 ) [relations:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [dateFormat:protected] => [casts:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 [wasRecentlyCreated] => )
To get all the ads you should use, Ad::all() not Ad::get().
Update your Ad model as one user has one ad:
public function ads()
{
return $this->hasOne('App\Ad','user_id');
}

Laravel 5 relationship belongsTo not working

First of all what I want to achieve it's quite simple : I want to use relationship belongsTo from Laravel 5 to get a value from another table. Like so
<?= $email->contact->first_name; ?>
But this is not working at all, instead, I get this error : Trying to get property of non-object. But when I do print_r($email) it shows me that $email is actually an object...So I really don't understand now.
This is (a part of) what I have in my Controller :
$emails = Email::where('user_id', '=', Auth::user()->id)->take(6)->orderBy('created_at', 'desc')->get();
This is what I have in my view :
foreach($emails as $key => $email) {
echo $email->contact->first_name; ?>
}
This is my model email
public function contact()
{
return $this->belongsTo('App\Contact', 'contact_id');
}
As you can see, I am using a second parameter inside belongsTo method, which is 'contact_id' because I don't want to use 'user_id' as the foreign key.
EDIT:
This is my output of print_r($email) :
App\Email Object ( [connection:protected] => [table:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [id] => 88 [user_id] => 1 [group_id] => 1 [contact_id] => 79 [reminder_id] => 31 [subject] => Test task #3 [view] => 0 [view_hash] => $2y$10$NZ3B.UUqtRRl2opH5UwXOeRNeRROeZGdid6Crs4ShT1VptbZJS.iu [content] => blablabla
EDIT 2 :
This is my output of print_r($email->contact) :
App\Contact Object ( [fillable:protected] => Array ( [0] => title [1] => content ) [table:protected] => contacts [connection:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [id] => 86 [user_id] => 1 [group_id] => 1 [first_name] => Matthieu [last_name] => Boistoule [email] => m.boistoule#gmail.com [phone] => [company] => [title] => [website] => [industry] => [address] => [city] => [country] => [province] => [postal_code] => [lead_source] => [notes] => [created_at] => 2015-06-04 12:13:00 [updated_at] => 2015-06-04 12:13:00 ) [original:protected] => Array ( [id] => 86 [user_id] => 1 [group_id] => 1 [first_name] => Matthieu [last_name] => Boistoule [email] => m.boistoule#gmail.com [phone] => [company] => [title] => [website] => [industry] => [address] => [city] => [country] => [province] => [postal_code] => [lead_source] => [notes] => [created_at] => 2015-06-04 12:13:00 [updated_at] => 2015-06-04 12:13:00 ) [relations:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [casts:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 )

Laravel Eloquent Get Append Attributes With Find

I created a model with 10 append attributes and I can't get them with find() method. But when I convert returned object from find() to an array with method toArray() I can see them but I need them like an object.
This object was printed when I use find():
School Object
(
[table:protected] => schools
[fillable:protected] => Array
(
[0] => name
[1] => type_id
[2] => description
[3] => info_specialties
[4] => contacts
[5] => cover_name
[6] => financing_id
[7] => city
)
[guarded:protected] => Array
(
[0] => id
)
[appends:protected] => Array
(
[0] => type
[1] => short_type
[2] => school_url
[3] => cover_photo_url
[4] => cover_photo_thumbnail_url
[5] => city
[6] => municipality
[7] => appended_district_id
[8] => district
[9] => description_without_tags
)
[district_id] =>
[cover_photo] =>
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 24
[type_id] => 3
[name] => adasdasd
[description] =>
asdadasdasdasdqwdqd\qw\[dqw\d
[info_specialties] =>
qwqwdqwdqwdqwdqwd
[contacts] =>
qwdqwdqwdqwd
[cover_name] => SAbjfpe4m7.jpg
[financing_id] => 1
[city_id] => 18
[created_at] => 2015-01-31 20:56:06
[updated_at] => 2015-02-04 18:50:13
)
[original:protected] => Array
(
[id] => 24
[type_id] => 3
[name] => adasdasd
[description] =>
asdadasdasdasdqwdqd\qw\[dqw\d
[info_specialties] =>
qwqwdqwdqwdqwdqwd
[contacts] =>
qwdqwdqwdqwd
[cover_name] => SAbjfpe4m7.jpg
[financing_id] => 1
[city_id] => 18
[created_at] => 2015-01-31 20:56:06
[updated_at] => 2015-02-04 18:50:13
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
And when I use toArray:
Array
(
[id] => 24
[type_id] => 3
[name] => adasdasd
[description] =>
asdadasdasdasdqwdqd\qw\[dqw\d
[info_specialties] =>
qwqwdqwdqwdqwdqwd
[contacts] =>
qwdqwdqwdqwd
[cover_name] => SAbjfpe4m7.jpg
[financing_id] => 1
[city_id] => 18
[created_at] => 2015-01-31 20:56:06
[updated_at] => 2015-02-04 18:50:13
[type] => qdasdasd
[short_type] => asdasdasd
[school_url] => http://localhost:8000/school/24
[cover_photo_url] => http://localhost:8000/storage/cover_photos/SAbjfpe4m7.jpg
[cover_photo_thumbnail_url] => http://localhost:8000/storage/cover_photos/thumbnails/SAbjfpe4m7.jpg
[city] => sdasdasd
[municipality] => ÐÑкаква община2 от нÑкакъв облаÑÑ‚2
[appended_district_id] => 6
[district] => ÐÑкакъв облаÑÑ‚2
[description_without_tags] => asdadasdasdasdqwdqd\qw\[dqw\d
)
Here's an example of my class:
<?php
class School extends Eloquent {
protected $appends = array('type');
public function getTypeAttribute()
{
return Type::find($this->type_id)->name;
}
}
That's the nature of custom attributes that you get with an attribute accessor. They will only be evaluated when used (and then cached for later use). When you convert it to and array, all $appends properties are loaded.
If you have your model object you can just access them like any other attribute:
$school->type;

Categories