I'm using Laravel 5.3. I'm trying to pass some input from the View to a Controller. Here is what I am doing now in the View:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/update/company') }}">
{{ csrf_field() }}
<input name="_method" type="hidden" value="PUT">
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</form>
And here is the route:
Route::put('/update/company', [
'as' => 'updateCompany',
'uses' => 'Auth\RegisterController#update'
]);
And here is the Controller:
public function update(Request $request){
$compEmail = $this->companyEmail;
if( ! $compEmail)
{
echo "Email Invalid";
}
$user = User::all()->where("email", $compEmail)->first();
if ( ! $user)
{
echo "Invalid Company";
}
$user->name = $request->input('name');
$user->confirmed = 1;
$user->confirmation_code = null;
$user->save();
}
This is giving me the error:
Creating default object from empty value on the line $user->name =
$request->input('name');
Any tips?
If you haven't find any $user:
if ( ! $user) {
echo "Invalid Company";
}
you're not returning the method so here:
$user->name = $request->input('name');
$user->confirmed = 1;
$user->confirmation_code = null;
You try to access fields on null value from $user empty var.
You should add return here:
if ( ! $user) {
echo "Invalid Company";
return;
}
Related
the error is : The GET method is not supported for this route. Supported methods: POST.
although the website works on localhost but on the server doesnt work ?? any solutions
protected function methodNotAllowed(array $others, $method)
{
throw new MethodNotAllowedHttpException(
$others,
sprintf(
'The %s method is not supported for this route. Supported methods: %s.',
$method,
implode(', ', $others)
)
);
}
layout
#extends('install.layout')
#section('content')
<div class="panel panel-default">
<div class="panel-heading text-center">Login Details</div>
<div class="panel-body">
<div class="col-md-12">
#if ($errors->any())
<div class="alert alert-danger alert-dismissible">
×
#foreach ($errors->all() as $error)
<p>{{ $error }}</p>
#endforeach
</div>
#endif
<form action="{{ url('install/store_user') }}" method="post" autocomplete="off">
{{ csrf_field() }}
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" value="{{ old('name') }}" required>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" name="email" value="{{ old('email') }}" required>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" required>
</div>
<button type="submit" class="btn btn-install">Next</button>
</form>
</div>
</div>
</div>
#endsection
########################################################################################################################################################
Web Route
<?php
Route::post('install/store_user', 'Install\InstallController#store_user');
?>
installController
public function store_user(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:191',
'email' => 'required|string|email|max:191|unique:users',
'password' => 'required|string|min:6',
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
$name = $request->name;
$email = $request->email;
$password = Hash::make($request->password);
Installer::createUser($name, $email, $password);
return redirect('install/system_settings');
}
In your form you may give post method but in route you may assign get method. Please cross check it.
most likely it's because of your URL in the form change your form
from
<form action="{{ url('install/store_user') }}" method="post" autocomplete="off">
to
<form action="{{ url('install,store_user') }}" method="post" autocomplete="off">
Using Laravel 5.6 before, my photos uploaded perfectly and now that I've upgraded to 5.7, now they wont and I'm at a loss. The posts will upload, just not the photos. I have checked any rechecked the relationships and routes gut to no avail. Any help will be appreciated.
home.blade.php:
<form method="POST" action="{{ route('makePost') }}">
#csrf
<div class="form-group row">
<label for="body" class="col-md-4 col-form-label text-md-right">{{ __('Body') }}</label>
<div class="col-lg-6">
<textarea id="body" type="text" class="form-control{{ $errors->has('body') ? ' is-invalid' : '' }}" name="body" value="{{ old('body') }}" required autofocus></textarea>
#if ($errors->has('body'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('body') }}</strong>
</span>
#endif
</div>
</div>
<div class="col-md-6">
<input id="image" type="file" class="form-control{{ $errors->has('image') ? ' is-invalid' : '' }}" name="image" value="{{ old('image') }}" autofocus>
#if ($errors->has('image'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('image') }}</strong>
</span>
#endif
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Create Post') }}
</button>
</div>
</div>
</form>
PostsController.php:
public function store(Request $request)
{
$input = $request->all();
$user = Auth::user();
if($file = $request->file('photo_id')) {
$name = time() . $file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
$user->post()->create($input);
return redirect('/home');
}
Photo.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Photo extends Model
{
protected $fillable = [
'file',
];
public function user() {
return $this->belongsTo('App\User');
}
public function photo() {
return $this->belongsTo('App\Photo');
}
}
Post.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'body', 'photo_id', 'user_id',
];
public function post()
{
return $this->belongsTo('App\User');
}
}
Add enctype="multipart/form-data" to <form> tag. This value is required when you are using forms that have a file upload control.
<form method="POST" action="{{ route('makePost') }}" enctype="multipart/form-data">
//
</form>
Source: HTML enctype Attribute
in my laravel 5.2 application I have file attachment form and save information in file table.
files/form.blade.php
<form class="form-vertical" role="form"
enctype="multipart/form-data"
method="post"
action="{{ route('projects.files', ['projects' => $project->id]) }}">
<div class="form-group{{ $errors->has('file_name') ? ' has-error' : '' }}">
<input type="file" name="file_name" class="form-control" id="file_name">
#if ($errors->has('file_name'))
<span class="help-block">{{ $errors->first('file_name') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Add Files</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Filecontroller
public function uploadAttachments(Request $request, $id,$taskId)
{
$this->validate($request, [
'file_name' => 'required|mimes:jpeg,bmp,png,pdf|between:1,7000',
]);
$filename = $request->file('file_name')->getRealPath();
Cloudder::upload($filename, null);
list($width, $height) = getimagesize($filename);
$fileUrl = Cloudder::show(Cloudder::getPublicId(), ["width" => $width, "height" => $height]);
$this->saveUploads($request, $fileUrl, $id,$taskId);
return redirect()->back()->with('info', 'Your Attachment has been uploaded Successfully');
}
private function saveUploads(Request $request, $fileUrl, $id,$taskId)
{
$file = new File;
$file->file_name = $request->file('file_name')->getClientOriginalName();
$file->file_url = $fileUrl;
$file->project_id = $id;
$file->task_id = $taskId;
$file->save();
}
Now I have task form in show.blade.php in projects file in view folder projects/show.blade.php
<form method="post" action="{{ route('projects.tasks.create', $project->id) }}">
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<input type="text" name="name" class="form-control" id="name" placeholder="Add Task" value="{{ old('name') ?: '' }}">
#if ($errors->has('name'))
<span class="help-block">{{ $errors->first('name') }}</span>
#endif
</div>
#endif
<br>
<div style='display:none'; class='somename'>
<div class="form-group">
<textarea name='body'class="form-control">{{ old('body') }}</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Save</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
#include('files.form') //include File.form
This is my file table structure
id file_name file_url project_id
When I open task data enter form I can see file enter form also, but now I need enter taskId to the file table with related to the each tasks. How can I do this?
First of all your 'files/form.blade.php' file is making a POST and not GET, so you need to include a input for taskid like you did for file_name.
maybe:
<input type="hiden" value="ID" /> //ID
<input type="hiden" value="taskID" /> //TASK ID
then in filecontroller > uploadAttachments() you need to get values from post.
$filename = $request->file('file_name')->getRealPath();
$id = $request->input('id');
$taskID = $request->input('taskID');
After that you can call saveUploads(); for save.
..or if you post to route with parameters then include your taskID in form action.
{{ route('projects.files', ['projects' => $project->id, 'taskid' => 'IdHere']) }}
I need add comment box to My Laravel app. this is blade file for comment box in comments folder of view file
comments/form.blade.php
<form class="form-vertical" role="form" method="post" action="{{ route('projects.comments.create', $project->id) }}">
<div class="form-group{{ $errors->has('comments') ? ' has-error' : '' }}">
<textarea name="comments" class="form-control" style="width:80%;" id="comment" rows="5" cols="5"></textarea>
#if ($errors->has('comments'))
<span class="help-block">{{ $errors->first('comments') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Add Comment</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>
and now I am going to include this file in to the show.blade.php file in tasks folder
tasks/show.blade.php
#include('comments.form')
this is My CommentController.php
public function postNewComment(Request $request, $id, Comment $comment)
{
$this->validate($request, [
'comments' => 'required|min:5',
]);
$comment->comments = $request->input('comments');
$comment->project_id = $id;
$comment->user_id = Auth::user()->id;
$comment->save();
return redirect()->back()->with('info', 'Comment posted successfully');
}
and comment model
public function user()
{
return $this->belongsTo(User::class);
}
but got following error
Undefined variable: project (View: C:\Users\Lilan\Desktop\ratakaju\resources\views\comments\form.blade.php)
how to fix this problem?
I'm currently trying to modify the laravel Auth two be able to register two different kinds of users, a seller and a buyer. Both have the same form, except one field, that only the seller has, called companyName.
So what I did is putting a dropdown for registration instead of the normal register button, this is what I got there:
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Registrieren
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li>
Als Käufer
Als Händler
</li>
</ul>
</div>
Then I made a route for this two kinds of registrations, like this:
Route::get('/register/{userType}', 'Auth\RegisterController#showRegistrationForm');
In the controller then I simply overwrote this showRegistrationForm function to pass the userType into my view, just like that:
public function showRegistrationForm($userType)
{
return view('auth.register', ['userType'=> $userType]);
}
And in my view, I got this:
#extends('master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Registrieren
als <?php if ($userType == 'customer') echo "Käufer";if ($userType == 'seller') echo "Verkäufer";?></div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('sex') ? ' has-error' : '' }}">
<label for="sex" class="col-md-4 control-label">Anrede</label>
<div class="col-md-6">
<select class="form-control" id="sex">
<option value="male">Herr</option>
<option value="female">Frau</option>
</select>
</div>
#if ($errors->has('sex'))
<span class="help-block">
<strong>{{ $errors->first('sex') }}</strong>
</span>
#endif
</div>
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="firstName" class="col-md-4 control-label">Vorname</label>
<div class="col-md-6">
<input id="firstName" type="text" class="form-control" name="firstName"
value="{{ old('firstName') }}" required autofocus>
#if ($errors->has('firstName'))
<span class="help-block">
<strong>{{ $errors->first('firstName') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Nachname</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name"
value="{{ old('name') }}" required autofocus>
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
<?php if ($userType == 'seller'){ ?>
<div class="form-group{{ $errors->has('companyName') ? ' has-error' : '' }}">
<label for="companyName" class="col-md-4 control-label">Firmenname</label>
<div class="col-md-6">
<input id="companyName" type="text" class="form-control" name="companyName"
value="{{ old('companyName') }}" required autofocus>
#if ($errors->has('companyName'))
<span class="help-block">
<strong>{{ $errors->first('companyName') }}</strong>
</span>
#endif
</div>
</div>
<?php } ?>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Addresse</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email"
value="{{ old('email') }}" required>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Passwort</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label for="password-confirm" class="col-md-4 control-label">Passwort
wiederholen</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control"
name="password_confirmation" required>
#if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
#endif
</div>
</div>
<div style="display:none;" class="form-group{{ $errors->has('role') ? ' has-error' : '' }}">
<label for="role" class="col-md-4 control-label">Deine Rolle:</label>
<div class="col-md-6">
<input name="role" type="radio"
<?php if ($userType == 'customer') echo "checked";?> value="Käufer"> Käufer<br/>
<input name="role" type="radio"
<?php if ($userType == 'seller') echo "checked";?> value="Verkäufer"> Verkäufer
#if ($errors->has('role'))
<span class="help-block">
<strong>{{ $errors->first('role') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Registrieren
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
So mostly basic, just few more fields then with the auth without modification and the companyName only showing up when accessed over the route /register/seller.
My RegisterController is of course also a bit modified, or especially the create function, it looks like this now:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'firstName' => $data['firstName'],
'sex' => $data['sex'],
'email' => $data['email'],
'username' => $data['username'],
'password' => bcrypt($data['password']),
'role' => $data['role'],
'templateURL' => ""
]);
if($data['role'] == 'Verkäufer'){
Complaint::create([
'user_id' => $user->id,
'complaintCount' => 0
]);
}
switch($data['role']){
case 'Käufer':
$user->attachRole(2);
break;
case 'Verkäufer':
$user->attachRole(3);
$user->companyName = $data['companyName'];
$user->save();
break;
default:
$user->attachRole(2);
break;
}
return $user;
}
And now comes the problem: As you can see, in my "invidual" views, which is basically just one anyway, I still post to the url /register, which I thought should work, but it doesn't.... Any ideas why this is not working? I also tried to add individual routes, so something like that:
Route::post('/register/seller', 'Auth\RegisterController#create');
Route::post('/register/buyer', 'Auth\RegisterController#create');
but thats not working as well. In both cases, I just get the same window back, as if there was an error (so my data still entered (expect the password), but nothing is registered or entered in the db, but as well there are no errors showing up, neither in my view, nor in the console.
What's interesting as well is the network tab, it seems like it definetely sends the request to /register as it shows up there with status code 302, but as well there's the route /register/customer again, and I'm wondering why...What's also interesting is that I think that somehow it kinda works, as if I enter a password with less then 6 characters or 2 different passwords, I get an error, so somehow the form seems to be posted, but nothing is entered into the db....
Any ideas why this happens like this and whats the problem?
First of all, I'd like to suggest you a Polymorphic approach to saving the users. Right now, you have only 2 user types, what if you get a third user type (say retailer or wholesaler or blah blah)... and for each of them, you will require different fields which may or may not fit in for all user types...
So, go with this
class User
{
public function profile()
{
return $this->morphTo();
}
}
class Seller
{
public function user()
{
return $this->morphOne('App\User', 'profile');
}
}
class Buyer
{
public function user()
{
return $this->morphOne('App\User', 'profile');
}
}
Now, In your routes, add these
Route::get('login', 'LoginController#show')->name('login.show');
Route::post('login', 'LoginController#login')->name('login.post');
Route::get('register', 'RegisterController#show')->name('register.show');
Route::post('register', 'RegisterController#register')->name('register.post');
Route::get('logout', 'LoginController#logout')->name('login.logout');
Now, in your form add a dropdown/radio button for User Type selection (you can also make seprate and run different routes and make these fields hidden);
Say,
<select name="type">
<option value="1">Buyer</option>
<option value="2">Seller</option>
<select>
Your RegisterController#register can be as follows:
use App\Buyer;
use App\Seller;
use Validator;
class RegisterController extends Controller
{
public function show()
{
return view('auth.register');
}
public function register()
{
$inputs = request()->all();
$validator = $this->validator($inputs);
if($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
$userInputs = array_only($inputs, ['name', 'email', 'password']);
$userInputs['password'] = Hash::make($userInputs['password']);
switch($inputs['type'])
{
case 1:
$sellerInputs = array_only($inputs, ['company_name']);
$seller = Seller::create();
$user = $seller->user()->create($userInputs);
case 2:
$buyer = Buyer::create();
$user = $buyer->user()->create($userInputs);
default:
$user = null;
break;
}
if(!$user) {
return redirect()->back(); //Show flash messsage etc... and redirect back to show an error
}
auth()->attempt(array_only($inputs, ['email', 'password']));
return redirect(route('some.route'));
}
protected validator($inputs)
{
$rules = [
'name' => 'required|min:1|max:50',
'email' => 'required|email|min:1|max:100',
'password' => 'required|min:6|max:25',
// Other rules
];
$messages = [
// Any special messages if required...
];
return Validator::make($inputs, $rules, $messages);
}
}
Use same kind of coding structure in LoginController... I am simply going to write the login behind logging in below
public function login()
{
$inputs = request()->all();
//Validator etc...
if(auth()->attempt(array_only($inputs, ['email', 'password']))) {
return redirect(route('some.route'));
} else {
return redirect()->back(); // Again... Show some error flash message
}
}
Note :- I have not tested the code but I am 99% sure this should work... I wrote all this down myself... Took a damn half an hour almost!
Hope everything is answered and you understood. Let me know in the comments below if you have any other query :)