I have a problem with send email with Laravel, it's my method:
public function store(CreateUserRequest $request){
$name = Input::get('name');
$imie = Input::get('imie');
$nazwisko = Input::get('nazwisko');
$email = array('email' => Input::get('email'));
$password = Input::get('password');
Mail::send(['name' => $name], function ($message) use ($name, $imie, $nazwisko, $email, $password) {
$message->from('us#example.com', 'System Magazyn');
$message->attach('Your temporary password: '.['password' => $password]);
$message->to(['email'=>$email])->subject('Rejestracja Magazyn');
});
User::create($request->all());
Session::flash('addUserOK', 'Użytkownik dodany poprawnie.');
return redirect('user');
}
This method saved to database new user. I want send email to new user with information on the correct registration and information with temporary password.
I did as it is written in the documentation https://laravel.com/docs/5.2/mail#sending-mail
and as it is in this topic: Laravel 4 from contact form to admin email, but still Laravel returned error:
FatalThrowableError in Mailer.php line 149: Type error: Argument 2
passed to Illuminate\Mail\Mailer::send() must be of the type array,
object given, called in
C:\xampp\htdocs\kurwa_magazyn\magazyn_michal\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php
on line 219
EDIT:
This is form to register new user:
{!! Form::open(['route' => 'user.store', 'method' => 'post', 'class' => 'form-horizontal']) !!}
{!! Form::text('name', null, ['class' => 'form-control', 'placeholder' => 'Nazwa użytkownika...']) !!}<br />
{!! Form::text('imie', null, ['class' => 'form-control', 'placeholder' => 'Imię...']) !!}<br />
{!! Form::text('nazwisko', null, ['class' => 'form-control', 'placeholder' => 'Nazwwisko...']) !!}<br />
{!! Form::email('email', null, ['class' => 'form-control', 'placeholder' => 'Adres e-mail...']) !!}<br />
{!! Form::text('password', uniqid(), array('class' => 'form-control', 'placeholder' => 'Podaj hasło...')) !!}<br />
{!! Form::select('permissions', array('0' => 'Pracownik fizyczny', '2' => 'Magazynier', '3' => 'Demo użytkownik', '1' => 'Administrator'), NULL, ['class' => 'form-control']) !!}<br />
{!! Form::hidden('remember_token', bcrypt(uniqid(rand(0,9)))) !!}
{!! Form::submit('Dodaj', ['class' => 'btn btn-default']); !!}
{!! Form::close() !!}
I know this is an old question, but, for anyone with the same problem, the second argument of Mail::send must be an array. This array could contain any data that will be read in the view.
The code would be like this:
$data = ['foo' => 'baz'];
Mail::send(['name' => $name], $data, function ($message) use ($name, $imie, $nazwisko, $email, $password) {
$message->from('us#example.com', 'System Magazyn');
$message->attach('Your temporary password: '.['password' => $password]);
$message->to(['email'=>$email])->subject('Rejestracja Magazyn');
});
Then, in the view, the variable $foo could be printed:
<p>Variable foo: {{$foo}}</p>
I think the issue you have is in a weird combination of this:
$email = array('email' => Input::get('email'));
and this
$message->to(['email'=>$email])->subject('Rejestracja Magazyn');
According to API docs method to accepts either combination of email/name or or an array
Try to change your code to:
$message->to($email)->subject('Rejestracja Magazyn');
Since you already defined that array earlier here:
$email = array('email' => Input::get('email'));
I think you need to change the $email variable in this case because as per your code $email is key->value type array passed in the Email sending function and for Email sending we only need to send an array with only values containing an email address.
Change $email = array('email' => Input::get('email')); to $email = array(Input::get('email'));
Related
I want to save some form data, and I'm Getting error.
The Error
Action App\Http\Controllers\Admin\ConcursoController#store not defined. (0)
My Form
{!! Form::open(['action'=>'Admin\ConcursoController#store', 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('company','Entidade')}}
{{Form::text('company','',['class' => 'form-control', 'placeholder' => 'Nome da entidade aquí..'])}}
</div>
{{Form::submit('submeter', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
My Route
$this->group(['middleware' => ['auth:admin'], 'namespace' => 'Admin', 'prefix' => 'admin'], function(){
$this->get('/', 'AdminController#index')->name('admin.home');
$this->resource('concursos', 'ConcursoController');
});
Controller index Method
public function index()
{
$concursos = Concurso::all();
$title = 'Concursos';
return view('admin.concursos.index',compact('title'))->with('concursos',$concursos);
}
Controller Create method
public function create()
{
return view('admin.concursos.create');
}
Controller Store Method
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required',
]);
//Criar concurso
$concurso = new Concurso;
$concurso->title = $request->input('title');
$concurso->body = $request->input('body');
$concurso->save();
return redirect('/admin/concursos')->with('Success', 'Concurso Adicionado');
}
Laravel version 5.7.14
Probably check this file: App\Http\Controllers\Admin\ConcursoController and see if you have a function/method called 'store'. The error is pretty straightforward that script can't find that function.
replace your form
['route' => ['concursos.store']
like
{!! Form::model($transactions, ['route' => ['transaction.store'], 'method' => 'POST','class'=>"form-horizontal"]) !!}
I am trying to edit a JSON formatted category with PUT method, so I am using guzzleHttp library to parse json requests and responses with laravel 5.5.
My POST, GET methods are working fine when I am trying to grab or insert data into my server, but I am getting error on PUT & DELETE method.
There are two types of errors I am getting :
localhost is currently unable to handle this request. HTTP ERROR 500
Out Of Memory - Fatal error: Out of memory (allocated 1472200704) (tried to allocate 176128 bytes)
Console Error :
These errors occurs not together just one after another if I request twice in row.
I have trying to change allocated memory, but it did not work !
Here are my procedures to handle a request :
My Controller :
public function update(Request $request, $id)
{
// get the inputs
$inputs = [
"cat_id" => $id,
"cat_name" => $request->input('cat_name'),
"cat_slug" => $request->input('cat_slug'),
"cat_description" => $request->input('cat_description'),
"cat_parent_id" => $request->input('cat_parent_id')
];
// post to the database
$response = $this->categories->update($id, $inputs);
if($response['success']){
$message = $response['message'];
Session::flash('success', 'Category is successfully saved !'.' Server Response Message : '.$message);
return redirect()->route('categories.index');
}else{
$message = $response['message'];
Session::flash('success', 'Category is not successfully saved !'.' Server Response Message : '.$message);
return redirect()->route('categories.index');
}
/////////////////////////////////////////////////
// If the edit page should be shown
//return redirect()->route('categories.edit', $id);
}
My Repository :
public function update($id, $category){
return $this->update("categories/{$id}", $category);
}
And My Custom GuzzleHttpRequest.php :
protected function update($url, $data){
$formatted_data = json_encode($data);
$request = $this->client->request('PUT', $url, [
'body' => $formatted_data
]);
$response = json_decode( $request->getBody()->getContents() , true );
return $response;
}
My Server Accepts JSON formatted inputs : https://rest-banai.herokuapp.com/
Edited :
And My Edit Form :
{!! Form::open(['route' => ['categories.update', $category['cat_id']], 'method' => 'PUT', 'data-parsley-validate' => '']) !!}
<div class="form-group">
{{ Form::label('cat_name', 'Name:') }}
{{ Form::text('cat_name', $category['cat_name'], ['class' => 'form-control', 'placeholder' => 'Enter category name ...', 'required' => '', 'maxlength' => '50']) }}
</div>
<div class="form-group">
{{ Form::label('cat_slug', 'Slug:') }}
{{ Form::text('cat_slug', $category['cat_slug'], ['class' => 'form-control', 'placeholder' => 'Enter a slug word ...', 'required' => '', 'maxlength' => '50', 'data-parsley-type' => 'alphanum']) }}
</div>
<div class="form-group">
{{ Form::label('cat_description', 'Description:') }}
{{ Form::textarea('cat_description', $category['cat_description'], ['class' => 'form-control', 'rows' => '3', 'placeholder' => 'Enter description of the category ...', 'maxlength' => '255']) }}
</div>
<div class="form-group">
{{ Form::label('cat_parent_id', 'Parent Category:') }}
<br />
{{ Form::select('cat_parent_id', $cat_array, null, ['placeholder' => $cat_parent_name]) }}
<br />
</div>
<div class="pull-right">
{{ Form::submit('SAVE', ['class' => 'btn btn-block btn-success btn-sm']) }}
</div>
{!! Form::close() !!}
I am not sure what is I am doing wrong here, any expert, please help me with the issue as I am new with Guzzle and JSON working with Laravel, it will be appreciated.
And If anythings unclear here, please suggest to edit.
Thanks In Advance !
Change your request to
$request = $this->client->request('PUT', $url,['json'=> $data]);
You are using resourceful routes, and yet you have a lot of complexity in your code. This quite frankly in my opinion is completely unnecessary. I will accomplish what you are doing in the following way:
Routes
Route::resource('category', 'CategoryController');
Controller
public function edit(Category $category)
{
return view('category.edit', compact('category'));
}
public function update(Request $request, Category $category)
{
try {
$category->update($request->all()->except(['_token']));
Session::flash('success', 'Category is successfully saved !');
} catch(\Exception $exception) {
Session::flash('error', 'Unable to update category!');
}
return redirect(route('category.index'));
}
HTML
category/edit.blade.php
{!! Form::open(['route' => route('categories.update', $category), 'method' => 'PUT', 'data-parsley-validate' => '']) !!}
<div class="form-group">
{{ Form::label('cat_name', 'Name:') }}
{{ Form::text('cat_name', $category->cat_name, ['class' => 'form-control', 'placeholder' => 'Enter category name ...', 'required' => '', 'maxlength' => '50']) }}
</div>
<div class="form-group">
{{ Form::label('cat_slug', 'Slug:') }}
{{ Form::text('cat_slug', $category->cat_slug, ['class' => 'form-control', 'placeholder' => 'Enter a slug word ...', 'required' => '', 'maxlength' => '50', 'data-parsley-type' => 'alphanum']) }}
</div>
<div class="form-group">
{{ Form::label('cat_description', 'Description:') }}
{{ Form::textarea('cat_description', $category->cat_description, ['class' => 'form-control', 'rows' => '3', 'placeholder' => 'Enter description of the category ...', 'maxlength' => '255']) }}
</div>
<div class="form-group">
{{ Form::label('cat_parent_id', 'Parent Category:') }}
<br />
{{ Form::select('cat_parent_id', $cat_array, null, ['placeholder' => $cat_parent_name]) }}
<br />
</div>
<div class="pull-right">
{{ Form::submit('SAVE', ['class' => 'btn btn-block btn-success btn-sm']) }}
</div>
{!! Form::close() !!}
```
I am getting MethodNotAllowedHttpException retuned when updating a form in Laravel 5.2. I understand there could be an issue with the put method.
The form sending from the index:
{!! Form::model('Customers', ['route'=>['products.update', Auth::user()->id]]) !!}
{{ Form::hidden('business', Auth::user()->name, array('class' => 'form-control', 'required' => '','maxlength'=>'255'))}}
{{ Form::label('post', 'Mailbox')}}
{{ Form::checkbox('post',1, null, array('class' => 'form-control'))}}
The Controller is:
public function update(Request $request, $id)
{
$this->validate($request, array (
'post' => '',
'mailbox' => '',
'conum' => '',
'prefix' => '',
'telans' => '',
'TC' => 'required',
));
//store
$post = Customers::find($id);
$post->post = $request->input('post');
$post->postpro = $request->input('mailbox');
$post->telans = $request->input('telans');
$post->conum = $request->input('conum');
$post->prefix = $request->inut('prefix');
$post->tc = $request->input('TC');
//save
$post->save();
//session flash message
//Session::flash('success','This customer has now been added');
//redirect
return redirect('/home');}
And the route is as follows:
Route::resource('products', 'ProductsController');
Thank you
Your forgot the quotes, replace this :
PHP
$post->post = $request->input(post);
with this :
$post->post = $request->input('post');
do not forget to set the _method as put.
I have a problem with validation rule in edit form. My form fields are name, email, password and password_confirmation. My UpdateUserRequest class return this rules:
return [
'name' => ['required', 'max:50', 'min:3'],
'email' => ['required', 'email', 'unique:users,email,' .$this->route('users')],
'password' => ['required_with:password_confirmation', 'confirmed']
];
And update method in UsersController is:
public function update(Requests\UpdateUserRequest $request, $id)
{
$user = $this->users->findOrFail($id);
$user->fill($request->only('name', 'email', 'password'))->save();
return redirect(route('backend.users.edit', $user->id))->with('status', 'User has been updated.');
}
And this is a form:
{!! Form::model($user, [
'method' => $user->exists ? 'put' : 'post',
'route' => $user->exists ? ['backend.users.update', $user->id] : ['backend.users.store']
]) !!}
<div class="form-group">
{!! Form::label('name') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email') !!}
{!! Form::email('email', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password_confirmation') !!}
{!! Form::password('password_confirmation', ['class' => 'form-control']) !!}
</div>
{!! Form::submit($user->exists ? 'Save User' : 'Create New User', ['class' => 'btn btn-primary btn-sm']) !!}
Password validate rule required_with:password_confirmation should allow user to leave empty fields password and password_confirmation if he does not want to change it, but this doesn't work. When I leave the field empty, the empty value is hashed and stored in database. All other validations work fine, but not when user leaves empty value in password and password_confirmation. Why this isn't working? What am I doing wrong?
You will need to updated the db seperate without using fill etc as it will always get the password value empty or not and then add it, so try:
$user = $this->users->findOrFail($id);
$user->name = $request->get('name');
$user->email = $request->get('emil');
if(!empty($request->get('password')) {
$user->password = $request->get('password');
}
$user->save();
Something like that so you are checking if the PW needs to also be updated etc.
Basically you need to verify if it need to be entered or not, your method takes all the requested values and will update no matter if they are empty or note.
Im trying to do a submit a file with a form submission however I continue to get this error:
Error:
Trying to get property of non-object
at HandleExceptions->handleError('8', 'Trying to get property of non-object', '/Users/plastics1509moore/Desktop/elephant_gin/app/Http/Controllers/AdminController.php', '33', array('request' => object(Request), 'input' => array('_token' => 'y0ExMD4FoH3y1hRX61IOvMW520rn7AEx0UOzrc2R', 'title' => 'lol', 'description' => 'picture of gin one', 'link' => 'www.google.com', 'image' => object(UploadedFile)))) in AdminController.php line 33
I have files set to true. Is the issue the request all?
Here is the Controller function:
public function createSlider(Request $request)
{
$input = Request::all();
if (Input::hasFile('image')) {
$imageName = $input->id . '.' .
$request->file('image')->getClientOriginalExtension();
$request->file('image')->move(
base_path() . '/public/assets/image/', $imageName
);
$input->image = $imageName;
}
Sliders::create($input);
return redirect('/admin');
}
HTML
{!!Form::open(array('url' => 'admin/new_slider', 'files' => true)) !!}
<div class = "form-group">
{!!Form::label('title', 'Title:', ['class' => 'control-label']) !!}
{!!Form::text('title', null, ['class'=> 'input-mini ina tch'])!!}
{!!Form::label('title', 'Description:', ['class' => 'control-label']) !!}
{!!Form::text('description', null, ['class'=> 'input-mini '])!!}
</div>
<div class = "form-group">
{!!Form::label('title', 'Link:', ['class' => 'control-label']) !!}
{!!Form::text('link', null, ['class'=> 'input-mini'])!!}
{!!Form::label('title', 'Image:', ['class' => 'control-label']) !!}
{!! Form::file('image', ['id' => 'imgInp', 'class' => 'prev-upload']) !!}
</div>
<div class = "form-group">
{!!Form::submit('Submit', ['class'=> 'btn btn-default'])!!}
</div>
{!! Form::close() !!}
You are trying to get the id from the input. Your form isn't passing any id so naturally, your input won't have the id.
You can create the slider first and then get the id of the slider like this:
public function createSlider(Request $request)
{
$input = Request::all();
// Create slider
$slider = Sliders::create($input);
if (Input::hasFile('image')) {
// Use the slider id
$imageName = $slider->id . '.' .
$request->file('image')->getClientOriginalExtension();
$request->file('image')->move(
base_path() . '/public/assets/image/', $imageName
);
$input->image = $imageName;
}
return redirect('/admin');
}