Undefined variable Laravel Foreach - php

I want to foreach some data but it says it doesn't know the variable:
The error I get:
ErrorException in dbda158712a631f22ffd888cd244c74e60f3a433.php line 51:
Undefined variable: albums (View: /var/www/clients/client2/web2/web/resources/views/album.blade.php)
Here is my code:
Album.blade.php
#foreach($albums as $others)
<option value="{{$others->id}}">{{$others->name}}</option>
#endforeach
My album controller function
public function getAlbum($id)
{
$album = Album::with('Photos')->find($id);
return View::make('album')
->with('album',$album);
}
public function getList()
{
$albums = Album::with('Photos')->get();
return View::make('index')
->with('albums',$albums);
}

You are passing album variable with view Album.blade.php, which is single object, not array of object so you can't iterate in a loop.
I think you are doing a mistake.
You want to do foreach in index.blade.php, because here you are passing the albums variable.
or
you need to return view album.blade.php in your getList function.

Try This Code.
public function getAlbum($id)
{
$albums=albums::with('Photos')->get();
$album=albums::with('Photos')->find($id);
return view('album',compact('albums','album'));
}

Related

Laravel Call to undefined method App\\ Model ::mapInto(), vendor\\laravel\\framework\\src\\Illuminate\\Support\\Traits\\ForwardsCalls.php

I'm trying to
public function show(Product $product)
{
return ProductDetailResource::collection($product);
}
Call to undefined method App\ Model ::mapInto(), exception:
BadMethodCallException file
vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php
then google and cant find any results
just need to replace
public function show(Product $product)
{
return ProductDetailResource::make($product);
}
instead of:
public function show(Product $product)
{
return ProductDetailResource::collection($product);
}
because collection for $products and collection of product.
but we have one item here

Why doesn't the controller recognise the data passed on from the model?

I'm trying to get data from my DB using a model, to then be used in the view. I return the query results to my controller and it gives me the undefined variable notice.
I tried first tried performing a select(get) statement in the controller, I then defined the result array as row before defining the specific row to be used, to which I pass on to my view. That threw an error, then I tried the same thing but with a model and a return to the controller:
controller.php
public function Home()
{
$this->load->model('Main');
$this->Main->getresults();
$this->load->view('header', $data);
}
model.php
public function getresults() {
$query = $this->db->get('table');
foreach ($query->result_array() as $row) {
$data = array(
'column' => $row["column"]
);
}
return $data;
}
view.php
<?php echo $column; ?>
I expect the return of $data to the controller for it to be used in the view, yet it still throws a notice of an undefined variable.
In your controller you don't assign and send data to view.
Change your code there:
public function Home()
{
$this->load->model('Main');
$data = $this->Main->getresults();
$this->load->view('header', $data);
}

PHP error: Undefined property: Illuminate\Database\Eloquent\Collection::$retails

Hello I'm trying get my objects on laravel like this.
But I'm getting this error.
PHP error: Undefined property: Illuminate\Database\Eloquent\Collection::$retails on line 18
Also this is my code.
public function index(){
$retails = Auth::user()->companies->retails->all();
return view('retails/retails', compact('retails'));
}
Company Model
class Company extends Model
{
public function retails(){
$this->hasMany(Retail::class);
}
}
Retail Model
class Retail extends Model
{
public function company(){
return $this->belongsTo(Company::class);
}
}
You have many companies. That is why you get a collection from
Auth::user()->companies
When you call ->retails from the collection instance, you get the exception you wrote.
You have to eager load the companies with retails and then loop through them.
Like:
$u = auth()->user();
$u->load('companies.retails');
$userRetails = [];
$u->companies->each(function($company) use (&$userRetails) {
$userRetails = array_merge($userRetails, $company->retails->toArray());
});
$userRetails = collect($userRetails);
return view('retails/retails', compact('userRetails'));
According to your code, you're trying to access collection from a collection which is impossible, you should individually fetch a collection first and do your process
public function index(){
$companies = Auth::user()->companies;
foreach($companies as $company) {
$retails_arr[] = $company->retails;
}
$retails = collect($retails_arr); // Gives Collection or
// Or if you want to send array, try this
$retails = $retails_arr; // Gives Array
return view('retails/retails', compact('retails'));
}
Try this out hope this helps..

Undefined variable: stations in view

I have undefined variable and it doesn't call variable.
I get missing argument 1 error when I try accessing a page. This is my code.
Part of the view:
#foreach($stations as $station)
<span> {{ $stations->station }} </span>
#endforeach
Controller:
public function show($id)
{
$stations = DB::table('stations')->pluck('station');
return view('configuration.configuration', $stations);
}
Route:
Route::get('configuration/', 'ConfigurationController#show');
Try with below code in controller
public function show($id)
{
$stations = DB::table('stations')->pluck('station');
$data['stations'] = $stations;
return view('configuration.configuration', $data);
}
View
#foreach($stations as $station)
<span> {{ $station->station }} </span>
#endforeach
You are directly passing the array value to view. It will not work like that. You have to assign the values to an array index and then call that index like $index_name in view. Then it will give you the desired output
or
public function show($id)
{
$stations = DB::table('stations')->pluck('station');
return view('configuration.configuration')->with(stations);
}
if you dont want to neft your $station into unused $data
Try with below code in controller:-
public function show($id)
{
$stations = DB::table('stations')->pluck('station');
return view('configuration.configuration', compact('stations'));
}

Laravel undefined variable inside a function

I get my $id from the url, but I couldn't find a way to use it in a function. How can I pass $id to the query function?
I tried global $id but I still can't get a value, just empty page.
My controller.
class BookController extends Controller
{
public function bookIndex($id, $slug)
{
// echo $id works without a problem
$findcover = Thing::with(array('cover' => function($query) {
$query->where('imageable_type', 'App\Models\Thing')
->where('imageable_id', $id);
}))->find($id);
$maincover = $findcover->cover;
}
Error:
ErrorException in BookController.php line 49:
Undefined variable: id
Line 49
->where('imageable_id', $id);
class BookController extends Controller
{
public function bookIndex($id, $slug) {
// echo $id works without a problem
$findcover = Thing::with(array('cover' => function($query) use ($id) {
$query->where('imageable_type', 'App\Models\Thing')
->where('imageable_id', $id);
}))->find($id);
$maincover = $findcover->cover;
}
Add the reserve word use to insert your $id along your query.

Categories