I'm having a bit of an issue trying to access all of a objects properties.
In my UsersController:
public function edit($id)
{
return View::make('users.edit')->with('user', User::find($id));
}
In users/edit view I can access only some of the objects properties such as {{ $user->username }} and {{ $user->email }} however, I cannot access {{ $user->id }} or {{ $user->role_id }} ... the app complains about trying to get the property of a non-object. On the other hand if I use {{ dd($user->id) }} it returns the correct value as expected. Being new to Laravel and Eloquent I'm at a loss for why this might be.
Any help is appreciated.
Try change variable name user to something else. Some frameworks has $user variable reserved in they templating system.
It turns that I inadvertently overwrote the $user object which resulted in having access to only some of the object attributes. The non-object error made sense considering the second $user was no long the user object returned from the controller. It was a silly mistake and I feel foolish for overlooking it in the first place. Thank you everyone for the input!
Related
I am programing a blog using Laravel 5 and bootstrap.
The #if control structure seem to not be working written this way in my blade.php:
#section('title')
#if($post)
<p> {{ $post->title}} </p>
#if(!Auth::guest() && ($post->name == Auth::user()->id || Auth::user()->is_admin()))
<button type="button" class="btn btn-default">Editer</button>
#endif
#endif
#endsection
Here is my function in PostController that calls the blade :
public function index()
{
$post = Post::get();
return view('posts.index', compact('post'));
}
I encounter this error :
Undefined property: Illuminate\Database\Eloquent\Collection::$title
I'm new to PHP, I can't seem to see where is the problem. If you have any clue, please help.
You need to attach a "post" variable when making a call to this view. In one of your controller methods.
In your edited question, "title" is missing. Pass a "title" variable along with post. Basically any variable you want to use in views that are not defined in view (except global variables), need to be passed to it explicitly from the controller.
Edit for collections error.
Your post variable is actually an array, loop through it and access title or any property of a post for each member of the post array
In my Laravel application, when I am trying to link the username of the person who has posted in the website with his profile page, with the code:
<div class="media-body">#{{ post.user.name }}
It is giving the error:
Sorry, the page you are looking for could not be found.
1/1 NotFoundHttpException in RouteCollection.php line 161:
But, when I am trying to print #{{ post.user.profileUrl }} it is giving the right address, also in the json response, it is giving the right address, and going to the address is also reaching the specific location.
So, I don’t think it is some problem with post.user.profileUrl, as it seems to work fine, it seems to be some problem with using it with href, the address of the error in Google Chrome is:
http://localhost:8000/%7B%7B%20post.user.profileUrl%20%7D%7D
and the address should have been
http://localhost:8000/users/2 where 2 refers to the id of the user, which I am passing to the user through Vue.js
The problem is #{{ post.user.profileUrl }} is not parsing. %7B%7B%20post.user.profileUrl%20%7D%7D is ASCII representation of {{ post.user.profileUrl }}
Check if the code is in .blade.php file. If it is, you should look into JS template engine if you're using any.
https://laravel.com/docs/5.3/blade#blade-and-javascript-frameworks
The # symbol will be removed by Blade; however, {{ name }} expression will remain untouched by the Blade engine, allowing it to instead be rendered by your JavaScript framework
I think you have a $post model in your view. When you setup relationships between the model Post and User you can link to them through the following:
#{{ $post->user->name }}
This will turn the users profileUrl into a valid link. But therefor you had to setup relationships in the Models.
Post model:
public function user() {
return $this->hasMany('App\Users', 'id', 'user_id');
//first parameter is the Model class
//send is the id of the user table
//thirth is the user_id in the post table
}
And in the User model:
public function posts() {
return $this->hasMany('App\Posts', 'user_id', 'id');
}
Hope this works!
Try this... this should definitely work for you
#verbatim
<div class="media-body">
<a href="{{ post.user.profileUrl }}">
{{ post.user.name }}
</a>
</div>
#endverbatim
I got my problem, the issue is with the javascript library, I am using, known as vue.js, it used to allow usage inside quotes, but in vue 2, they don't, so there is a turnaround for this,
<a :href="post.user.profileUrl">#{{ post.user.name }}</a>
and this works fine.
Thanks to all the people who contributed by answering...
I would like to store my application settings in database.
In order to get a variable in template, I'm currently using
{{ Config::get('file.variable) }}
and settings are stored in config/file
I would like to create controller SettingsController with public static get and set methods and get variables in template in this way:
{{ Settings::get('var_name') }}
instead of
{{ SettingsController::get('var_name') }}
But I'm getting error: Class 'Settings' not found.
I've tried to set routes:
Route::controller('Settings', 'SettingsController'); and
Route::resource('Settings', 'SettingsController');
But none of the methods works.
Any ideas how to solve this problem?
This should be done using facades, which is already answered here:
How to create custom Facade in Laravel 4
On a dashboard page, I've created a select list in a form that lists the names of components; the value that's passed from the select list is obviously the component id. On pressing submit, the user is routed to a page that displays the data about that component. Should be dirt simple...
Controller:
public function showDashboard()
{
$components = Component::lists('name','id'); ...
return View::make('dashboard', array('components'=>$components, ...))
}
dashboard.blade.php:
{{ Form::open(array('route' => array('components.show', $components->id), 'method'=>'get')) }}
{{ Form::Label('id','Component:') }}
{{ Form::select('id', $components) }}
{{ Form::submit('Show Component', array('class'=>'button')) }}
{{ Form::close() }}
I've tried various ways of doing this, and get a different error every time. The above code doesn't even let me display the dashboard page -- I get a "Trying to get property of non-object" error. Clearly, it's not liking $components because that was passed as a list array and not an object. As I said, I'm sure this is dirt simple, I just can't figure out the proper syntax, and Laravel docs aren't giving me the answer. Thanks!
The problem isn't the dropdown, or the lists method, but rather in your form opening. Here, you have $components->id as an argument to the route, but $components is an array and you can't access an id property on it.
Finally figured this out. I had posted a similar question here subsequent to this one, and rather than repeat the answer, it is here:
How to pass id value from select list to controller when controller expects an object? laravel-4
The very short version: change Route::get to Route::post. Details with code in the link above. Problem solved!
This is a strange one. I'm trying to implement a 1:1 relationship between Twig and some ViewModel objects, such that Twig is aware of its context and assumes variables are methods on the object.
For example, I have a Twig template and a ViewModel_Product. I could do this...
$template->render(array('product', $product));
...and in the template...
<p>{{ product.name }}</p>
However, because the only thing that will ever be passed to the template is the model, it seems pointless to have users prefix each variable. Better usage would be:
$template->render(array('viewModel', $product));
...and...
<p>{{ name }}</p>
How can I achieve this?
I don't believe this would be possible because twig keeps track of other global variables in each template so how would it know if the variable {{ name }} is part of your view or some other global variable? And as it was mentioned above, having the variable prefix helps to namespace your view which makes for easier reading.
Don't be a lazy coder.