Displaying an array element in a View - Laravel - laravel

I am trying to output a variable like this: <h1>{{ $unitcode }}</h1>
I have this code making that variable:
// Validation was successful.
$inputcode = Input::get('unitcode');
// First validation is successful. Next to check that it is a correct unit code.
$unitcode = DB::table('unitcodes')->where('unitcode', $inputcode)->first();
if(!$unitcode) {
// Input code is wrong. The unit does not exist.
return Redirect::route('get-buy')
->with('global', 'That unit code does not exist. Try again.');
} else {
// Success! Unit code exists!
return View::make('showbooks')
->with('unitcode', $unitcode);
}
When I run everything it gives me this:
ErrorException (E_UNKNOWN)
Array to string conversion (View: ..BLAH goes on to display path to view.
How can I get it to display the variable I want that was pulled from the DB?
Thanks!

In Laravel's DB abastraction layer a row from a database table (result of a SELECT ... LIMIT 1 - DB::...->first()) is an array.
You can send any kind of variables using View::...->with but you need to use them properly in the template itself ('showbooks').
You most probably are doing a {{{ unitcode }}} in the template which is actually similar to executing an echo $unitcode. Now if $unitcode is an array and echo requires a string then PHP automatically tries to convert it and fails (ErrorException (E_UNKNOWN)
Array to string conversion...).
What you need to do is use it correctly:
{{{ unitcode['unitcode'] }}}
And this will work because $unitcode is a key-value dictionary where each key is a column from the DB table with its associated value.

Related

Retrieve Value from Field in Laravel Cast Array

I have a table named settings, in it, there are several columns, id,company_id, key, value.
In my Laravel model for Setting, I have the following cast:
protected $casts = [
'value' => 'array',
];
But when I go to retrieve data that has been stored, I can't.
For example, I have a record with the following value: "{\"default_remit_address\":\"2395\"}"
And when I go to retrieve the record in a Blade, it does pull it up correctly, but I'm not sure how to grab a specific value from the value field (like default_remit_address).
If I print the return "{{$settings->value}}" directly in the Blade, this is what I get:
{"default_remit_address":"2395"}
So how can I go one level deeper?
As this json object is being cast to an Array, you can just use regular array syntax to access it's contents.
$default_remit_address = Settings::find(1)->value['default_remit_address'];
or in your blade template
{{ $settings->value['default_remit_address'] }}

Laravel 5 how to pass single variable from controller and catch the same in blade view file?

I have passed $id from the controller as:
$data['getId'] = $id; // say value = 12
return view('Administrator.notification.index',$data);
However, in the view file when I used {{getId}} , it show me the error:
Use of undefined constant getId - assumed 'getId'
The $data variable is acessible from the view, not getId. $data is an array, getId is a key of the $data array and 12 your value according to the key value.
Try printing out something like: {{$data['getId']}}
If you want to see the output of the variable, for debugging purposes, use {{dd($data)}}
https://laravel.com/docs/5.6/views
update this line from
return view('Administrator.notification.index',$data);
To
return view('Administrator.notification.index',compact('data'));
and then in view u can access it like this,
{{ $data['getId'] }}
The error is you are missing the $ sign. It's {{$getId}} not {{getId}}.

Undefined property: Illuminate\Support\Collection::$election_name in view

In the controller, I store some data in an array.Then I want to send it in myelectionlist.blade.php
$my_election=[];
$i=0;
foreach ($election_list as $election_list)
{
$my_election[$i]=DB::table('election')
->where('id','=',$election_list->election_id)
->get();
$i++;
}
return view::make('myelectionlist')->with('election_list',$my_election);
I check with
return $my_election
It works fine. But In myelectionlist.blade.php when I write
#for($i=0;$i<sizeof($election_list);$i++)
{{$election_list[$i]->election_name}}
#endfor
It does not work.
Undefined property: Illuminate\Support\Collection::$election_name
happens, How to solve the problem?
There's a combination of problems that I would fix in order to bring clarity to your code and explain to you the problem. First of all in your code you have:
foreach ($election_list as $election_list)
I'll rename the variable for you to see:
foreach ($foo as $foo)
So what this does is that inside the foreach loop you will get all the values one by one, like expected but every time you store it to the initial variable. And once you are done with the foreach or break out of it earlier the $election_list will have the last used value. It might have unexpected results if you're trying to use the same $election_list later again. So I would suggest to use a differet variable names perhaps like $election_list as $election
Next, it's a little bit unclear why you would track the $index of the new elements by yourself. Instead you could just push into the array like this:
$my_election[] = $newObj
Actual error message:
Now for the actual error message: ->get() returns a Collection. So in the end you have an array full of Collections. So inside the for loop when you do $election_list[$i] - this will actually be a Collection object instead of the Model and thus the exception.
->get() will always get you the collection. For example with methods like ->first() and find/findOrFail you would get a single model. These functions might also be more appropriate to use since we are requesting with and id anyway.
Additionally, if this question is not a simplified version of your code then what I think you should actually do inside the controller:
$my_election = DB::table('election')
->whereIn('id', $election_list->pluck('election_id')) // pluck will give all the id's
->get();
This way you have a Collection of models assigned to $my_election.

accessing array values in view

I have the following code in zend:
$arrErrors=array();
if (!empty($this->post['submit']))
{
// Each time theres an error, add an error message to the error array
// using the field name as the key.
if (empty($this->post['client_name']))
$arrErrors['client_name'] = "Please Enter Client's name as it appears in the carrier software";
}
if i set $this->view->arrErrors=$arrErrors in the controller,
Can I access it as $this->arrErrors['client_name'] in the view?
i aplogize for a silly question.
Here is right approach:
$this->view->arrErrors=arrErrors['client_name'];
Use the above code in the controller

how to get a return value from ajax and php

ive set up a php file, called by ajax, to interact with my database. so far i have successfully used json to get the data back from the sql requests. now i am returning my own values from the php file, and trying to get the value back into my javascript. but they are returning as empty. this is how i am doing it:
// in the php file, gets data and database, and returns a number through echo
$result = 2;
echo $return;
//the javascript which calls the ajax and gets the value
var temp = ajaxdb(args);
showError(temp);
show error displays the variable in a div.
i know that my functions are working properly, as they work well when i return a json string, and parse it into an array. but how can i get my custom values from the php?
echo json_encode($return);

Resources