Accessing nested array's in Laravel view - php

Controller Code:
public function claims($id)
{
$claims = Claim::whereBetween('created_at', [
'2016-03-01',
'2016-03-31'
])->get();
return View::make('pdfs.view', $claims);
}
In my view I'm getting a message that $claims is an undefined variable.
I know that with a single array I can simply access the array properties by callig a variable of the same name. i.e. $claims['id] would simply by $id
However I cannot do this with a multidimensional array, as $claims does not exist
Also, I cannot pass the data as an object using ->with('claims' $claims) as I'm generating a PDF and the library does not support that function.
Any ideas how I can access the data?

Because your array doesn't contains that key
return View::make('pdfs.view', $claims);
instead you can use compact like as
return View::make('pdfs.view', compact('claims'));
Or you need to do it somewhat assigning your values to same key like as
$claims['claims'] = Claim::whereBetween('created_at', [
'2016-03-01',
'2016-03-31'
])->get();
return View::make('pdfs.view', $claims);
or you can simply use Laravels way using with variable like as
return View::make('pdfs.view')->withClaims($claims);
Note : While using compact make sure your variable name must matches your string

Related

Laravel 8 override relationship value after get the result

My controller looks like this:
public function show($id)
{
$model = MyModel::with([
'model2.model3.model4:id,value',
...
]);
if (myCondition) {
unset($model->model2->model3->model4);
$model->model2->model3->model4 = Model4::where('value', 'Some Value')->first();
}
return $audit;
}
In certain condition I'd like to override the result from the query with another value from the Model4 to return the good data to the client.
But I want to know if there is another way with laravel to do that. Actually I have to use unset and then push the new content if I want to change the value of the model4 property. If I don't use unset the object isn't changed, the value new value assigned to model4 is ignored I don't know why I can't just write this line
$model->model2->model3->model4 = Model4::where('value', 'Some Value')->first();
So I want to know why I can't see changes in my json object when I don't use unset and I want to know if there is anotehr way to deal with laravel for my situation ?
You can simply use setRelation method.
if ($myCondition) {
$model->model2->model3->setRelation('model4', Model4::where(...)->first());
}

Undefined foreach variable laravel

hello guys im having a problem with passing variable from my controller to views, as it does not identify its variable, here is my code:
RegisterController.php
use App\angkatan;
public function index()
{
$select = Angkatan::all();
return view('/auth/register')->with('name', $select);
}
My Route
web.php
Route::get('register', 'RegisterController#index');
and my view
register
#foreach($name as $ps)
#endforeach
the error say
Undefined variable: name (0)
im very thankful if anyone can help
You are just passing the wrong way $select variable to your view.
when you use Illuminate\View\View::with method you should pass an associative array which as key => value pairs
return view('/auth/register')->with(['name' => $select]);
You can also use compact which allows to pass variable which are accessible inside of the scope of your controller to the view and the string passed as argument to that function will be the name of variable accessible inside of the view file
$select = Angkatan::all();
return view('/auth/register', compact('select'));
You can not pass the variable in this way to the view. You have to pass an array in the second parameter of the with() method - it should be something like this:
return view('greeting', ['name' => 'James']);
return view('/auth/register', ['name' => $select]);
you can pass a second massive parameter to the view,
and also in with method, you need to pass massive as I remember.

sortBy returns object instead of array

I have this code
$list = Elements::where('list_id', $id)->with('visitors')->get()->sortBy(function($t)
{
return $t->visitors->count();
});
return json_encode($list);
This code returns object, not array. How I can change it?
You should add ->values() if you want an actual JSON array in the end.
As you might add other manipulations like filters and transforms, I'd call ->values() at the very last moment:
return json_encode($list->values());
The reason for using ->values() over other options is that it resets the array keys. If you try returning some associative array (like ['name' => 'Roman'] or even [1 => 'item', 0 => 'other']), it will always get encoded as an object. You need to have a plain array (with sequential integer keys starting at 0) to avoid unexpected things that filtering and sorting will do.
You just need to call the ->all() Collection method, so
$list = Elements::where('list_id', $id)->with('visitors')->get()->sortBy(function($t)
{
return $t->visitors->count();
}
)->all();
this differs to the ->toArray() method because it will also cast to array also the object inside the collection, and not only the colletion itself (actually ->all() won't cast anything, it will just return the elements inside the collection)
$list = Elements::where('list_id', $id)->with('visitors')->get();;
This code return Collection Instance. $collection->sortBy(...); also is Collection instance. For get array in Collection you must be use or ->toArray(), or ->all()
In your case you can use
$list = Elements::where('list_id', $id)->with('visitors')->get()->sortBy(function($t) {
return $t->visitors->count();
})->toArray();

how to execute a function with the same name as variable evaluates in laravel

I have array which contains laravel model (App\User) attribute names like following:
$documents = array(
'passport_expire' => 22,
'residency' => 13
);
If I called these functions directly it will return a boolean value like:
App\User::find(2)->passport_expire;//will output true / false
I want to execute the functions inside a foreach:
foreach($documents as $type => $val){
// I want to call the attributes
App\User::find(1)->{$type};
//I want to call and execute App\User::find(1)->passport_expire and App\User::find(1)->residency
I read in php documentation about a way with similar approach called Variable functions but I don't know how to accomplish that in laravel.
How about
App\User::find(1)->$type;
Also you want to use string & variable
App\User::find(1)->$type . '_test';
However, if you want to use string as first, I think you need to create a variable beforehand and use first approach.
$type = "xx_$type_xx"
App\User::find(1)->$type;
Whereas, App\User::find(1)->{$type}; works,
But App\User::find(1)->{$type . 'text'}; doesn't work.
You're calling the attributes/properties as functions?
So far I think you meant variable variables.
If you want to access the properties like passport_expire or residency then you can try like
$user = App\User::find(1);
foreach (array_keys($documents) as $document) {
$documents[$document] = $user->{$document};
}
The above code will update the $documents array with the values of $user object's properties.
But, if you want to execute as functions as you mentioned, you need to try call_user_func or call_user_func_array

Strict standards error

The function parse_users returns an array.
I am doing the following in another function:
return reset($this->parse_users($records));
But I get a Strict Standards: Only variables should be passed by reference in...
Is it because I do a reset() on the function?
Do I have to do it this way:
$users = $this->parse_users($records);
return reset($users);
Or is something else?
That's it exactly. reset takes a reference to an array as a parameter, so it basically needs a real variable to reference -- even if it is a pass-by-reference value.
why didn't you try your
$users = $this->parse_users($records);
return reset($users);
?
It's correct
The one-line-solution uses an additional pair of brackets; this will turn the reference into a variable and omit the error:
return reset( ( $this->parse_users($records) ) );
From the PHP documentation for reset:
mixed reset ( array &$array )
reset() rewinds array's internal pointer to the first element and returns the value of the first array element.
The PHP reset() function accepts a reference to an array.
The strict warning is raised because you're directly passing the result of parse_users to reset, without a way to access that array in your other function.
If you're trying to return the full array (and not just the first value) after it's been reset, you should use:
$users = $this->parse_users($records);
reset($users);
return $users;
Alternatively, if you just want the first value from parse_users, you could just use:
$users = $this->parse_users($records);
return $users[0];
The reset function is only needed when you're iterating over the array and want to make sure you start from the beginning.

Categories