Laravel getting collection ordered by relationship table - php

I have 2 tables, one is magazine and the other is issues. I have set up their relationship like this:
Magazine model:
public function issues()
{
return $this->hasMany('App\Issue')->orderBy('date', 'desc');
}
Issue model:
public function magazine()
{
return $this->belongsTo('App\Magazine');
}
I have a query where I get magazines ordered by a column and then in foreach loop I am getting the first issue of each magazine and storing it in array:
$magazines = Magazine::with('issues')->orderBy('order')->get();
foreach ($magazines as $magazine)
{
$issues[] = $magazine->issues()->first();
$images[] = $magazine->issues()->first()->image;
}
But, I would like to make a collection instead of an array. I am not sure how can I do that kind of query or is there a way to somehow store values to a collection in a foreach loop?

There are several ways of doing this.
You can turn an array into a collection by doing the following.
$issuesCollection = collect($issues);
$imagesCollection = collect($images);
You could also initialize an empty collection and then use the push() method to append items to it. Check the documentation

Just use the map function:
$magazines = Magazine::with('issues')->orderBy('order')->get();
$firstIssues = $magazines->map(function($magazine) {
return $magazine->issues->first();
});
$images = $firstIssues->map(function($issue) {
return $issue->image;
});
The map method iterates through the collection and passes each value
to the given callback. The callback is free to modify the item and
return it, thus forming a new collection of modified items

Related

Create array key inside foreach loop in laravel

I'm fetching data from database, inside foreach loop i have to add one array index list_array
$list = Lists::where('name',$request->name)->get();
$Data=[];
foreach($list as $key => $list_row)
{
$list_row['list_array'][]=$list_row['data_1'];
$list_row['list_array'][]=$list_row['data_2'];
$Data[]=$list_row;
}
It should be of array type but when i declare it as array it is not working,
message: "Indirect modification of overloaded property
App\Models\Lists
Any solution to create list_array as an array index inside foreach loop. Thanks
You are not dealing with an array, you are dealing with an App/Models/Lists model. What you probably instead want to do is adding a custom attribute to the model. This happens one step before.
// your model
class Lists
{
protected $appends = ['list_array'];
public function getListArrayAttribute()
{
$data = [
// your array data from where ever
];
return $data;
}
}
You then can access (without adding it when you want to access the data) the new attribute from within the model, like so:
// where you access your data
$lists = Lists::where('name', 'whatever')->get();
foreach($lists as $list) {
dd($lists->list_array)
}
You can read more about the so called Accessors here:
https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor (Laravel 8)
https://laravel.com/docs/9.x/eloquent-mutators#defining-an-accessor (Laravel 9)
I hope I got the intention of your question right. From your question it's not very clear what data you want to add and where it is coming from. There might be other and better ways too.
Edit:
Based on the comment from #user3653474 the Accessor function could look like this.
public function getListArrayAttribute()
{
$data = [
$this->data_1,
$this->data_2,
];
return $data;
}
data_1 and data_2 are the column names in the table of the same model.

Updating multiple id from array list with eloquent

I've passed to the controller an array of id and it is collected inside the student variable. I want to update the database column "lecture_id_FK" for each id in the array. I'm not sure as to how to use the array id to find the students. New in laravel.
Controller
public function setLecture($lecture,$student)
{
$students = student::whereIn('student_id', $student)->get();
$students->lecture_id_FK = $lecture;
$students->save();
//if i type "return $student" will produce -> ai160064,ai160065
}
The whereIn method takes an array as the second argument. You can get all students by using the explode function. Following getting all the records you want to update, you can do an update on all of them with the update method in laravel. With that you might be left with some code like the following:
public function setLecture($lecture,$student)
{
$studentIds = explode(',', $student);
return student::whereIn('student_id', $studentIds)
->update(['lecture_id_FK' => $lecture]);
}

Laravel merge multiple collections and sort by corresponding datetime field

