Patch update Authenticated User in Laravel - php

I'm trying to edit/update profile information into my Users table.
My idea is for the authenticated user to be able to edit his own profile in the Users table.
During the registration, you are only required to fill out a couple specific items (username, name, lastname, email, password) but I've also added a couple extra columns to the User table (city, country, phone, twitter, facebook).
I have a profile user page (route= '/profile') where all the information is shown. Of course all columns that aren't required during the registration are not filled in:
I also have an edit page where the columns that need info added are editable:
Here is the code for this editprofile.blade.php (where I try to send out a PATCH method):
...
{!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!}
<div class="form-group form-horizontal">
<div class="form-group">
{!! Form::label('username', 'Username:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->username}}<label>
</div>
</div>
<div class="form-group">
{!! Form::label('email', 'E-mail:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->email}}<label>
</div>
</div>
<div class="form-group">
{!! Form::label('name', 'Name:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->name}} {{ $user->lastname}}<p>
</div>
</div>
<br />
<div class="form-group">
{!! Form::label('city', 'City:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('city', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('country', 'Country:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('country', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('phone', 'Phone:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('phone', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('twitter', 'Twitter link:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('twitter', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('facebook', 'Facebook link:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('facebook', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
{!! Form::submit('Save Profile', ['class' => 'btn btn-primary']) !!}
</div>
</div>
</div>
</div>
{!! Form::close() !!}
...
I have this is my Http/routes.php:
# Profile
Route::get('/profile', 'PagesController#profile');
Route::get('/profile/edit', 'ProfileController#edit');
Route::bind('user', function($id) {
$user = App\User::find($id)->first();
});
Route::patch('user/{user}', 'ProfileController#update');
I have an Http/Requests/UpdateUserRequest.php to validate the request:
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;
class UpdateUserRequest extends Request {
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'city' => 'max:30',
'country' => 'max:30',
'phone' => 'max:30',
'twitter' => 'max:30',
'facebook' => 'max:30'
];
}
}
And my Http/Controllers/ProfileController.php with the update fuction:
<?php namespace App\Http\Controllers;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ProfileController extends Controller {
public function edit()
{
$user = Auth::user();
return view('pages.editprofile')->withUser($user);
}
public function update(UpdateUserRequest $request, User $user)
{
$user->fill($request->all());
$user->save();
return redirect()->view('pages.editprofile')->withUser($user);
}
}
At the moment it seems I can't even open my 'editprofile.blade.php' page anymore, unless I remove the 'route' & 'methode' from my form.
I keep getting this error:
Could anyone guide me on how I should exactly trigger the PATCH methode?

change your form opening tag to
{!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!}
you forgot '->id'
UPDATE
Change you Route
from
Route::patch('user/{user}', 'ProfileController#update');
to
Route::patch('user/{user}', 'as' => 'profile.patch', 'ProfileController#update');
and your form opening tag to
{!! Form::model($user, ['route' => array('profile.patch', $user->id), 'method' => 'PATCH']) !!}

You need to change:
{!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!}
into:
{!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!}
At the moment you create URL in your form with User object converted to Json - that's why it doesn't work.

Related

Trouble with Route in laravel collective and laravel 7.6

I'm new to laravel. When I try to generate a form, the problem occurred.
My route
Route::get('post/add', "Post1Controller#add");
Route::post('post/store', "Post1Controller#store");
My Post1Controller
function add()
{
return view('post/create');
}
function store(Request $request)
{
echo "Had file";}
My form
{!! Form::open(['url'=> 'post/store','method'=>'POST', 'files'=> true]) !!}
<div class="form-group">
{!! Form::file('image', ['class' => 'form-control'])!!}
</div>
<div class="form-group">
{!! Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Title in here'])!!}
</div>
<div class="form-group">
{!!form::textarea('content','',['class'=> 'form-control', 'placeholder'=> 'Text in here'])!!}
</div>
<div class="form-group">
{!! Form::submit('Add', ['name'=>'sm-add', 'class' => 'btn btn-danger'])!!}
</div>
{!! Form::close() !!}`enter code here`
My issue: The GET method is not supported for this route. Supported methods: POST. Thank you for your support.

Laravel 5 storing empty row in database?

I'm currently working on a simple blog system for my website, I'm using Laravel 5.5 for this, but I'm having a problem. Whenever I create a new article using the form I created, it is storing a empty row in my database instead of the data from the form...
This is my function to store a article:
public function newsStore()
{
$input = Request::all();
Article::create($input);
return $input;
}
According to the tutorial that I'm following should this do the trick.. This is the form that I'm currently using:
{!! Form::open(['url' => 'news']) !!}
<!-- Article Title -->
<div class="form-group row">
{!! Form::label('title', 'Title:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Description -->
<div class="form-group row">
{!! Form::label('description', 'Description:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('description', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Excerpt -->
<div class="form-group row">
{!! Form::label('excerpt', 'Excerpt:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('excerpt', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Image -->
<div class="form-group row">
{!! Form::label('image', 'Image:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::file('image') !!}
</div>
</div>
<!-- Article Message -->
<div class="form-group row">
{!! Form::label('message', 'Message:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::textarea('message', null, ['class' => 'form-control', 'rows' => '7']) !!}
</div>
</div>
<!-- Article Submit -->
<div class="form-group">
{!! Form::button('Add Article', ['class' => 'btn btn-primary waves-effect waves-light float-right', 'type' => 'submit']); !!}
</div>
{!! Form::close() !!}
This is the route I'm using:
Route::post('news', 'DashboardController#newsStore'); // The articles store route
This is my model:
class Article extends Model
{
protected $fillable = [
'title',
'description',
'excerpt',
'image',
'body',
'published_at'
];
}
I have done this once before and have tried to redo it all but can't seem to figure out what is going wrong.
Thanks in advance!
To make create() work make sure you're using columns with same names. For example, if your column name is title, you need to use title instead of news-title in the form too:
{!! Form::text('title', null, ['class' => 'form-control']) !!}
As you use this Article::create(); it means all your input names should be same as table column name.
Ex:
<input name="title" ... same be same to column call news-title in respective table
If not same
Use Query Builder
DB::table('table_name')->insert(
[
'name' => $input->news-title
.....
]
);
Read More about Hyphens in column names in MySQL DB

Form model data not running correctly on #yield Laravel 5

all I want is to define the form model on template and then put a yield as the content of form data. But it cannot assign the model data correctly into each field that has been defined.
This is my code:
template.detail.blade.php
#extends('admin.template.lte.layout.basic')
#section('content-page')
{!! Form::model($model, ['url' => $formAction]) !!}
#yield('data-form')
{!! Form::close() !!}
#if ($errors->any())
#stop
partial.edit.blade.php
#extends('template.detail')
#section('data-form')
<div class="form-group">
{!! Form::label('Dal_Name', 'Alternative Name', ['class' => 'required']) !!}
{!! Form::text('Dal_Name', null, ['required', 'class' => 'form-control', 'placeholder' => 'Enter Alternative Name']) !!}
</div>
<div class="form-group">
{!! Form::label('Dal_DssID', 'DSS Period', ['class' => 'required']) !!}
{!! Form::select('Dal_DssID', $dssOptions, null, ['class' => 'form-control']) !!}
</div>
<div class="checkbox">
<label for="Dal_Active">
{!! Form::hidden('Dal_Active', 'N') !!}
{!! Form::checkbox('Dal_Active', 'Y') !!}
Active
</label>
</div>
#stop
My Controller part:
/**
* Show the form for editing the specified resource.
*
* #param int $id
*
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$this->data['model'] = DssAlternative::find($id);
$this->data['formAction'] = \Request::current();
$this->data['dssOptions'] = Dss::lists('Dss_Name', 'Dss_ID');
return view('partial.edit', $this->data);
}
But the model data does not propagate to form correctly.
Sorry for my bad english.
It won't work since you are passing the $model object to partial/edit.blade.php file, and hoping to use it within template/detail.blade.php
exactly at this line
{!! Form::model($model, ['url' => $formAction]) !!}
Solution:
in template/detail.blade.php Take the form model line inside like this:
#extends('admin.template.lte.layout.basic')
#section('content-page')
#yield('data-form')
#if ($errors->any())
#stop
So partial/edit.blade.php would be like below:
#extends('template.detail')
#section('data-form')
{!! Form::model($model, ['url' => $formAction]) !!}
<div class="form-group">
{!! Form::label('Dal_Name', 'Alternative Name', ['class' => 'required']) !!}
{!! Form::text('Dal_Name', null, ['required', 'class' => 'form-control', 'placeholder' => 'Enter Alternative Name']) !!}
</div>
<div class="form-group">
{!! Form::label('Dal_DssID', 'DSS Period', ['class' => 'required']) !!}
{!! Form::select('Dal_DssID', $dssOptions, null, ['class' => 'form-control']) !!}
</div>
<div class="checkbox">
<label for="Dal_Active">
{!! Form::hidden('Dal_Active', 'N') !!}
{!! Form::checkbox('Dal_Active', 'Y') !!}
Active
</label>
</div>
{!! Form::close() !!}
#stop

Laravel 5.1 formfacade can't get value from select

I have created a form to change the user name and its role in the website.
#extends('layout/layoutAdmin')
#section('content')
<div>
<h1>{{ $user -> name }}<h1>
<p>{{ $user -> email }}<p>
</div>
{!! Form::model($user, ['url' => 'admin/menu/user_profiles/' . $user->id, 'method' => 'PATCH']) !!}
<div class="row">
<div class ="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', $user->name,['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('role', 'Role:') !!}
{!! Form::select('role', array('admin' => 'admin', 'super_admin' => 'super admin',
'super_researcher' => 'super researcher', 'researcher' => 'researcher',
'consultant' => 'consultant', 'user' => 'user'), $user->role)
!!}
</div>
<div class="form-group col-xs-8 col-md-7">
{!! Form::submit('Update', ['class' => 'btn btn-primary']) !!}
</div>
</div>
{!! Form::close() !!}
#stop
Everything is correct with the controller and I can change the name just fine, but I cannot save a new value for the role. it always stays the same.
Could anyone tell me how to save the role value?
EDIT!!!!
I did not have role field as fillable!

mismatch token laravel on add form

I have this form to create a new article. User inputs everything correctly but i get a return of TokenMismatchException in VerifyCsrfToken.php line 53:. I saw another post saying that because i used the open form Tag it will already add this for me and to take it out. How do i take this out? or is there some other problem that im overlooking?
here is my controller
public function store( AddArticleController $request)
{
$request= $request->all();
$request['user_id']=Auth::id();
Article::create($request->all());
return redirect('/user');
}
and here is the html
<form class="form-horizontal" role="form" method="POST" action="{{ url('/article/add') }}">
{!! Form::open(
array(
'class' => 'form',
'novalidate' => 'novalidate',
'files' => true)) !!}
<div class = "form-group">
{!! Form::label('title','Title:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::text('title',null, ['class' => 'form-control']), !!}
</div>
</div>
<div class = "form-group">
{!! Form::label('title','Image:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::file('image') !!}
</div>
</div>
<div class="form-group">
{!! Form::label('title','Type:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-lg-1">
{!! Form::select('type', array('select' => 'select','fashion' => 'Fashion', 'music' => 'Music', 'dance' => 'Dance', 'event' => 'Event'))!!}
</div>
</div>
<div class = "form-group">
{!! Form::label('body','Comment:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::textarea('body',null, ['class' => 'form-control']) !!}
</div>
</div>
<div class = "form-group">
{!! Form::label('body',' ', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form:: submit('Sumbit', ['class' => 'btn btn-primary form-control']) !!}
</div>
</div>
{!! Form::close() !!}
</form>
First, which version of Laravel do you use?
The Laravel's HTML Helper has been removed in the version 5 of Laravel and is now not updated. If you still want to use this helper I recommend you to use the version updated by the community https://github.com/LaravelCollective/html.
For your problem, if you don't use the helper, you need to add the token manualy by adding {!! csrf_token() !!} in your form. You can find more information in the documentation: http://laravel.com/docs/5.1/routing#csrf-protection
You should get rid of either the <form> tag or from {{ Form::open() }} from your code.
The Laravel's form generator (which is depricated since Laravel 5) allows you to write that syntax {{ Form::open() }} which is rendered to plain html tag <form>.
so in your case the right way to go would be either
<form class="form form-horizontal" novalidate="novalidate" enctype="multipart/form-data" role="form" method="POST" action="{{ url('/article/add') }}">
{!! csrf_token() !!} // note that in this case we need to put csrf token manuall
......
</form>
OR
{!! Form::open(['action' => url('/article/add'), 'novalidate' => 'novalidate' 'files' => true, 'class' => 'form form-horizontal']) !!}
... //your fields and you don't have to explicitly include csrf token. Form::open() will do that for you
{!! Form::close() !!}

Categories