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);
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'm having a problem with laravel., I'm trying to send the variable $codes on my view :
$codes = Code::where('user', $id)->get();
return view('user.edit', ['user' => $user,'codes' => $codes]);
But I get that error Too few arguments to function e(), 0 passed in
The variable $user goes well but not the variable code, anyone have an idea for a solution?
Thank you all
use this:-
return view('user.edit')->with('codes',$codes);
you can get the user with :-
Auth::user()->name;
I'm pretty sure, if you are following Laravel naming conventions, in your eloquent query the column name is user_id and not user. Also if you are using such querys, and you have foreign, you can define relations in your models, like the following:
Code Model
public function user
{
$this->belongsTo(User::class);
}
User Model
public function codes
{
$this->hasMany(Code::class);
}
Instead of line
$codes = Code::where('user', $id)->get();
You can go with:
$codes = $user->codes();
If you don't have the foreigns in your database
$codes = Code::where('user_id', $id)->get();
If non of these helps, please dd() your variables before returning the view, and share result with me in comment. In laravel 7 how you return view is good.
I'm using Laravel 5.7 and would like to return a view, with query strings. E.g. below is what I'm doing right now.
return view('cart', compact('somevar'))
This takes the user to mydomain.com/cart
I'd like to add query strings, e.g. so the user goes to mydomain.com/cart?id=123
How would I do this?
You can achieve this :
return view('cart', ['id' => $id]);
Your data should be an array with a key-value pair.
You can also use with method:
return view('cart')->with('id', $id);
Also, you can use compact :
return view('cart', compact('id'));
For more, please refer Passing Data To Views
You can pass data to view like this:
return view('admin-panel.leave.index')->with($data);
And $data is an array that contain multiple values like this:
$data = array('count' => '7', 'id' => '4');
What your asking is not doable with this approach.Because, the main problem is /var/www/vendor/laravel/framework/src/Illuminate/View does not have what you are looking for. Only way to do that is return redirect()->route('cart', ['id' => 123]);
If this is really necessary, I think you need to redirect before call the /cart Something like this to your rootes/web:
Route::get('/addcart', 'CartsController#workaround');
Route::get('/cart', 'CartsController#showtheview')->name('cart');
Then in your CartsController:
public function workaround(){
// your code.....
return redirect()->route('cart', ['id' => 1]);
}
public function showtheview(){
//your code....
return view('cart', compact('somevar'))
}
Hope you get the logic.
I don't think it's possible. All the answers here forget that "somevar" has to be returned as well. Not only somevar compact data must be returned, but the URL must have variables too.
Put otherwise, you want internal blade data by compacting, but the URL must look different because it must have "&somevar2=xyz&somevar3=abc" appended.
Please correct me if I'm wrong, but not possible.
You can try this :
Route::get('cart/{id}', 'TicketsController#edit');
And in return:
return view('cart/'.$id, compact('somevar'))
Also you can try like this:
// app/Http/routes.php
Route::get('/cart/{id}', function ($id) {
return view('cart')->with('id', $id);
});
// resources/views/example.blade.php
The last part of the route URI is <b>{{ $id }}</b>
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'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'
]);