Trying to get property 'POST /employee HTTP/1.1 - php

I have created a laravel application to store employee data, but when I submit the form it gives me the following error, what should I do to avoid this problem. thanks
This is my EmployeeController store method
public function store(Request $request)
{
$this->validate($request,array(
'lastname'=>'required|max:60',
'firstname'=>'required|max:60',
'middlename'=>'required|max:60',
'address'=>'required|max:120',
'NIC'=>'required|max:10',
'city_id'=>'required|max:60',
'state_id'=>'required|max:60',
'mobile'=>'required|max:10',
'email'=>'required|max:60',
'postal_code'=>'required|max:10',
'birthdate'=>'required|date',
'date_hired'=>'required|date',
'department_id'=>'required|max:10',
));
$employee = new Employee();
$employee->lastname=$request->lastname;
$employee->firstname=$request->firstname;
$employee->middlename=$request->middlename;
$employee->address=$request->address;
$employee->NIC=$request->NIC;
$employee->city_id=$request->city_id;
$employee->state_id=$request->state_id;
$employee->mobile=$request->mobile;
$employee->email->$request->email;
$employee->postal_code=$request->postal_code;
$employee->birthdate=$request->birthdate;
$employee->date_hired=$request->date_hired;
$employee->department_id=$request->department_id;
$employee->save();
}
Form header
{!! Form::open(['route'=>'employee.store','class'=>'form-horizontal p-t-20']) !!}
Classes i used for the controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Department;
use Illuminate\Support\Facades\DB;
use App\Employee;

There is an error in your code.
$employee->email->$request->email;
This should be,
$employee->email = $request->email;

By the looks of it you are trying to validate the $request variable itself. Hence the Trying to get property POST
Should it not be...
$request->validate(array(
'lastname'=>'required|max:60',
'firstname'=>'required|max:60',
'middlename'=>'required|max:60',
'address'=>'required|max:120',
'NIC'=>'required|max:10',
'city_id'=>'required|max:60',
'state_id'=>'required|max:60',
'mobile'=>'required|max:10',
'email'=>'required|max:60',
'postal_code'=>'required|max:10',
'birthdate'=>'required|date',
'date_hired'=>'required|date',
'department_id'=>'required|max:10',
));

ErrorException: Trying to get property 'POST /Addpatients HTTP/1.1
This ERROR IS DUE to
when you difine your variable in any method Please check that you doest not define as below because it gives the error
$patients->address->$request->input('address');
The solution is of this is as below
$patients->address=$request->input('address');

Related

What's the best way to debug variables in Laravel?

I am new to Laravel and I need to debug some variables (requests) in my Controller. I've read that you can output variables into a view with functions like dd(). My frontend is built with Vuejs and it seems to be a hassle to echo them out from the Controller to the frontend. Are there better ways than echoing them out? Is it possible to log them?
NewsletterController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class NewsletterController extends Controller
{
public function submit(Request $request) {
$this->validate($request, [
'email' => 'required | email'
]);
$data = array('email' => $request);
dd($data);
DB::table('newsletter')->insert($data);
return response()->json(null, 200);
}
}
What would be the best way to debug $data variable in this case?
You can use var_dump
var_dump($data);
https://www.php.net/manual/en/function.var-dump.php
I came accross this:
\Log::info(print_r($var, true));
which dumps a variable or any content into laravel.log in a somehow human readable format.
dd is fine. You can inspect the output in the console.
Or you can dump the variable as json with
echo json_encode($data);
die;

Laravel notification won't send

