Laravel 4 DB::table - Foreach - php

I'm trying to run a sql query and return all values into an array of the tables names...
This is what I have so far:
$vehiclemodeldata = DB::table('model')->where('model_id', $viewData['model_id'])->get();
$row = $vehiclemodeldata->row();
foreach ($row as $key => $value){
$viewData['vehiclemodeldata_'.$key]= $value;
}
What are your throughts?
EDIT
I've tried this it seems to bring it back in an array but I can't access the array for some reason.
$VehicleModel = DB::table('model')->where('model_id', $viewData['model_id'])->get();
Thanks

This is because $vehiclemodeldata = DB::table('model')->where('model_id', $viewData['model_id'])->get(); actually returns an object instead of an array.
Your foreach loop is correct. You can later access properties in the array by using syntax like this $viewData['vehiclemodeldata_1']->yourProperty; which is used to access properties of objects.
Hope it answers the question.

$vehiclemodeldata = DB::table('model')->where('model_id', $viewData['model_id'])->first();
$viewData['vehiclemodeldata'] = $vehicle->toArray();
But why would you like to do this?
I would pass query result directly to view instead
$vehicle = DB::table('model')->where('model_id', $id)->first();
return View::make('whateverview', compact('vehicle'));
// view file
Model: {{ $vehicle->model }}
...

Related

How to dynamically set a parent table when accessing a collection?

I'm trying to fetch data from dynamically different tables depending on which user is logged on and if he is allowed to edit said table, what I thought was going to work was this:
$colaboradores = Colaboradore::where('email', '=', Auth::user()->email)->first();
$tables = DB::connection('mysql2')->select("SHOW TABLES LIKE 'intervencoes\_%'");
foreach ($tables as $object) {
$arrays[] = (array)$object;
}
foreach ($arrays as $array) {
$string = implode('', $array);
$test = $colaboradores->Niveis->$string->id;
}
And it outputs an error on the last bit of code saying
"Trying to get property 'id' of non-object"
I have been looking around for hours but couldn't find anything related.
This is all inside a livewire component by the way.
Thanks in advance.
Edit:
dd($colaboradores->Niveis)
dd($colaboradores->Niveis->$string)
Since $colaboradores->Niveis->$string returns a Collection, you need to iterate over it to get results.
$test = [];
foreach ($colaboradores->Niveis->$string as $obj) {
$test[] = $obj->id;
}
Then you can check the IDs in $test with dd($test); for example.
This can cause a problem if $obj is not a real object, so you could check it with:
if (!is_object($obj)) {
//Do something - Not an object!
}

laravel: How to store values in array, from collection using each method

$all_display = array();
$ad_all->each(function($ad){
$all_display[] =array('num'=>$ad->num);
});
print_r($all_display);
alternative
$all_display = array();
$ad_all->each(function($ad) use ($all_display){
$all_display[] =array('num'=>$ad->num);
});
print_r($all_display);
$ad_all has four rows, but when I print $all_display, it doesn't display anything.
Laravel has envisioned this with a Collection::only method.
var_dump($ad_all->only('num')->toArray());
Changing each to foreach works
$all_display = array();
foreach($ad_all as $ad){
$all_display[] =array('num'=>$ad->num);
}
But I don't know why each doesn't work..

How to pass data when we have data in multi dimensional array in Laravel?

