I am trying to retrieve a list of genres which have a track assigned to them, I am using a whereHas eloquent query but feel a little lost.
I have the following
//model
public function workingGenre() {
$this->whereHas('tracks', function($query) {
$query->where('');
});
return $this->tracks()->first();
}
//controller (where i am passing variables etc)
$genres = Genre::with('tracks')->get();
//my view
#foreach($genres as $genre)
{{print_r($genre->workingGenre())}}
#endforeach
I'm aware that my Where is empty, currently my print_r returns the following:
1
App\Track Object
(
[table:protected] => Track
[fillable:protected] => Array
(
[0] => id
[1] => GenreId
[2] => Title
[3] => CreatedById
[4] => SelectedCount
[5] => ProducerName
[6] => SourceId
[7] => Published
)
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 6
[GenreId] => 4
[Title] => Guns Up
[CreatedById] => 1
[SelectedCount] => 0
[ProducerName] =>
[SourceId] => 1
[Published] => 1
)
[original:protected] => Array
(
[id] => 6
[GenreId] => 4
[Title] => Guns Up
[CreatedById] => 1
[SelectedCount] => 0
[ProducerName] =>
[SourceId] => 1
[Published] => 1
)
[relations:protected] => Array
(
)
[hidden: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] =>
)
**And so on**
A push in the right direction would be great, I am finding this whole eloquent stuff really useful but hard to wrap my head around.
Thanks
$genres = Genre::with('tracks')->has('tracks')->get();
foreach($genres as $genre)
//do what you need
endforeach
This way you will get Genre only if they Have Tracks and Eager Load them, if you dont want eager loading remove ::with('tracks')
Related
I have access to a collection called $files inside my blade. I am paginating and setting it to $allFiles.
<?php $allFiles = $files->paginate(12); ?>
In the blade beneath that, when I call:
<div>
{{ $allFiles->links() }}
</div>
, I am getting the error call to a member function links() on array. I am not sure what I am doing wrong.
EDIT: Adding more info and show what $allFiles is. I logged it in my code and add that below:
Object
(
[collection:protected] => App\Models\Collection Object
(
[connection:protected] => data
[table:protected] => collections
[timestamps] => 1
[fillable:protected] => Array
(
[0] => title
[1] => content
)
[casts:protected] => Array
(
[smart_collection] => boolean
[is_app_collection] => boolean
)
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 2
[title] => Test
)
[original:protected] => Array
(
[id] => 2
[title] => Test
)
[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] => *
)
)
[resultsCount:protected] => 19
[resultsProducts:protected] =>
[cache:protected] => Array
(
)
[splitOption:protected] =>
[context:protected] =>
)
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';
});
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');
}
I am working on a project using Laravel 4.2 and I have a line that retrieves some rows from a table called Gifts
$theGifts = Gift::whereIn('id', $giftImages)->get();
This variable $giftImages is an array and values are ordered like this
Array
(
[0] => 5
[1] => 2
[2] => 3
[3] => 4
)
When I output this one however $theGifts the retrieved results are not in the same order as the array of ids is and I need it to be in the same order..
How can I insure that the order is the same? I am using the data from the object in the controller, not the view by the way.
The printout of the Gift::all() looks like this (in case someone needs to compare)..
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 2
[name] => Banana
[image] => bananas-icon.png
[value] => 10
)
[original:protected] => Array
(
[id] => 2
[name] => Banana
[image] => bananas-icon.png
[value] => 10
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[1] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 3
[name] => Cherry Cake
[image] => Cherry-Cake-icon.png
[value] => 15
)
[original:protected] => Array
(
[id] => 3
[name] => Cherry Cake
[image] => Cherry-Cake-icon.png
[value] => 15
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[2] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 4
[name] => Coconut
[image] => coconut-icon.png
[value] => 5
)
[original:protected] => Array
(
[id] => 4
[name] => Coconut
[image] => coconut-icon.png
[value] => 5
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[3] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 5
[name] => Lemon
[image] => lemon-icon.png
[value] => 3
)
[original:protected] => Array
(
[id] => 5
[name] => Lemon
[image] => lemon-icon.png
[value] => 3
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
)
)
I believe this will do it for you:
Gift::whereIn('id', $giftImages)
->orderBy(DB::raw('FIELD(id, ' . implode(',', $giftImages) . ')'), 'ASC')
->get();
Taken from this question. You have to use DB::raw() since Laravel doesn't provide a native way to do this. Untested...
When paginating results in CakePHP 2.4.2 the $this->params['paging']['Post']['nextPage'] parameter in the view is always 1 for all pages. So is the prevPage parameter.
Here is how the paging data looks like on page 2
[paging] => Array
(
[Post] => Array
(
[page] => 2
[current] => 40
[count] => 1719
[prevPage] => 1
[nextPage] => 1
[pageCount] => 43
[order] => Array
(
[Post.created] => DESC
)
[limit] => 40
[options] => Array
(
[page] => 2
)
[paramType] => named
)
)
Here is my $this->paginate settings in the controller.
Array
(
[conditions] => Array
(
[Post.published] => 1
)
[contain] => Array
(
[Feed] => Array
(
[fields] => Array
(
[0] => id
[1] => board_id
)
[Board] => Array
(
[fields] => Array
(
[0] => id
[1] => title
[2] => key
)
)
)
[PostFeature] => Array
(
[fields] => Array
(
[0] => id
[1] => width
[2] => height
[3] => url
[4] => color
)
)
)
[fields] => Array
(
[0] => id
[1] => feed_id
[2] => post_feature_id
[3] => title
[4] => summary
[5] => author
[6] => url
[7] => score
[8] => published_when
[9] => board_count
[10] => like_count
[11] => liked
[12] => tagged
)
[order] => Array
(
[Post.created] => DESC
)
[limit] => 40
[maxLimit] => 40
)
I can't figure out why paginator is not sending the correct prev/next page numbers to the view?
Stupid me.
These parameters are not page numbers. They are boolean values if a next or prev page exists in the set. So the values are correct.
I'll leave this here to save other people the embarrassment.