I want to get address data from customer table.
But my address data includes country, city and address.
Therefore I want to make address field to country, city and address three fields.
The following is my code
$result = Customer::select('customers.address AS country', 'customers.address AS city', 'customers.address')->get();
dump($result[0]); // First result
$temp = $result->toArray();
dd($temp[0]); // Second result
The following is my questions:
Why is country and city changed in attribute property? I didn't do anything.
It is magic. I change [country] to [countrys] and [city] to [citys].
The following is my new code
$result = Customer::select('customers.address AS countrys', 'customers.address AS citys', 'customers.address')->get();
dump($result[0]);
$temp = $result->toArray();
dd($temp[0]);
The countrys and citys didn't change in attribute property anymore.
The output picture:
Can anyone tall me why? It's really strange.
As per Laravel Documenation:
The toArray method converts the collection into a plain PHP array. If the collection's values are Eloquent models, the models will also be converted to arrays
Aslo
toArray also converts all of the collection's nested objects that are
an instance of Arrayable to an array. If you want to get the raw
underlying array, use the all method instead.
Laravel -> Collection -> toArray()
The following is my code to resolve my question [Why is country and city changed in attribute property?]
// Add the following code to Customer model
public function setCountryAttribute($value)
{
$this->attributes['country'] = $value;
}
public function getCountryAttribute()
{
return $this->attributes['country'];
}
public function setCityAttribute($value)
{
$this->attributes['city'] = $value;
}
public function getCityAttribute()
{
return $this->attributes['city'];
}
Related
I am sure there is a similar question, but I am unsure what to search to find my answer (If you know what I am looking for, please just comment with the link or whatever you do). I need a way to pull all the countries allowed on a particular page (Just the name column in DB) and put it in an array. example:(United States, Canada, New Zealand, etc..) but in the database I have it set up when the pages are created to insert in the DB an array of the id #'s (id column in countries table) works perfectly fine inserts as (1,22,30, etc) 0 is not a countries table row, but I use 0 as the country ID for All Countries. I am just unsure where to start or how to "foreach" this. Here is a little bit of what I am trying to achieve in my Laravel controller:
public function view(Page $page, Request $request)
{
// $page->countries = array of country id's (1,12,14,etc)
// How do i retrieve the country names
$ip = $request->ip();
$geo_info = \Location::get($ip);
$geo_country = $geo_info->country;
$countries = Country = ??
// How do I add the country names to an array (United States, Mexico, etc)
// instead of the country id's while keeping the id's so its smaller data in the column for the page countries
if(in_array($geo_country, $countries)) {
return view();
} else {
return 'Country not allowed.';
}
}
How exactly would I format to pull the countries by the IDs and then attach only the country's names column together in an array with ", "?
Sorry if this post is confusing, I am honestly confused myself.
Assuming you have a Country model and corresponding table with country ID and country name, I think you are looking for a simple WHERE IN query, eg:
// $page->countries = string list of country ids "1,2,3 ..."
// Let's convert that to an array and use is in our query
$allowed_countries = explode(',', $page->countries);
$countries = Country::whereIn('id', $allowed_countries)->get();
That should give you a Collection of your allowed countries. Now to check if $geo_country is in that collection (assuming the country name is in a field called name), you can use the contains() Collection method:
if ($countries->pluck('name')->contains($geo_country)) {
return view();
} else {
return 'Country not allowed.';
}
Could probably do something like this. Just create a empty array, loop then set the persisting data to that array. Hopefully this gives you an idea.
$countries = array();
foreach($geo_country as $countries) {
$countries[] = $geo_country;
}
Hi I am trying to over ride the property value using a foreach loop.
Here is what I am trying
$runningLeagues = $this->getLeagueListing('running', $clubIds);
// chanage city
foreach($runningLeagues as $d){
$d->club->city = $d->city;
}
return $runningLeagues;
But if I just return from the loop I get the correct result. So the below code works.
foreach($runningLeagues as $d){
$d->club->city = $d->city;
return $d; // it shows the overridden city. It works.
}
I have city property inside club object and outside object as well. I want to change the city inside the club object from the outside city property value.
It keeps the values same. It doesn't change.
Update
it seems it updates with the last item. So if last properties city is 'ss' it will add 'ss' in all the properties.
Any idea whats happening?
Thank you.
Try this:
First. Laravel collection each() method:
$runningLeagues->each(function ($d) {
$d->club->city = $d->city;
});
Second. By collection index:
foreach($runningLeagues as $k => $d) {
$runningLeagues[$k]->club->city = $d->city;
}
I have 2 tables in the database employee(Ename,id,manager_id) and manager(name,id).1 manager has multiple employees under him.
The first line extracts all the employees under 1 manager and creates a list of it. Now, I wish to extract the name of each employee name from the retrieved employee id. How do i access each element of the list? This is what i have tried and it throws errors
$emplyeeId=DB::table('employee')->where('manager_id', $givenManagerId)->lists('id');
for ($i=0;$i<listCount;$i++)
{
$Ename = DB::table('employee')->where('id', $emplyeeId($i))-> value('Ename');
}
By looking at the docs (and scroll down a little bit, you find a method named pluck. This will return an array with all the values of the given column.
In your case this would be:
$names = DB::table('employee')->where('manager_id', $givenManagerId)->pluck('Ename');
// This will return the following array:
['Kevin', 'Tom', 'Tina', ...]
Laravel pluck method ,for example get just name from data
$names = DB::table('employee')->where('manager_id', $givenManagerId)->pluck('Ename');
Second method with array_filter
$names = DB::table('employee')->where('manager_id', $givenManagerId)->get()->toArray();
$justNames=array_filter($names, function ($ar) { return $ar['Ename']; });
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]);
}
I have the following line $totalRequests = \App\LoanRequest::all(); which returns all records, Along a model Applicant which have One to Many relation with LoanRequest.
So i can access applicants data with $totalRequests[0]->applicant->region for with foreach().
Question :
Now i want all the distinct regions in the Applicant or table applicants from the LoanRequest::all() model and want to push those regions to an array which then i can pass to a view.
What i am trying :
$totalRequests = \App\LoanRequest::all();
foreach($totalRequests as $applicant){
$filterMeta = [];
array_push($applicant->aplicant->region, $filterMeta);
}
It throws:
Indirect modification of overloaded property App\Applicant::$region has no effect
If you want to push the region to the array, you're using the wrong argument order. Also, shouldn't you be initalising the filter meta array outside of the loop?
$totalRequests = \App\LoanRequest::all();
$filterMeta = [];
foreach($totalRequests as $applicant){
array_push($filterMeta, $applicant->aplicant->region);
}