I am trying to pass data to my view but I have data in multi-dimensional array.
I can iterate through it but I think using multiple foreach is not a good way because I have to use foreach loops for every single array to iterate through it.
Is there any alternative cleaner way?
Controller method code
public function city(city $job_test)
{
$data = privatejobcity::where('city_id', $job_test->id)->get();
$private_job = [];
foreach ($data as $values) {
echo "<pre>";
$private_job[] = private_jobadb::with('cities')->where('id', $values->private_jobabd_id)->get();
}
foreach ($private_job as $values1) {
echo "<pre>";
echo $values1;
}
die();
return view('frontend.jobTestCatagory.jobsByCities', compact('private_job'));
Results before die()
[{"id":31,"company_logo":null,"company_name":null,"job_desc":null,"company_phone":null,"sector_id":null,"company_eail":null,"created_at":"2017-09-03 04:38:50","updated_at":"2017-09-03 04:38:50","image":"public\/newpaper_jobs\/AAEAAQAAAAAAAAaHAAAAJDhmMTBjZmJlLWUxNmQtNDE3Zi1iY2Y5LTY1OWI1MzkwNzQ1YQ.png","contact_person":null,"job_title":null,"slug":null,"meta_tags":null,"meta_description":null,"cities":[{"id":4,"city_name":"Islamabad","pivot":{"private_jobabd_id":31,"city_id":4}}]}]
[{"id":32,"company_logo":null,"company_name":null,"job_desc":null,"company_phone":null,"sector_id":null,"company_eail":null,"created_at":"2017-09-03 04:44:09","updated_at":"2017-09-03 04:44:09","image":null,"contact_person":null,"job_title":null,"slug":null,"meta_tags":null,"meta_description":null,"cities":[{"id":4,"city_name":"Islamabad","pivot":{"private_jobabd_id":32,"city_id":4}}]}]
[{"id":33,"company_logo":null,"company_name":null,"job_desc":null,"company_phone":null,"sector_id":null,"company_eail":null,"created_at":"2017-09-03 04:50:28","updated_at":"2017-09-03 04:50:28","image":"public\/newpaper_jobs\/241_District-Council-Bahawalnagar.jpg","contact_person":null,"job_title":null,"slug":null,"meta_tags":null,"meta_description":null,"cities":[{"id":4,"city_name":"Islamabad","pivot":{"private_jobabd_id":33,"city_id":4}}]}]
[{"id":46,"company_logo":null,"company_name":null,"job_desc":null,"company_phone":null,"sector_id":1,"company_eail":null,"created_at":"2017-09-03 06:53:46","updated_at":"2017-09-03 06:53:46","image":null,"contact_person":null,"job_title":null,"slug":"943","meta_tags":null,"meta_description":null,"cities":[{"id":4,"city_name":"Islamabad","pivot":{"private_jobabd_id":46,"city_id":4}},{"id":3,"city_name":"Karachi","pivot":{"private_jobabd_id":46,"city_id":3}}]}]
I don't know it will work or not. but you can use join something like this.
$result = privatejobcity::join(
'private_jobadb',
'privatejobcity.private_jobabd_id', '=', 'private_jobadb.id'
)->with('cities')
->where('privatejobcity.city_id', $job_test->id)
->get();
If you post your table details then we can answer easily to your question.

How to manipulate this array?

I am a beginner in PHP. I am trying to make an operation in this array. I want to insert this array in my database like on to many in a table.But before the insertion i have to modify the array values.
this is my array.
$services=[0=>('id_e'=>91701,'id_s'=03),
1=>('id_e'=>'','id_s'=>01),
2=>('id_e'=>'','id_s'=>02)
];
It has to become like as follow.
$services=[0=>('id_e'=>91701,'id_s'=>03),
1=>('id_e'=>'91701','id_s'=>01),
2=>('id_e'=>'91701','id_s'=>02)
];
And then i want insert into the database. Any idea please?
Try this:
$id_e = null;
foreach ($services as &$row) {
if ($row['id_e']) $id_e = $row['id_e'];
else $row['id_e'] = $id_e;
}
unset($row);
demo

Returning an array of objects in a php function

I have multiple rows getting returned from a database query.
I am able to get just a row at a time, but I want to put the rows in an array of objects like this:
$trailheads[] = new StdClass;
Loop
{
$trailheads[] = $trailhead; // Put each object into the array of objects
}
But when I try to loop through each array, I am having trouble extracting the values of each row. What is the right way to loop through the returned object and get the values?
The code that does not work is pretty basic:
$returned_obj->o->trailhead_name
But I am actually hoping be able to loop through each array element.
Maybe try casting to array in your for loop, this is untested and may not work as is but should get you on the right track:
foreach ($trailheads as (array) $key){
// $key is now an array, recast to obj if you want
$objkey = (object) $key;
}
i'm assuming your using mysql since you did not say...
you can use thsi function mysql_fetch_object()
<?php
$trailheads = array()
$result = mysql_query("select * from mytable");
while ($row = mysql_fetch_object($result, 'trailhead')) {
$trailheads[] = $row;
}

Categories