I have multiple collections merging into one, then sorting that one by datetime to ultimately create a timeline between the collections.
Heres the catch, the datetime columns to sort are different names.
Is there anything I can do to make this cleaner - possibly attach the foreach loop with the ->merge? Looks ugly with the foreach loop. note: code below works but I feel it's a lazy way out and might be slow with more items in the collection.
// Create timeline, sortby creation datetimes.
$TimelineItems = collect();
$TimelineItems = $Appointments->merge($lead->SalesResult);
foreach ($TimelineItems as $key => $TimelineItem) {
if(!empty($TimelineItem->appointment_created)) {
$TimelineItems[$key]->created_at = $TimelineItem->appointment_created;
}
if(!empty($TimelineItem->salesresult_created_timestamp)) {
$TimelineItems[$key]->created_at = $TimelineItem->salesresult_created_timestamp;
}
}
$TimelineItems = $TimelineItems->sortByDesc('created_at');
dd($TimelineItems);
The best solution would probably be to standardize your model objects to use standard date stamp fields - then you wouldn't need to transform them.
Failing that, you could use each() or transform():
// Create timeline, sortby creation datetimes.
$TimelineItems = collect();
$AppointmentTemps = collect($Appointments);
$SalesResultTemps = $lead->SalesResult;
$TimelineItems = $AppointmentTemps
->merge($SalesResultTemps)
->transform( function ($item) {
if(!empty($item->appointment_created)) {
$item->created_at = $item->appointment_created;
}
if(!empty($item->salesresult_created_timestamp)) {
$item->created_at = $item->salesresult_created_timestamp;
}
return $item;
})
->sortByDesc('created_at');
dd($TimelineItems);
The transform method iterates over the collection and calls the given callback with each item in the collection. The items in the collection will be replaced by the values returned by the callback:
See the docs for the transform() collection method for reference.

how to get all related model IDs in Yii?

Suppose $model hase some items (one to many relationship), So in Yii $model->items returns an array of item models. How can I get an array of IDs of related items. This means each element of returned array is an integer.
You should simply write your own function for this, e.g.
public function getItemsIDs()
{
$ids = array();
foreach($this->items as $item)
$ids[] = $item->id;
return $ids;
}
After you just have to call $model->itemsIDs.
EDIT : as darkheir said in its comment, you should consider using DAO.
Here is an example of direct query, run from Model:
$this->getDbConnection()->createCommand("SELECT id FROM items WHERE model_id = :modelId")->bindParam(":modelId", $model->id, PDO::PARAM_STR)->queryColumn();
In result you will get numeric Array() with IDs from the table as values.
Another variant.
Yii::app()->db->createCommand("SELECT id FROM items WHERE model_id=".$model->id)->queryColumn()
This will get all IDs from table as array

idiorm / paris has_many as_array result set

I'm having trouble getting the results of a has_many query using php idiorm/paris. Following the example from the paris site the has_many result for posts returns as an object.
That's great, and I can run through the object and access individual methods, but what I want to be able to do is pass the result set as an associative array off to my template engine for display.
Example:
class Post extends Model {
}
class User extends Model {
public function posts() {
return $this->has_many('Post'); // Note we use the model name literally - not a pluralised version
}
}
The api works this way:
// Select a particular user from the database
$user = Model::factory('User')->find_one($user_id);
// Find the posts associated with the user
$posts = $user->posts()->find_many();
I am able to access the posts object and print the result set like this:
// echo each post id
foreach ($posts as $post) {
echo $post->id;
}
What I'd really like to be to do though, is use as_array() to get the entire resultset as an associative array, limited by certain fields in the way as_array works for an individual row, e.g.
$post_list = $posts()->as_array(id,title,post,date);
This, or a call on something like $user->posts()->find_many()->as_array() don't work.
What is the correct way to access this type of result set using paris?
Adding this method to idiorm.php gives me the desired functionality.
public function find_array() {
if (func_num_args() === 0) {
return $this->_run();
}
$args = func_get_args();
$array = array();
foreach ($this->_run() as $r) {
$array[] = array_intersect_key($r, array_flip($args));
}
return $array;
}
Now I can call $post_list = $posts()->find_array(); or $posts()->find_array('id','title'); etc.
find_one returns a Model object, find_many returns an array of Models.
If you want to get the entire result set as an array of associative array, one solution should be to use array_map
function model_as_array($model) {
return $model->as_array();
}
$posts = $user->posts()->find_many();
$my_view->posts = array_map(model_as_array, $posts);
var_dump($my_view->posts);
or in php 5.3+ (not tested)
$aa_posts = array_map(function($model) {return $model->as_array();} , $posts);

Categories