How to access class variables from within blade template - php

My controller index file looks like this:
class KJVController extends Controller
{
public $book = "Genesis";
public $chapter = 1;
public function index() {
$results = KJV::where('book', '=', $this->book)->where('chapter', '=', $this->chapter)->get();
return view("bible", compact('results'));
}
}
And in my blade template I want to be able to show the variables above ($book,$chapter).
I tried:
{{ KJVController::book }}
{{ KJVController->book }}
I can't seem to access it. How can I display this information in my blade template?

You need to pass these variables to the view first:
return view("bible", ['results' => $results, 'book' => $this->book, 'chapter' => $this->chapter]);
In the bible view:
{{ $book }}
Or you can just do this:
{{ app('App\Controllers\KJVController')->book }}

There's nothing wrong with your controller query. You just aren't returning the variable. You're trying to return the controller and as you're running ->get(), it returns an array. Therefore, you need to do this within the blade:
#foreach ($results as $book)
{{$book->book}}{{$book->chapter}}
#endforeach
Blade documentation

Related

Undefined variable: categories in blade file

I tried to create show function for main category but there is an error showing that variable categories is undefined. can help to solve it?
controller:
public function show($id)
{
$categories = Categories::find($id);
return view('Admin.categories.show',compact('maincategory'));
}
blade:
{{ $categories->main_cate }}
Route:
Route::get('/admin/show/{id?}',
['uses'=>'AdminCategoriesController#show','as'=>'Admin.categories.show']);
You do not pass the $catgories variable to the view. Do this:
return view('Admin.categories.show', compact('categories'));

Invalid argument supplied for foreach() in view file

