How to get php string values already set into an array - php

I get some values from laravel query builder. I would like to get and set these values into somewhere of my code. So i get undefined index exception message with the following codes :
laravel query :
$generatedRowValue = DB::table('myTableName')
->where('id', '=', 'someValue')
->where('anyOtherValue', '=', 'anotherValue')
->get();
I want to reach the values set in $generatedRowValue variable.
P.S. : Yeah i can get the values from the query, checked it out with var_dump(); dd(); etc. and i know my values are not undefined.
I'm new on this platform so i don't know how to reach these values.
Any help would be appreciated.
---UPDATE---
This is a simple HTML file which includes php partially.
<?php
$html = new simple_html_dom();
$html->load('<input type="text" value="'.$generatedRowValue['thevalueName'].'">')
?>
So i need $generatedRowValue['thevalueName'] 's value.

get() returns an array of objects. I think you want to use first() instead since you only expect one result.
$generatedRowValue = DB::table('myTableName')
->where('id', '=', 'someValue')
->where('anyOtherValue', '=', 'anotherValue')
->first();
By the way, you can also use the object syntax in your view (if you want)
$generatedRowValue->thevalueName

Related

How to fetch data in laravel 5.4 with out using array_push

its there's a way to optimize this code. I already google it but I don't know what the exact keyword to search, so I always failed to find the answer.
At this code I get the Approver List of ID 512 (requestor)
$approver_list = DB::table('users')
->leftjoin('approver_group_list', 'approver_group_list.user_id', '=', 'users.id')
->leftjoin('approval_roles', 'approval_roles.as_id', '=', 'approver_group_list.as_id')
->leftjoin('approver_requestor_list', 'approver_requestor_list.at_id', '=', 'approval_roles.at_id')
->where('approver_requestor_list.user_id', 512)
->get();
Then I use array_push to extract the data of approver_list, then I use the value of $result to get value in LeaveMain table.
$result = array();
foreach($approver_list as $al)
{
array_push($result , $al->user_id);
}
$leave_list = LeaveMain::whereIn('requestor_id', $result)->get();
My problem is, it is always need to use array_push to to extract data, or laravel have a way to optimize this code.
$approver_list = DB::table('users')
->leftjoin('approver_group_list', 'approver_group_list.user_id', '=', 'users.id')
->leftjoin('approval_roles', 'approval_roles.as_id', '=', 'approver_group_list.as_id')
->leftjoin('approver_requestor_list', 'approver_requestor_list.at_id', '=', 'approval_roles.at_id')
->where('approver_requestor_list.user_id', 512)
->pluck('id');
$leave_list = LeaveMain::whereIn('requestor_id', $approver_list)->get();
Basically you can pluck the id from the table itself, rather than fetching all the data and then taking the id later, and whereIn accepts collection as the second argument. so no need to cast to an array
The whereIn method filters the collection by a given key / value
contained within the given array: Link
You don't need to loop the approver_list values, you can use pluck method to retrieves all of the values for user_id.
$result = $approver_list->pluck('user_id')->toArray();
$leave_list = LeaveMain::whereIn('requestor_id', $result)->get();

How to solve Object of class stdClass could not be converted to string while returning data into view in laravel?

This is my Laravel controller code
public function switchInfo($prisw, $secsw){
$cur_sw_pair_id = DB::table('sw_pairs')
->select('sw_pair_id')
->where('pri_sw','=',$prisw)
->where('sec_sw','=',$secsw)
->get();
$infolist = DB::table('cust_sw_pair')
->select('interface','pri_sw_vlan','sec_sw_vlan','pri_sw_admin_status','sec_sw_admin_status','description')
->where('sw_pair_id', '=', $cur_sw_pair_id)
->get();
return view('switchinfo.switchinfoview',compact('infolist'));
}
when this code executing it gives Object of class stdClass could not be converted to string error. How can i solve this? How can i pass my infolist into switchinfoview view without getting this error?
You can use pluck() to get value of a column.
Your first query should like this:
$cur_sw_pair_id = DB::table('sw_pairs')
->where('pri_sw','=',$prisw)
->where('sec_sw','=',$secsw)
->pluck('sw_pairs');
Keep the second query as it is.
Hope it will help.
The issue is that ->get() returns a collection, which is a 0-indexed collection of results (rows) from the database. In your case, you want to be using the following query:
$cur_sw_pair_id = DB::table('sw_pairs')
->select('sw_pair_id')
->where('pri_sw','=',$prisw)
->where('sec_sw','=',$secsw)
->first();
And then accessing the property sw_pair_id of $cur_sw_pair_id, like so:
$infolist = DB::table('cust_sw_pair')
->select('interface','pri_sw_vlan','sec_sw_vlan','pri_sw_admin_status','sec_sw_admin_status','description')
->where('sw_pair_id', '=', $cur_sw_pair_id->sw_pair_id)
->first();
Then, in your view, you can access all the properties (anything in the ->select() statement) of $infolist using $infolist->PROPERTY_HERE
Note that using ->first() is essentially using the same as ->get();, but you have to access the results of ->get() using an index, so
$cur_sw_pair_id[0]->sw_pair_id
Hope that provides some insight for you.

Different values when use DB::raw() in second param on where()

