Get last N entries and order by ascending - laravel

I'm building a simple message box. Each message belongs to a conversation. What I'm trying to do is get the last 5 messages in the conversation and order them by ascending.
InboxController.php
public function messages($hashedId)
{
$conversation = Conversation::where('hashed_id', $hashedId)->first();
$messages = Message::where('conversation_id', $conversation->id)
->latest()
->take(5)
->get();
$messages->orderBy('created_at','asc');
return MessageResource::collection($messages);
}
Error
BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.

Use sortBy function. Collection doesn;t have an function named orderBy.
$collection->sortBy('created_at');
Reference:- https://laravel.com/docs/5.8/collections#method-sortby

try this
public function messages($hashedId)
{
$conversation = Conversation::where('hashed_id', $hashedId)->first();
$messages = Message::where('conversation_id', $conversation->id)
->take(5)
->orderBy('created_at','desc')
->get();
return MessageResource::collection($messages);
}

Related

retrieving related field in controller index function gives error but ok in show function

I define the relation in Company table (where I added the plural):
protected $table = 'companies';
public function country() {
return $this->belongsTo(Country::class, "country_id")->withDefault(['country' => 'unknown']);
}
I also did the same in the Country model.
When I use the following code in the controller show function it works:
public function show (Company $company) {
$company->country = $company->country()->pluck('country');
But if I use the same code in the index function in a loop, I get an error "Call to undefined method stdClass::country()":
public function index (Company $company) {
if (request('tag')) {
$companies = Tag::where('name',request('tag'))->firstOrFail()->companies;
$companies->page_title = "Businesses matching tag '".request('tag')."'";
} else {
$companies = DB::table('companies')
->where([['is_active', '=', '1']])
->orderBy('company')
->get();
}
foreach($companies as $key => $thisCompany) {
...
$thisCompany->country = $company->country()->pluck('country');
}
I guess it is due to the fact that $company is created in the loop and not passed through the function like in show(Company $company)... but I could not find how to solve this issue... so help will be appreciated.
I have added the model in the argument of the function and change the name of the $company variable in the loop by $thisCompany to avoid confusion with the $company model.
No error but the field $country->country does not contain the name of the country but "Illuminate\Support\Collection {#443 …1}"
Why is it so complicated? Please help...
Paul, sorry, I think I didn't explain myself well in the comments.
What I meant by "What about if you change DB::table('companies') by Company?", is to stop using DB Query Builder to use the Eloquent Company model.
Specifically in this segment of code:
$companies = DB::table('companies')
->where([['is_active', '=', '1']])
->orderBy('company')
->get();
So, it could be:
$companies = Company::where([['is_active', '=', '1']])
->orderBy('company')
->get();
The explanation is that in the first way (with DB Query Builder), the query will return a collection of generic objects (the PHP stdClass object) that do not know anything about the Company and its relationships.
On the other hand, if you use the Eloquent model Company, it will return a collection of Company objects, which do know about relationships, and specifically the relationship that you have defined as country.
Then, when you loop over the collection, you will be able to access the country relation of each Company object:
foreach($companies as $key => $company) {
//...
$company->country = $company->country()->pluck('country');
}
Finally, your code could looks like:
public function index () {
if (request('tag')) {
$companies = Tag::where('name',request('tag'))->firstOrFail()->companies;
$companies->page_title = "Businesses matching tag '".request('tag')."'";
} else {
$companies = Company::where([['is_active', '=', '1']])
->orderBy('company')
->get();
}
foreach($companies as $key => $company) {
//...
$company->country = $company->country()->pluck('country');
}
//...
}

Laravel 2 functions querybuilder

I would like to query multiple tables from multiple databases.
My first query should return an array of sites_id.
The second request should return manufacturers_id based on the sites_id returned in the first request.
How do I set this up?
My repository:
public function getSitesImminence()
{
$arraySites = PublicationSite::
select('sites_id')
->where('publi_code', '=', 'imminence_intercab')
->where('publi_status', '>', '0')
->get();
return $arraySites;
}
public function getManufacturersMls($arraySites){
$arrayManufacturers = Site::
select('manufacturers_list')
->where('sites_id', '=', $arraySites)
->get();
return $arrayManufacturers;
}
My controller:
$toto = $siteRepository->getSitesImminence()->getManufacturersMls($arraySites);
My error:
Method getManufacturersMls does not exist.
This isn't tested but your code should look more like this:
$arraySites = $siteRepository->getSitesImminence();
$toto = $siteRepository->getManufacturersMls($arraySites);
and your function should be a little different, using whereIn:
public function getManufacturersMls($arraySites){
$arrayManufacturers = Site::
select('manufacturers_list')
->whereIn('sites_id', $arraySites)
->get();
return $arrayManufacturers;
}

Orwhere has method does not allow null

enter image description hereI am trying to implement a many to many relationship search with 2 models.
i get input from multiple checkbox values and want to search for items that match A or B when there is an input of data.
I read this url and wrote the same logic.
https://laracasts.com/discuss/channels/laravel/many-to-many-relationship-with-2-pivot-table-data-search
public function search(Request $request)
{
$languages = $request->lang;
$fields = $request->field;
$agencies = Agency::with('languages')->with('specialized_fields')
->orWhereHas('languages', function($query) use ($languages) {
$query->whereIn('language_id', $languages);
})
->orWhereHas('specialized_fields', function($query) use ($fields) {
$query->whereIn('specialized_field_id', $fields);
})
->get();
dd($agencies);
}
i expected to achieve A or B search but instead I got this error.
Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given, called in /var/www/jtf/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php on line 907
it seems that it causes this error if either A or B is null, but why??? Does the OrWhereHas method work only when theres an input??
/added info/
my error message
my agency model
class Agency extends Model {
protected $guarded = [
'id'
];
public function languages(){
return $this->belongsToMany('App\Language');
}
public function specialized_fields(){
return $this->belongsToMany('App\SpecializedField');
}
public function region(){
return $this->hasOne('App\Region');
} }
I believe it's because either $languages or $fields is null.
Since ->whereIn() is expecting an array, but you're passing null.
You just need to make sure you're passing an array.
$languages = array_filter((array) $request->lang); // cast to array & remove null value
$fields = array_filter((array) $request->field);
$agencies = Agency::with('languages', 'specialized_fields')
->orWhereHas('languages', function($query) use ($languages) {
$query->whereIn('language_id', $languages);
})
->orWhereHas('specialized_fields', function($query) use ($fields) {
$query->whereIn('specialized_field_id', $fields);
})
->get();
I'm speculating that you started your where query chain with an orWhereHas() which may have caused the problem, try starting with whereHas() instead.
public function search(Request $request){
$languages = $request->lang;
$fields = $request->field;
$agencies = Agency::with('languages', 'specialized_fields') // you can get away by just using one with(), not needed but its cleaner this way
->whereHas('languages', function($query) use ($languages) { // previously orwherehas
$query->whereIn('language_id', $languages);
}) ->orWhereHas('specialized_fields', function($query) use ($fields) {
$query->whereIn('specialized_field_id', $fields);
})
->get();
dd($agencies);
}

Property [***] does not exist on this collection instance Laravel eloquent relationship

In my Post Model
public function user()
{
return $this->belongsTo('App\User');
}
And in the User Model
public function posts()
{
return $this->hasMany('App\Post');
}
Now I am trying to get the comments of a specific user
$user= User::where('name', 'like', '%Mat%')->first();
return $user->posts->comment;
But it shows
Property [comment] does not exist on this collection instance.
The user has many posts which therefore returns a collection, you will need to loop over this to get your comments out. I.e.
$user = User::where('name', 'like', '%Mat%')->first();
$user->posts->each(function($post) {
echo $post->comment;
});
See the documentation on Laravel Collections
I think you can try this :
$user= User::with('post')->where('name', 'like', '%Mat%')->get();
$postComment = array();
foreach($user->post as $post){
$postComment = $post->comment;
}
return $postComment;
Hope this help for you !!!
If you want to have all comments you can use the following code:
$comments = [];
$user = User::where('name', 'like', '%Mat%')->with(['post.comment' => function($query) use (&$comments) {
$comments = $query->get();
}])->first();
return $comments;
Property [comment] does not exist on this collection instance.
The above error occurs because the Posts function returns a collection. Now you will have to traverse through each element of the collection.
Since, you are returning $user->posts()->comment, I am assuming you need it in the form of an array and don't have to simply echo them out, one by one. So you can store them all in an array & then process it whatever whay you like.
$comments = array();
$user->posts()->each(function $post){
$comments = $post->comment;
}
return $comments;
For greater insight, into this collection function read:
https://laravel.com/docs/5.4/collections#method-each

Eloquent nesting with clause in the middle relation

I have the following relationships:
User [many-to-many with] Assignments
Assignments [many-to-many with] Badges
The assignment_user pivot table has a flag 'is_finished' which I need to be true
These are my relationships in the matching models:
User.php
public function assignments()
{
return $this->belongsToMany('Assignment')->withPivot('is_finished')->withTimestamps();
}
Assignment.php
public function badges()
{
return $this->belongsToMany('Badge', 'badge_assignment')->withTimestamps();
}
Badge.php
public function assignments()
{
return $this->belongsToMany('Assignment', 'badge_assignment')->withTimestamps();
}
I'm trying to get all the Badges for my User's finished Assignments. This was my query:
$user = User::with(array(
'assignments' => function($query) {
$query->where('is_finished', '=', true)->orderBy('updated_at', 'desc');
})
)
->with('badges')
->username($name)->first();
However this is rendering the error "Call to undefined method Illuminate\Database\Query\Builder::badges()" when I use foreach($user->assignments->badges ...).
I then tried to get badges inside the scope of the first with, but I don't know how to reach is_finished:
$user = User::with(array(
'assignments.badges' => function($query) {
$query->where('is_finished', '=', true)->orderBy('updated_at', 'desc');
})
)
->username($name)->first();
With this I get "column not found", because the query is looking for is_finished within the badges, not the assignment_user relationship.
As I was having this issue I tried this just to be sure:
User::with('assignments.badges')->whereUsername($username);
... and I'm still getting "Undefined property: Illuminate\Database\Eloquent\Collection::$badges".
What am I doing wrong?
You need to load nested assignments.badges like this:
$user = User::with(array(
'assignments' => function($query) {
$query->where('is_finished', '=', true)->orderBy('updated_at', 'desc');
})
)
->with('assignments.badges')
->username($name) // do you have a scope for this or should it be whereUsername?
->first();

Resources