Laravel 4 error: Undefined variable [duplicate] - php

This question already has answers here:
How to pass data to view in Laravel?
(15 answers)
Closed 2 years ago.
I have made a list query that populates my drop down box. I have tried var_dump on the controllers part and it all went well, but whenever I tried to call my function on my blade template, it would return me an error: Undefined variable: categories (View: C:\wamp\www\airlines\app\views\content\onewayflight.blade.php)
What seems to be the problem here?
OnewayflightController.php
public function onewayflight()
{
$categories = DB::table('oneways')->lists('destination-from');
return View::make('content.onewayflight')->with('destination-from', $categories);
}
onewayflight.blade.php
{{ Form::select('destination-from', $categories) }}
routes.php
Route::get('flight/onewayflight','OnewayflightController#onewayflight');

You should use in Blade:
{{ Form::select('destination-from', $destination-from) }}
because in your method you used:
with('destination-from', $categories)
so you told that in Blade $categories have to be named $destination-from
However you cannot use - in variable name, so you should probably change it into:
with('destinationFrom', $categories)
and in Blade:
{{ Form::select('destination-from', $destinationFrom) }}

Related

how to fix Undefined variable: Products in view blade laravel 9.50.1 [duplicate]

Hey as i am passing a blade view which is having it own controller also i am including it into the view which does not have its own controller. it gives me an undefined variable error can any one help me how to it.
I have a view which does not have any controller only have Route like this Route::get('index', function () { return view('index'); }); in this view i am passing another view which having its own controller and also having some data from an array. but after using this view inside the view i get undefined variable error.
Two steps :
Declare & transfer $variable to View from Controller function.
public function index()
{
return view("index", [ "variable" => $variable ]);
}
Indicate where transferred $variable from Controller appear in view.blade.php.
{{ $variable }}
If you do not make sure, $variable is transferred or not
{{ isset($variable) ? $variable : '' }}
If this helps anyone, I was completely ignorant to the fact that my route was not hooked with the corresponding controller function and was returning the view directly instead, thereby causing this issue. Spent a good half hour banging my head till I realized the blunder.
Edit
Here again to highlight another blunder. Make sure you're passing your array correctly. I was doing ['key', 'value] instead of ['key' => 'value'] and getting this problem.
You can try this:
public function indexYourViews()
{
$test = "Test Views";
$secondViews = view('second',compact('test'));
return view('firstview',compact('secondViews'));
}
and after declare {{$secondViews}} in your main view file(firstview).
Hope this helps you.
public function returnTwoViews() {
$variable = 'foo bar';
$innerView = view('inner.view', ['variable' => $variable]);
return view('wrapper.view, ['innerView' => $innerView]);
}
This may be what you are looking for?
... inside your wrapper.view template:
{!! $innerView !!}
EDIT: to answer the question in the comment: In order to fetch each line you for do this inside your $innerView view:
#foreach($variable as $item)
{{ $item }}
#endforeach
... and in the wrapper view it will still be {!! $innerView !!}

Laravel pagination not working with tailwindcss [duplicate]

This question already has answers here:
Laravel Collection paginate does not exist
(2 answers)
Closed last year.
This is my controller file's index function
public function index()
{
$projects = Projects::All()->paginate( 5 );
return view('projects.index')->with('projects', $projects);
}
And, This is my blade file's pagination code
#foreach ($projects as $project)
{{ $project->links('pagination::tailwind') }}
#endforeach
But, Still, there's an error, that is saying
BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::paginate does not exist.
You have a typo. This is not correct:
{{ $project->links('pagination::tailwind') }}
Error 1: You are missing a 's':
{{ $projects->links() }}
Error 2: You do not have to put your links in the loop but outside the loop. normaly after the loop.
#foreach($records as $record)
{{ $record->field }}
#endforeach
{{ $records->links() }}
Suggestion: I prefer to tell laravel which paginator I am using in this file:
App/Providers/AppServiceProvider.php
public function boot()
{
Paginator::useBootstrap(); //bootstrap in this case
}
For tailwind, there is no need to specify anything, since it is the default.

How to display data passed to Blade view [duplicate]

This question already has answers here:
How to pass data to view in Laravel?
(15 answers)
Closed 1 year ago.
How to display variable ['attributes'] & ['products'] separately in blade
$data = [];
$data ['attributes'] = Attribute::active()->whereNull('option_id')->get();
$data ['products'] = Product::orderBy('id', 'DESC')->get();
return view('dashboard.productOption.edit', compact('prOption','data'));
You call them as you would any other associative array element:
#foreach ($data['attributes'] as $attribute)
<p>{{ $attribute }}</p>
#endforeach
The same applies for accessing products.

Pagination for the view page is not working in laravel5

I am trying to use pagination for my view pages but getting error as : Call to a member function links() on array
Limiting to display records using paginate in controller is fine,but issue is when passing to the view. Controller code is:
$referredProcedureBookings = Gateway::procedurebooking()->getReferredProcedureBooking()->paginate(5);
$new_records = [];
foreach ($referredProcedureBookings as $referredProcedureBooking)
{
$records['ProcedureId'] = $referredProcedureBooking->id;
$records['ProcedureName'] = $referredProcedureBooking->procedure_name;
$new_records[] = $records;
}
return view('procedurebookings',compact('new_records');
In my view its like this:
#foreach($new_records as $new_recordss)
<td><div class="col-md-2"><h5 class="panel-body">{{ $new_recordss['Name'] }}</h5></div></td>
<td><div class="col-md-2 col-md-pull-6"><h5 class="panel-body">{{ $new_recordss['Age'] }}</h5></div></td>
#endforeach
{{ $new_records->links() }}
Error i am getting is :Call to a member function links() on array.
Even if i use :{{ $new_records[links()] }}
Then i get :Call to undefined function links()
how should i use the links method?
links method available on $referredProcedureBookings
$referredProcedureBookings->links()
so pass $referredProcedureBookings to view first.

Laravel: print URL or default value in blade view

I have Post model which has user function returning User model - creator of the post.
class Post extends Model
{
/**
* Get the user that owns the post.
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
In blade view I achieved how to print author name
{{ $post->user->name or 'Anonymous' }}
Above code works, but it is very hmm sensitive?
For example, I tried change this code to:
{{ $post->user->name, 'Anonymous' }}
<?php $post->user->name or 'Anonymous' ?>
Results? Trying to get property of non-object error on line with this code.
Maybe I am skipping something simple but important. How to print URL or default value in blade view. Pseudo code what I mean:
{{ '' or 'Anonymous' }}
Try
{{ isset($post->user->name) ? '' : 'Anonymous' }}
If this doesn't work (I haven't check it) try something like this:
#if (isset($post->user->name))
#else
Anonymous
#endif
Actually the error is not because the code is sensitive, it is because you are actually trying to access non objects values.
To achieve what you are looking for:
{{ isset($post->user)?'' : 'Anonymous' }}

Categories