I have a variable named $authorSpotlightData , while dd($authorSpotlightData); it returns an array in the same blade file: Now, while i want the data ‘name’ in my
in my blade file : it says Undefined array key “name” also Undefined array key 0.
this is my p tag. Both are not working.
<h5 class="card-title">{{ $authorSpotlightData[0]['name'] }}</h5>
<h5 class="card-title">{{ $authorSpotlightData['name'] }}</h5>
Result Undefined array key “name” also Undefined array key 0.
Some controller code that may be useful are:
Controler Function
public function getRandomAuthorSpotlight()
{
$data = $this->getAuthorSpotlightData();
// Get a random item from the array
$randomItem = $data[array_rand($data)];
return view('QuotesPartials.AuthorSpotlight', ['authorSpotlight' => $randomItem]);
}
**AppServiceProvider**
public function boot()
{
// Fetch author spotlight data using the controller method
$authorSpotlightController = new AuthorSpotlightController();
$randomItem = $authorSpotlightController->getRandomAuthorSpotlight();
$data = $randomItem->getData();
// Share the random item with all views
view()->share('authorSpotlightData', $data);
}
*Output of dd($authorSpotlightData);
array:1 [▼ // resources/views/QuotesPartials/AuthorSpotlight.blade.php
"authorSpotlight" => array:6 [▼
"id" => 2
"image1" => "Book1.jpg"
"image2" => "Book2.jpg"
"image3" => "Book1.jpg"
"name" => "Brijesh"
"description" => "This is Brijesh"
]
]*
I tried everything but couldn’t find where the problem is:
I think something is wrong with the AppServiceProvider as this is new to me.
while dd($randomItem[‘name’]); it returned me the name in my controller.php file, But in this AppServiceProvider.php it says Undefined array key “name”. also for and dd($data[‘name’]);
My goal is to have the name of the array and be on my p tag in my blade file.