may i know why this code showing invalid argument supplied for foreach() and how i should correct it ?
#foreach($project_details as $project) {
#foreach($project->projectusers as $users) {
{{ $user->name }}
}
#endforeach }
#endforeach
Controller
public function index()
{
$this->project = Project::with('projectusers')->get();
return view('projects.index', [
'project_details' => $this->project
]);
}
projectusers is a relationship ? Do you use the with() function in your Eloquent query to get that relationship within the collection ?
In addition, in your foreach code, you use the plural for users ($project->projectusers as $users), and then in the double brackets, you use the singular {{ $user->name }}
Remove the 's' from users in the foreach and it should work.
I would suggest reading up on foreach() a little more:
http://php.net/manual/en/control-structures.foreach.php
But if this is your entire code, you have not set any array values or assigned any values from a database or anything
$arr = array(1, 2, 3, 4);
Using the website above you can see it is quite simple to fill an array yourself depending on how many values you need to be added.
Just use compact as a second argument of your view()
public function index()
{
$projects = Project::with('projectusers')->get();
return view('projects.index', compact('projects'));
}
Then in your projects.index just call :
#foreach($projects as $project) {
#foreach($project->projectusers as $users) {
{{ $user->name }}
}
#endforeach }
#endforeach
Try this one:
public function index() {
$projects = Project::whereHas('user')->get();
return view('projects.index', compact('projects''));
in your blade:
#foreach($projects as $project)
{{$project->user->name}}
#endforeach

Passing variable from controller to view - Laravel

I'm trying to pass a variable from one view to a controller to another view. I'm not getting any errors, but when it gets to the last view, it doesn't show the variable like it's supposed to. In the first view, I'm just getting a name.
{{ Form::open(array('route' => 'form', 'method'=>'post')) }}
{{ $name = Form::text('name') }}
{{ Form::submit('Go!') }}
{{ Form::close() }}
Here is my HomeController.php.
public function view1()
{
return View::make('stuff');
}
public function postView1($name)
{
return Redirect::route('view2')->with($name);
}
public function view2($name)
{
return View::make('view2')->with($name);
}
routes.php
Route::get('/', array('as' => 'stuff', 'uses' => 'HomeController#stuff'));
Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController#postView1'));
Route::get('view2/{name}', array('as' => 'view2', 'uses' => 'HomeController#view2'));
view2.blade.php
{{ $name = Input::get('name') }}
<p> Hello, {{ $name }} </p>
So why isn't it showing up?
First you should change your postView function into:
public function postView1()
{
return Redirect::route('view2', ['name' => Input::get('name')]);
}
And your route:
Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController#postView1'));
into:
Route::post('form', array('as' => 'form', 'uses'=>'HomeController#postView1'));
Now, you should change your view2 function into:
public function view2($name)
{
return View::make('view2')->with('name',$name);
}
Now in your view2.blade.php you should be able to use:
<p> Hello, {{ $name }} </p>
You need to name the variable:
public function view2($name)
{
return View::make('view2')->with('name', $name);
}
class HomeController extends Controller {
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
}
public function index()
{
$data = array (
'title'=>'My App yo',
'Description'=>'This is New Application',
'author'=>'foo'
);
return view('home')->with($data);;
}
}
Try form will be if you using POST method why setting variable in route it will get directly on your function with post data.
{{ Form::open(array('url' => 'form', 'method'=>'post')) }}
{{Form::text('name') }}
{{ Form::submit('Go!') }}
{{ Form::close() }}
route :-
Route::post('form','HomeController#postView1');
controller function :-
public function postView1() {
$data = Input::all();
return Redirect::route('view2')->with('name', $data['name']);
}
and get data on view2 :-
<p> Hello, {{ $name }} </p>
For more follow HERE
Here's what other answers are missing, straight from Laravel docs:
Since the with method flashes data to the session, you may retrieve the data using the typical Session::get method.
So instead of {{$name}} write {{Session::get('name')}}.

Trouble retrieving data laravel4(oneToMany)

Okay so I just started learning Laravel and its fantastic. I just got stuck trying to retrive all the post from a user. Here is my code
models/User.php
public function posts()
{
$this->hasMany('Post');
}
models/Post.php
public function user()
{
$this->belongsTo('User');
}
controller/UserController.php
public function getUserProfile($username)
{
$user = User::where('username', '=', $username)->first();
$this->layout->content = View::make('user.index', array('user'=>$user));
}
views/user/index.blade.php
<div class="show-post">
<ul>
<li>{{ $user->posts }}</lo>
</ul>
</div>
I also tried:
#foreach($user->posts as $post)
<li>{{$post->post}}</li>
#endforeach
So im having trouble displaying the post for each specific user. Thank you.
So with the help of #WereWolf- The Alpha I was able to solve it and make my code better, If you notice I forgot to return my relationship functions. example:
Notice I hadn't returned it before
models/Post.php
public function users()
{
return $this->belongsTo('users');
}
But also the way I was querying my database was inefficient so Alpha showed me Eager Loading
http://laravel.com/docs/eloquent#eager-loading
example of controller
public function getUserProfile($username)
{
$user = User::with('posts')->where('username', '=', $username)->first();
$this->layout->content = View::make('user.index', array('user'=>$user));
}
and finally the view:
div class="show-post">
#foreach($user->posts as $post)
{{ $post->post }}
#endforeach
</div>
Actually $user->posts returns a collection of Post model so when you try this
{{ $user->posts }}
Laravel tries to echo that collection object which is not possible. You may try foreach loop instead:
#foreach($user->posts as $post)
{{ $post->somepropertyname }}
#endforeach
It's also possible to do something like this:
// Get first post->somepropertyname
{{ $user->posts->first()->somepropertyname }}
// Get last post->somepropertyname
{{ $user->posts->last()->somepropertyname }}
// Get first post->somepropertyname (same as first)
{{ $user->posts->get(0)->somepropertyname }}
// Get second post->somepropertyname from collection
{{ $user->posts->get(1)->somepropertyname }}
// Get the post->somepropertyname by id (post id) 2
{{ $user->posts->find(2)->somepropertyname }}
Also you may use eager loading like this (better):
$user = User::with('posts')->where('username', '=', $username)->first();
You may like this article about Cocllection.
Update: Also use return in model methods, like:
public function posts()
{
return $this->hasMany('Post');
}

Laravel belongsTo returns, put can't save in var

I'm trying to get the username by a item with belongsTo.
I have this in my Item model:
public function user()
{
return $this->belongsTo('User', 'user_id', 'id');
}
And this in my controller:
$user = $item->user->username;
But I get:
Trying to get property of non-object
And when I do:
return $item->user->username;
In my controller it works.
What can be wrong?
Controller/Model: http://pastebin.com/qpAh8eFd
You have following function in your controller:
public function index($type)
{
$items = $this->item->where('type', '=', $type)->get();
foreach($items as $item):
$user = $item->user->username;
endforeach;
return View::make('items.index', ['items' => $items, 'user' => $user]);
}
You don't need to do a foreach look in the controller and whatever you are doing you are doing it wrong, instead make your index function like this:
public function index($type)
{
$items = $this->item->where('type', '=', $type)->get();
return View::make('items.index', ['items' => $items]);
}
Pass only $items to your items/index.blade.php view. You may do a foreach loop in your view if needed and within each iteration you may access the user related to the item using something like this:
#foreach($items as $item)
{{ $item->user->uername }}
#endforeach
You may get Trying to get property of non-object error message because each $item may don't have a related user. So, make sure each $item has a related user. You can also, use following to get items with user:
$items = $this->item->with('user')->where('type', '=', $type)->get();
return View::make('items.index', ['items' => $items]);
In your view you may check if the item has a related user:
#foreach($items as $item)
#if($item->user)
{{ $item->user->uername }}
#endif
#endforeach

Categories