I'm trying to populate a form with my logged in user so I can edit some profile information.
I currently have this code (only trying to display the email first, will populate the form later once this works):
I made an editprofile.blade.php template with:
#extends('masterpage')
#section('content')
<h1> Edit Profile </h1>
{{ Form::model($user) }}
<div class="form-group">
{{ Form::label('email', 'E-mail') }}
{{ Form::Text('email', null, ['class' => 'form-control']) }}
</div>
{{ Form::close() }}
#stop
In myProfileController.php I have:
<?php namespace App\Http\Controllers;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Html\FormFacade;
use Illuminate\Html\HtmlFacade;
class ProfileController extends Controller {
public function show() {
$user = Auth::user();
return view('pages.editprofile')->withUser($user);
}
}
In my route I added:
Route::get('/profile/edit', 'ProfileController#show');
But when I try to load the page I get this:
I have two questions:
1: Why isn't there a form showing up with the data? As you can see the email is being loaded into the form but it displays it as text.
2. Once the data is loaded into a succesful form and the user has edited some of the data, how would I make update function to save the edited data?
I think that you are just outputting the raw data because probably you are using double brackets {{ }}.
As i remember i had a same thing when i migrated a site from laravel 4 to laravel 5. They changed the way blade output and echo the data. try to change double brackets {{ }} to this {{!! !!}} or this {{{ }}} i don't remember exactly which one it is.
Edit: about saving you can use controllers so when user post the data it goes to something like the same url just with post method, so it calls the update function inside controller.
For some functions where user should have full control over his profile like edit, update, delete, create i would use the examples from this http://laravel.com/docs/5.0/controllers#restful-resource-controllers take a look at restful controller
Related
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 have a Form that contains a Dropdown and a Submit button. Like so:
View path: webmasters/filters.blade.php
{{ Form::open() }}
{{ Form::select('filt', $Dropdown, 2) }}
{{ Form::Submit('Filter') }}
{{ Form::close() }}
{{ Form::close() }}
And a controller that populates the Dropdown with values queried from a DB. Like so:
Controller name: WebController.php
class WebController extends BaseController {
public function getFilters() {
$filters = Dropdown::getFilters();
return View::make('webmasters.filter',['Dropdown'=>$filters]);
}
Here is my route:
Route::resource('topPage', 'WebController#getFilters');
getFilters is a model method that queries the DB for the values that come into the dropdown.
I would like to call a controller method upon submitting the form so that the said method queries another DB and returns a table (on the same page as the dropdown and submit button) based on the selected value of the dropdown.
I'm probably going about this the wrong way, so i'd really appreciate if anybody gave me a headsup. I would still prefer not to use Ajax on this because i dont know my way around it.
Thanks for your help.
Since the default form created by Form::open() is using POST you can just add another method in your controller and are all set
public function postFilters() {
$filt = Input::get('filt'); // getting the value of the select
}
I'm trying to create a page dynamically for each courses that I'm adding in a database.
I have a CoursesController who is taking care of adding, displaying the courses.
So, when I click on a course, it should dynamically create a page for that course and show details in that course page.
In the route.php page, I have
Route::get('courses/{code}', [ 'as'=>'course-show', 'uses'=>'CoursesController#getShow']);
and in the
CoursesController.php
public function getShow($code){
return $code;
}
And in the index.blade.php for CoursesController,
<h4>{{ $course->name }}</h4>
Now, It create the link with a unique code (saved in database) and upon clicking there, it takes me to the course page with an error:
BadMethodCallException
Method [show] does not exist.
What might be the problem? Can anyone help me?
The getShow() function in your controller should be show().
Also URL::action() goes to a controller action.
You probably want URL::route()
<h4> {{ $course->name }} </h4>
or you could do this
<h4> {{ $course->name }} </h4>
I created a very simple form so that I could use a submit button rather than a link to open up an edit users page. Using a link works perfectly, but the form button fails and yields a MethodNotAllowedHttpException even though the method ("edit") is perfectly defined in the UsersController resource and otherwise works fine.
Route:
Route::resource('users','UsersController');
UsersController:
public function edit($id)
{
$user = $this->user->find($id);
return View::make('users.edit')->with('user',$user);
}
show.blade.php:
<!-- This works fine: -->
{{ link_to_route('users.edit', ("Edit: " .$user->first_name." ".$user->last_name), $user->id) }}
<!-- This doesn't work, and yields the Method Not Allowed exception: -->
{{ Form::open(array('route' => array('users.edit',$user->id))) }}
{{ Form::submit('Edit User', array('class'=>'button')) }}
{{ Form::close() }}
Thanks.
When you do Form::open(), it defaults to using the post request method. But when you create a Route::resource(), the edit method takes a get request.
To make it work through the form, you'll need to open it with an additional parameter, like this:
{{ Form::open(array('route' => array('users.edit',$user->id),
'method' => 'get')) }}
You need to point to the update route, not edit.
{{ Form::open(array('route' => array('users.update', $user->id))) }}
The edit route is for displaying the view, while the update is for the put/patch request.
For more information about using the RESTful routes, I'd recommend checking out http://laravel.com/docs/controllers#resource-controllers
I'm having some strange behavior with my forms in Laravel 4. I have a "settings" page with two forms, each (are supposed to) POST to a controller method, update the database and return back to the settings page. However, there seems to be an issue, either with the way my forms are working or my routes.
Here's how it is, simplified:
Settings page: (site.com/settings)
<div id="form-one" class="form-area">
{{ Form::open(array('action' => 'SettingController#editOption')) }}
{{ Form::text('optionvalue', 'Default')) }}
{{ Form::submit('Save Changes') }}
{{ Form::close() }}
</div>
<div id="form-two" class="form-area">
{{ Form::open(array('action' => 'SettingController#editPage')) }}
{{ Form::text('pagevalue', 'Default')) }}
{{ Form::submit('Save Changes') }}
{{ Form::close() }}
</div>
So basically, two seperate forms on the same page that post to two seperate methods in the same Controller - when the method is successful, it redirects them back to "settings". I won't post the methods, since tested them and they work, I believe the problem is in the routes file:
routes.php
// Checks if a session is active
Route::group(array('before' => 'require_login'), function()
{
Route::group(array('prefix' => 'settings'), function()
{
Route::get('/', 'SettingController#index');
Route::post('/', 'SettingController#editOption');
Route::post('/', 'SettingController#editPage');
});
});
Now I'm pretty sure it doesn't like the two POST routes being like that, however I cannot think of another way to do it, since the forms are on the same page. I get the error:
Unknown action [SettingController#editOption].
Since the option form comes first I guess. If I take the open form blade code out (for both), it loads the page - but obviously the form doesn't do anything.
Any help would be nice! Thanks in advance.
You can't add two same routes for different actions, because of they will be passed to first matched route and in your case to SettingController#editOption. Change your routes to :
Route::post('/option', 'SettingController#editOption');
Route::post('/page', 'SettingController#editPage');
Than in both actions you can redirect to '/': return Redirect::back(), and if error was occured:
if ($validation->fails())
{
return Redirect::to('/settings')->with_errors($validation);
}
My alternative solution for this is to create an hidden html input in each form and make the controller identify what for is submitted based in this field. So, yu can use just one route for both.