I have next query in Laravel Eloquent:
$buildings = Building::select('buildings.*')->join(
DB::raw('('.
(
IngameBuilding::select('buildings.building_id', 'buildings.level')
->join('buildings', 'buildings.id', '=', 'ingame_buildings.building_id')
->toSql()
).
') as `added_buildings`'), 'added_buildings.building_id', '=', 'buildings.building_id')
->where('buildings.level', '>', 'added_buildings.level')
->get();
This query returns all allowed rows from base, but one row more. When I added DB::raw() in where() return values is valid.
Good-working code:
$buildings = Building::select('buildings.*')->join(
DB::raw('('.
(
IngameBuilding::select('buildings.building_id', 'buildings.level')
->join('buildings', 'buildings.id', '=', 'ingame_buildings.building_id')
->toSql()
).
') as `added_buildings`'), 'added_buildings.building_id', '=', 'buildings.building_id')
->where('buildings.level', '>', DB::raw('`added_buildings`.`level`'))
->get();
Why first code workig, hmm.. Wrong?
I'm not a big fan of Laravel at all.
I've got only small experience with this framework but i'm almost sure that where function accepts only a 'constant' values to be checked against.
If you'll get an output of this query using toSQL method on the query object you will see that eloquent will convert it as something like:
(...) where buildings.level > 'added_buildings.level'
so the condition checks if the buildings.level (whatever the type is)
is greater than the given string and not the column value.
Using the DB::raw you're getting the proper sql as the eloquent won't parse/convert it.
You would need to use whereRaw method I suppose.
http://laravel.com/docs/4.2/queries#introduction

request in PhpMyAdmin works, with Laravel Query Builder, doesn't seem to work

I have a problem that I can't fix :
this request works in PHPMYADMIN
SELECT cronState.name, command.name, parameter.keyword, eventParameterValue.value
FROM cronState, commandInstance, command, eventParameterValue, parameter
WHERE cronState.id = commandInstance.cronState_id
AND commandInstance.command_id = command.id
AND eventParameterValue.commandInstance_id = commandInstance.id
AND eventParameterValue.parameter_id = parameter.id
AND cronState.id =32
it returns :
name name keyword value
on20 DigitalPinSet State 1
on20 PwmPinSet DutyCycle 20
and it's ALL I want.
LITTLE EDIT : I use an AJAX Request(with a state_name) to this method which should returns the query result.
Now, when I try to implement it with the Laravel Query Builder, it returns some errors.
My code:
$cronState_id = DB::table('cronState')
->where('name', Input::get('state_name'))
->first();
$cronEvent = DB::table('cronState')
->join('commandInstance', 'commandInstance.cronState_id', '=', 'cronState.id')
->join('command', 'commandInstance.command_id', '=', 'command.id')
->join('eventParameterValue', 'eventParameterValue.commandInstance_id', '=', 'commandInstance.id')
->join('eventParameterValue', 'eventParameterValue.parameter_id', '=', 'parameter.id')
->where('cronState.id', '=', $cronState_id->id)
->select('cronState.name', 'command.name', 'parameter.keyword', 'eventParameterValue.value');
foreach($cronEvent as $result){
$ev = $result['keyword'];
}
return $ev;
The error for this code :
{"error":{"type":"ErrorException","message":"Undefined index:
keyword","file":"/var/www/stage/app/controllers/EventController.php","line":48}}
if I change
foreach($cronEvent as $result){
$ev = $result['keyword'];
}
return $ev;
to
return $cronEvent;
(just delete the foreach loop and rename the returning variable), I have this error :
{"error":{"type":"ErrorException","message":"Object of class
Illuminate\Database\Query\Builder could not be converted to
string","file":"/var/www/stage/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Response.php","line":361}}
So, how can access to my data in the object ? Maybe my Query Builder implementation doesn't works...
Thanks for any help !
There is missing get() at the end of that query builder, thus you don't run the query at all. That's it.

Eloquent and Pivot Tables in Laravel 4

I have a Poll table, a Students table, and a pivot table between them that includes a token and their three votes.
public function students()
{
return $this->belongsToMany('Student', 'polls_students')->withPivot('token','first','second','third');
}
While working out saving the poll results, I came across some odd behavior that I don't quite understand. I'm hoping somebody can explain what it is I'm missing:
$poll = Poll::find(Input::get('poll_id'));
foreach($poll->students()->where('students.id', '=', Input::get('student_id'))->get() as $student){
var_dump($student->pivot->token);
}
$student = $poll->students()->where('students.id', '=', Input::get('student_id'))->get();
var_dump($student->pivot->token);
In the above code, the foreach loop will successfully display the token, where the second one throws the exception Undefined property: Illuminate\Database\Eloquent\Collection::$pivot
What am I missing? Are these two calls not logically creating the same object? How is 'pivot' working on the first and not the latter?
You first example:
$poll = Poll::find(Input::get('poll_id'));
foreach($poll->students()->where('students.id', '=', Input::get('student_id'))->get() as $student){
var_dump($student->pivot->token);
}
Here $poll->students() retrieves a collection and because of foreach loop you get a single object in your $student variable and you can use $student->pivot->token
You second example:
$student = $poll->students()->where('students.id', '=', Input::get('student_id'))->get();
var_dump($student->pivot->token);
Here you are doing same thing, using $poll->students() you are getting a collection but this time you are not using a loop and trying to do same thing using $student->pivot->token but it's not working because you didn't define any index from which you want to get the pivot->token, if you try something like this
$student->first()->pivot->token
Or maybe
$student->get(1)->pivot->token
Or maybe you can use first() instead of get() like this
$student = $poll->students()->where('students.id', '=', Input::get('student_id'))->first();
Then you can use
$student->pivot->token
Remember that, get() returns a collection even if there is only one record/model.
$poll = Poll::find(Input::get('poll_id'));
foreach($poll->students as $student){
var_dump($student->pivot->where('student_id',$student->id)->where('poll_id',$poll->id)->first()->token);
}

Categories