I try to send notification to Twitter when new post created but I'm getting:
Couldn't post Notification. Response: Bad Authentication data.
Codes
Notification class
use NotificationChannels\Twitter\TwitterChannel;
use NotificationChannels\Twitter\TwitterStatusUpdate;
use App\Post;
class PostPublished extends Notification
{
use Queueable;
public function via($notifiable)
{
return [TwitterChannel::class, TelegramChannel::class];
}
public function toTwitter($post)
{
$title = $post->title;
$slug = $post->slug;
$image = $post->image;
return new TwitterStatusUpdate($title .' https://domain.co/blog/'. $slug, [$image]);
}
Post controller
use Illuminate\Notifications\Notifiable;
use App\Notifications\PostPublished;
$post->save();
$post->notify(new \App\Notifications\PostPublished($post));
Post model
use Illuminate\Notifications\Notifiable;
use Notifiable;
Question
Why I'm getting this error?
How to fix it?
It is definitely something wrong with your configuration or your tokens. It looks to me as if something is not set up properly. In your config/services.php file do you have the following?
'twitter' => [
'consumer_key' => env('TWITTER_CONSUMER_KEY'),
'consumer_secret' => env('TWITTER_CONSUMER_SECRET'),
'access_token' => env('TWITTER_ACCESS_TOKEN'),
'access_secret' => env('TWITTER_ACCESS_SECRET')
]
Please check to ensure all of these are set correctly using tinker. In the terminal type php artisan tinker and then check each of the following one line at a time:
env('TWITTER_CONSUMER_KEY'),
env('TWITTER_CONSUMER_SECRET'),
env('TWITTER_ACCESS_TOKEN'),
env('TWITTER_ACCESS_SECRET')
SOLVED
All I needed to do was :
php artisan config:cach
composer dump-autoload
now it's working like charm.
Note it can be type error in any part of .env file, not only twitter access tokens and key, for example:
APP_NAME=Name of your App
instead of:
APP_NAME="Name of your App"

Very Confusing MethodNotAllowedHttpException on a put request laravel

So far all attempts to modify the routing methods have failed.
Been following some documentation on laravel restful controllers and have one set up to do basic editing and adding of items to a database. It was going well till I hit the snag on... well I'm not sure what precisely is triggering the problem, but basically, everything works till I hit submit on the form and then it's Game Over.
Normally I'd be able to diagnose this by checking to see if I'm using the right call, or made a spelling mistake or something. But this is a new request for me, so I can't quite debug where the problem is coming from.
This is the error those who know what to look for. In full here.
MethodNotAllowedHttpException in RouteCollection.php line 218:
My routes are pasted here.
A printout of the routes is here:
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ContactFormRequest;
use App\UserEdit;
use DB;
use App\Http\Requests;
class EditUserController extends Controller
{
public function index()
{
$array = UserEdit::all()->toArray();
return view('UserEntry', compact('array'));
}
public function create()
{
$id = UserEdit::find(715)->toArray();
return view('NewUser', compact('id'));
}
public function store(UserFormRequest $request)
{
//$user = new UserEdit([
// 'name'=>$request->get('First_Name'),
// 'email'=>$request->get('email'),
// 'username'=>$request->get('name')
//]);
//
//$user->save();
//return \Redirect::route('users')->with('message', 'Nice Work.');
}
public function show($id)
{
try {
$array = UserEdit::findorFail($id)->toArray();
return view('UserEdit')->with('array', $array);
} catch(\Exception $e) {
return \Redirect::route('users.index')
->withMessage('This user does not exist');
}
}
public function edit($id)
{
$user = UserEdit::findorFail($id);
return view('EditUser')->with('user',$user);
}
public function update($id, UserFormRequest $request)
{
$user = UserEdit::findorFail($id);
$user->update([
'name' => $request->get('name'),
'email' => $request->get('email')
]);
return \Redirect::route('users.edit', [$user->id])->with('message', 'Details Updated!');
}
public function destroy($id)
{
//
}
}
The Blade is here.
if you have a hard time finding the solution the easiest solution is using
Route::any('users/{user}', 'UserEntryController#update');
this allow you to access this action with any method type
OR
Route::match(array('get', 'put'), 'users/{user}', 'UserEntryController#update');
so you need 2 method which are
get -> view
put -> submit update
you can just indicate which method type you want to be accessible with in this action
i think you are using model in form.try this
{{ Form::open(['method' => 'put', 'route'=>['users.update', $user->id], 'class'=>'form']) }}
As per your route list and route put doesnt taking id so you get method not found exception
PUT users/{user} App\Http\Controllers\EditUserController#update
instead of using resouce just type each route for each method
Route::put('users/{user}', 'EditUserController #update');
It seems like after sorting out the routes, the issue fell to a bad capitalisation. $user->id should have been $user->ID.

Using a CustomRequest for Form Validation in Laravel 5.1 fails?

I've created a custom Request called CustomerRequest that I want to use to validate the form fields when a new customer is created. I cannot get it to work, it appears to be continuing into the store() method even when it should fail.
I have three required fields: givenname, surname, email
Here is my CustomerRequest:
public function rules()
{
return [
'givenname' => 'required|max:3',
'surname' => 'required',
'email' => 'required|unique:customers,email',
];
}
Here is my CustomerController:
use pams\Http\Requests\CustomerRequest;
-----------------------------------------
public function store(CustomerRequest $request, Customer $customer)
{
$request['created_by'] = Auth::user()->id;
$request['modified_by'] = Auth::user()->id;
$customer->create($request->all());
return redirect('customers');
}
When I submit the form using a givenname of "Vince" it should fail because it is greater than 3 characters long, but instead I get this error:
FatalErrorException in CustomerController.php line 52: Cannot use object of type pams\Http\Requests\CustomerRequest as array
Line 52 in the controller is $request['created_by'] = Auth::user()->id;
From what I understand, if the CustomerRequest fails then the user should be redirected back to the customers.create page and not run the code contained in the store() method.
I found the problem.
CustomerRequest had:
use Request;
instead of:
use pams\Http\Requests\Request;
Now the validation passes and fails as expected.
From docs, in Form Request Validation:
All you need to do is type-hint the request on your controller method.
The incoming form request is validated before the controller method is
called, meaning you do not need to clutter your controller with any
validation logic
Solution would be if you use it like so:
CustomerController.php
use Illuminate\Http\Request;
// ...
public function store(CustomerRequest $customerRequest, Customer $customer,Request $request)
{
$request['created_by'] = Auth::user()->id;
$request['modified_by'] = Auth::user()->id;
$customer->create($request->all());
return redirect('customers');
}
So what you want to know is that FormRequest -- which you are extending in your custom request validator CustomerRequest.php -- is a bit different than request that resides in Illuminate\Http namespace.
actually, you may find out why if you run (with your old code) dd($request['created_by'] = Auth::user()->id);. you should get the word forbidden or maybe! an exception telling that it'is not instantiable. (in my case I got forbidden because I've tested it on 5.2 right now).
$request is object, use it like following
use pams\Http\Requests\CustomerRequest;
-----------------------------------------
public function store(CustomerRequest $request, Customer $customer)
{
$inputs = $request->all();
$inputs['created_by'] = Auth::user()->id;
$inputs['modified_by'] = Auth::user()->id;
$customer->create($inputs);
return redirect('customers');
}

Laravel 5.1 PHP

I'm new to coding and Laravel 5.1, and after watching the tutorials by Laracasts I have been creating my own webpage. I came across and error that I cant fix...
Method [send] does not exist.
My code looks like this:
namespace App\Http\Controllers;
use Mail;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ContactController extends Controller
{
/**
*
* #param Request $request
*/
public function emailContactForm (Request $request){
$msg = $request->input('message');
$name = $request->input('name');
$email = $request->input('email');
//
$this->validate($request, [
'title' => 'required|max 500',
'name' => 'required',
'email' => 'required',
]);
//
Mail::send(
'emails.contactForm',
[
'message'=>$msg,
'name'=>$name,
],
function($m) use ($email) {
$m->to('jessica.blake#autumndev.co.uk', 'say hi')
->subject('new message')
->from($email);
}
);
//
return;
}
}
I'm trying to use the mail function, which we have now got working, but the send still doesn't? Any suggestions? Thanks!
EDIT: Full stack trace as per laravel log file: http://pastebin.com/ZLiQ7Wgu
At the very first sight, you are calling the controller method send() but you actually named it emailContactForm()
You dont post routes and actions so the quick fix by now is trying to rename emailContactForm to send, despite instead you should probably need to review all your related routing logic.

Categories