I've made a drop down list that takes 'destination-from' values on 'oneways' table from the database. Following this tutorial here: http://www.laravel-tricks.com/tricks/easy-dropdowns-with-eloquents-lists-method
But whenever I try to run it, It gives me this kind of error: Undefined variable: categories What seems to be the problem here? I am really new to this. A newbie on laravel.
Here are my codes:
onewayflight.blade.php
$categories = Category::lists('destination-from', 'title');
{{ Form::select('category', $categories) }}
onewayflightcontroller.php
public function onewayflightresults()
{
return View::make('content.onewayflight');
$list = DB::table('oneways');
$listname = Oneways::lists('destination-from');
$content = View::make('content.onewayflight', array('list'=>$list, 'listname'=>$listname));
}
I am not really sure of what I have left out one this. And I am also wondering if the model has something to do with this?
Additionally to #jd182 's advice (as I lack the reputation to comment, I use answer); are you sure that lists() function/facade returns some value other than null. PHP, sometimes, considers null values as undefined.
And your blade syntax is wrong. if you want to define a variable inside a blade file (which is not advised) you should wrap it around tag and work it as a regular php file.
The code in your template where you are fetching the categories needs to be surrounded by PHP tags otherwise it won't be executed. But better still - move it to your controller which is where it belongs.
Also in your controller you return the view at the top of your onewayflight() - nothing after this will be executed.
So change it work work like this and you should be ok:
onewayflight.blade.php
{{ Form::select('category', $categories) }}
onewayflightcontroller.php
public function onewayflight()
{
$categories = Category::lists('destination-from', 'title');
return View::make('content.onewayflight', array('categories' => $categories));
}
Also, in routes.php the route should look like this:
Route::get('/your-route-path', [
'as' => 'your_route_name',
'uses' => 'onewayflightcontroller#onewayflight'
]);
Related
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 !!}
I am having an issue by modifying the route for a view. I want instead of /company/id to show /company/id/name
the route:
Route::get('/company/{id}/{name}', 'PagesController#showCompany')->name('company.detail');
show method in controller:
public function showCompany($id){
$company = Company::find($id);
return view('company.show')->with('company', $company);
}
and in the view $companies is from a search controller - and it should get the results with a link to open the view
#foreach($companies as $company)
Show detail
#endforeach
if using only with id like /company/id works. What i am wrong?
A simple an elegant way (i think) is:
{{route('company.detail', ['id' => $company->id, 'name' => strtolower(preg_replace('/[^A-Za-z0-9-]+/', '-', $company->company_name))}}
You can have a friendly url name. I am sure that there are better solutions out there.
If you have more params in the route you can use an associative array and initialize each param name with a value.
the controller now is the same with the id.
I've followed the instructions on the Laravel documentation for pagination with appends([]) however I'm having a little trouble with the persistence of these parameters.
Say for example, I pass home?category=Cars&make=Tesla to my view. What is the best way to paginate with them Get requests?
Right now I've passed the category as a parameter to the view as (where category is the model i've grabbed findOrFail with the request('category');)
$category_name = $category_model->name;
And then in my view it's like so:
{{ $vehicles->appends(['category' => $category_name])->links() }}
But when I go between pages in the pagination, this $category_name value doesn't seem to persist. Whats the recommended way to achieve what I want?
Thanks.
You can append the query string in your controller when you paginate the result. I'm not sure if that was your only question or even regarding applying the query string as a condition. So here is a sample showing you how to do both. This should give you an idea of how to do it. I just assumed the column names in this example.
$category = request('category');
$make = request('make');
$vehicles = Vehicle::when($category, function ($query) use ($category) {
return $query->where('category', $category);
})
->when($make, function ($query) use ($make) {
return $query->where('make', $make);
})
->paginate(10);
$vehicles->appends(request()->query());
return view('someview', compact('vehicles'));
I want to build a custom find function that retrieves bands for a given genre, i have tried this but the function can't access to the parameter $genre:
public function findGenre(Query $query, array $options)
{
$genre = $options['genre'];
$bands = $this->find()->contain([
'Genres' => function($q){
return $q->where(['Genres.id' => $genre]);
}
]);
return $bands;
}
I can access the $genre outside the contain() method, but not inside it.
My question is, how can i pass the $genre var to the function($q) inside the contain method.
I found where the problem is, i had to use the keyword use after the function($q), so that part of the code will look like this
$bands = $this->Bands->find()->contain('Genres', function($q) use ($genre){
return $q->where(['Genres.name'=>$genre]);
});
Also,the contain() method returns all the data even if the bands don't belong to a genre, but when i replaced it with matching() it worked just fine.
I hope this will help anyone who is having a similar problem in the future.
I was facing same issue but now it's resolved. I will explain you step by step:
My tables are:
articles: id,name,status,created
tags:id,name,status,created
articles_tags: id,article_id, tag_id
my query is this:
I want to pass my $tag_data['slug'] in matching variable but
directly this variable is not working in query. So I put in simple
$uses variable and now it's working properly.
$uses = $tag_data['slug'];
$contain_article = ['Tags'];
$query = $this->Articles->find('All')
->where(['Articles.status' => '1'])
->contain($contain_article)
->matching('Tags', function ($q) use ($uses) {
return $q->where(['Tags.slug' => $uses]);
});
Please try this :-)
I have a piece of code and I'm trying to find out why one variation works and the other doesn't.
return View::make('gameworlds.mygame', compact('fixtures'), compact('teams'))->with('selections', $selections);
This allows me to generate a view of arrays for fixtures, teams and selections as expected.
However,
return View::make('gameworlds.mygame', compact('fixtures'), compact('teams'), compact('selections'));
does not allow the view to be generated properly. I can still echo out the arrays and I get the expected results but the view does not render once it arrives at the selections section.
It's oké, because I have it working with the ->with() syntax but just an odd one.
Thanks.
DS
The View::make function takes 3 arguments which according to the documentation are:
public View make(string $view, array $data = array(), array $mergeData = array())
In your case, the compact('selections') is a 4th argument. It doesn't pass to the view and laravel throws an exception.
On the other hand, you can use with() as many time as you like. Thus, this will work:
return View::make('gameworlds.mygame')
->with(compact('fixtures'))
->with(compact('teams'))
->with(compact('selections'));
I just wanted to hop in here and correct (suggest alternative) to the previous answer....
You can actually use compact in the same way, however a lot neater for example...
return View::make('gameworlds.mygame', compact(array('fixtures', 'teams', 'selections')));
Or if you are using PHP > 5.4
return View::make('gameworlds.mygame', compact(['fixtures', 'teams', 'selections']));
This is far neater, and still allows for readability when reviewing what the application does ;)
I was able to use
return View::make('myviewfolder.myview', compact('view1','view2','view3'));
I don't know if it's because I am using PHP 5.5 it works great :)
Laravel Framework 5.6.26
return more than one array then we use compact('array1', 'array2', 'array3', ...) to return view.
viewblade is the frontend (view) blade.
return view('viewblade', compact('view1','view2','view3','view4'));
Route::get('/', function () {
return view('greeting', ['name' => 'James']);
});
<html>
<body>
<h1>Hello, {{ $name }}</h1>
</body>
</html>
or
public function index($id)
{
$category = Category::find($id);
$topics = $category->getTopicPaginator();
$message = Message::find(1);
// here I would just use "->with([$category, $topics, $message])"
return View::make('category.index')->with(compact('category', 'topics', 'message'));
}
You can pass array of variables to the compact as an arguement
eg:
return view('yourView', compact(['var1','var2',....'varN']));
in view:
if var1 is an object
you can use it something like this
#foreach($var1 as $singleVar1)
{{$singleVar1->property}}
#endforeach
incase of scalar variable you can simply
{{$var2}}
i have done this several times without any issues
$data = [
'var1' => 'something',
'var2' => 'something',
'var3' => 'something',
];
return View::make('view', $data);