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 !!}
Related
This is probably something simple, but it's doing my head in.
So, my layout blade template has this:
#include('layouts.partials.sidebar')
{{ $slot }}
#include('layouts.partials.footer')
#include('layouts.partials.scripts')
I create a view which loads a template. This I assume gets parsed in $slot.
return view('request', [
'boo' => 'Hoo'
]);
No problems, the page loads and the variable 'boo' is accessible as {{ $boo }} in the 'requests' template.
But my question is, how can I pass the 'boo' variable to an included file in the layout file? In this case the following:
#include('layouts.partials.scripts')
So, in 'layouts.partials.scripts' how can I access {{ $boo }}? At the moment I just get an undefined index error.
Thank you very much for the help.
#include('layouts.partials.scripts', ['boo' => 'Hoo'])
Laravel Docs
https://laravel.com/docs/8.x/blade#including-subviews
If you have a partial like a nav, header, or sidebar, part of the master layout from which you are composing other views. It requires data that doesn't change from one view to another, like navigation links. Then, instead of passing the data from each controller method, you can define a view composer in a service provider's boot() method:
Service Provider's boot method
public function boot()
{
View::composer('layouts.partials.sidebar', function ($view) {
//$links = get the data for links
return $view->with('links', $links);
});
}
Laravel Docs
https://laravel.com/docs/master/views#view-composers
I cannot seed my blade with variables from my controller.
Look at my controller:
public function formAdd()
{
return view('restaurant.categoryAdd')->with(['myvar' => 'xxx']);
}
In my view categoryAdd:
{{$myvar}}
This way, I got a 500 error. But if I change my view to:
#if (isset($myvar))
<p>Success!</p>
#endif
There is not 500 error, the page is ok, but the 'Success!' is not returned. It means $myvar is undefined. I tested various codes, but I still cannot seed my view.
I tried:
return view('restaurant.categoryAdd')->with(['myvar' => 'xxx']);
return view('restaurant.categoryAdd')->with('myvar', 'xxx');
return view('restaurant.categoryAdd',['myvar' => 'xxx']);
obs: I'm running XAMPP, and It's the only issue I had at this time.
obs2: The problem is not the 'return view' method, It's just working. The problem is my view, It's not receiving the variable.
So try this:
In your controller:
public function formAdd() {
$variable = 'Hello';
return view('restaurant.categoryAdd')->with('myvar', $variable);
}
In your view (if HTML, simply):
<?php
print $myvar;
?>
The problem was in camelcase. I changed the view to 'categoryadd' and It's working now.
I am getting an Undefined Variable error even though I have passed the variable through the controller using compact(), and I even tried passing the variable to the blade view like so return view('address/create', ['addresses' => $addresses]);
Route:
Route::get('/addresses/create', function () {
return view('address/create');
})->middleware(\App\Http\Middleware\CheckSuperUser::class);
Controller function:
public function create()
{
$addresses = Address::all();
return view('address/create', compact('addresses'));
}
View code (address/create):
<div class="col-md-4">
<legend>Available Addresses</legend>
#foreach($addresses as $address)
{{ $address }}
#endforeach
</div>
Error message:
ErrorException in 656cf590f275580a61ff680434f2a7bc0399cbdb.php line 51:
Undefined variable: addresses (View: C:\Users\xxx\xxx\xxx\dorf\resources\views\address\create.blade.php)
I tried clearing the cache and views, but that didn't help at all. Restarted the server and the computer and still same error. Any ideas what might be going on?
#qasimalbaqali You are giving wrong path You have to use like this
Route::get('/addresses/create','ControllerName#create')->middleware(\App\Http\Middleware\CheckSuperUser::class);
Replace ControllerName with your Controller.
I hope this will help you.
Try to pass this way
return view('address/create')->with('addresses', $addresses);
Updated
#foreach($addresses as $address)
{{ $address->address }}
#endforeach
I have a problem understanding how variables work inside the Laravel templating system, Blade.
I set the variables in the controller, I can see them in the first view I make, but not inside the next one.
I'm using a master.blade.php file that holds the header and footer of the page, yielding the content in the middle. I can use the variable inside the master.blade.php file, but not inside the content blade file (variable undefined).
For example, for the contact page:
CONTROLLER FUNCTION:
$this->data['page'] = "contact";
$this->layout->content = View::make('pages.contact');
$this->layout->with('data', $this->data);
MASTER.BLADE.PHP:
if ($data['page'] == 'contact')
{ //do something, it works }
#yield('content')
CONTACT.BLADE.PHP:
if ($data['page'] == 'contact')
{// do something. ErrorException Undefined variable: data}
Am I missing something or is it a known restriction?
Thanks!
The problem was that I was passing the variables only to the layout:
Instead of:
$this->layout->content = View::make('pages.contact');
$this->layout->with('data', $this->data);
I fixed it using:
$this->layout->content = View::make('pages.contact', array('data' => $this->data));
$this->layout->with('data', $this->data);
This way passing the variables to the layout, but also to that particular view.
Hope it helps someone.
i have the following route:
Route::get('notes/main', function(){
$destinations = Destination::where('show', '=','1')->get();
$notes = Destination::find($destination->id)->notes()->get();
return View::make('notes.main')
->with('destinations', $destinations);
});
//the relationship models:
<?php
class Destination extends Eloquent {
public function notes()
{
return $this->has_many('Note');
}
}
<?php
class Note extends Eloquent
{
public function destination()
{
return $this->belongs_to('Destination');
}
}
//View:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
{ $notes->destination->text }} // this isn't echoed
#endforeach
what's the correct way to filter this and define $destination->id
thanks
How would i Filter the notes in an if statement inside the loop ?
#if (isset($note->title) != 'shorttext')
#else
<p> {{ $note->text }} </p>
#endif
#endforeach
You use $destination->id in your Route but it seems to be not defined yet. The question is, what do you want to achieve with your code?
With:
$destinations = Destination::where('show', '=','1')->get();
$notes = Destination::find($destination->id)->notes()->get();
you are getting only the Notes of one specific destination (so far, $destination is not defined. You could use Destination::all()->first() to get the first, or Destination::find(id), with ID being replaced with the primary key value of the destination you need).
But I guess you don't want it that way. From your Output it seems like you want to have an output with each destination and below each destination the corresponding Notes.
Controller:
//gets all the destinations
$destinations = Destination::where('show', '=','1')->get();
return View::make('notes.main')
->with('destinations', $destinations);
View:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
#foreach($destination->notes as $note)
{{ $note->text }} <br>
#endforeach
<hr>
#endforeach
Didn't test this, but this way your View would show you all your Destinations and for each Destination all the Notes.
In your route, your not sending $notes variable to the view.
How I would do it:
$destinations = Destination::where('show', '=','1')->get();
$destinations->notes = Destination::find($destination->id)->notes()->get();
Then in view:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
{{ $destination->notes->text }}
#endforeach
So first you asked a question, I gave you a correct answer.
You marked my answer as accepted, but later on you edit your initial question to add another question (for which you should create a new thread instead).
Then you create your own answer which is only answering part of your initial question and mark that as the good solution?
I could tell you why your isset and empty did not work, but why should I if you don't value my help at all?
OK
for the last part this worked out at the end
function isset and empty didn't work , strange:
#if ($note->title == 'shorttext')