ErrorException in registerController.php line 18: Undefined variable: name - laravel

I made a signup form with laravel 5 and im getting this error
ErrorException in registerController.php line 18:
Undefined variable: name
<?php
namespace App\Http\Controllers;
use Hash;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class registerController extends Controller
{
public function register()
{
$request = new Request;
$user = new User;
$user->$name = $request->input('nameInputSignup');
$user->$email = $request->input('emailInputSignup');
$user->$password = HASH::make($request->input('passwordInputSignup'));
$user->save();
return view('index');
}
}
Route::post('/register' , 'registerController#register');
here is my code, can anyone help with this error.

Drop the extra $ from the members of your 'user' object:
$user->name
$user->email
$user->password

Related

Undefined variable:title

I added migrate in database, but why getting error I don't know, I am pretty new to Laravel, I don't know how can I fix this.
Error is:
Undefined variable: title
My code:
namespace App\Http\Controllers\Admin;
use App\Models\Abouts;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AboutusController extends Controller
{
public function index(){
return view('admin.aboutus');
}
public function store(Request $request){
$aboutus = new Abouts();
$aboutus->$title = $request->input('title');
$aboutus->$subtitle = $request->input('subtitle');
$aboutus->$description = $request->input('description');
$aboutus->save();
return redirect('/abouts')->with('success','nice');
}
}
you can write this way also.
namespace App\Http\Controllers\Admin;
use App\Models\Abouts;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AboutusController extends Controller
{
public function index(){
return view('admin.aboutus');
}
public function store(Request $request){
$input = $request->all();
Abouts::create($input); //here About us your model
return redirect('/abouts')->with('success','nice');
}
}

Laravel ViewComposer - Undefined variable: countUnreadNotifications

In my Laravel-5.8, I am trying to use ViewComposers so that I can display data in layouts\header
App\http\View\Composers\NotificationsComposer
<?php
namespace App\http\View\Composers;
use App\Models\Notification\UserNotification;
use Illuminate\View\View;
use Illuminate\Support\Facades\Auth;
class NotificationsComposer
{
public function compose(View $view)
{
$userCompany = Auth::user()->company_id;
$userID = Auth::user()->id;
$countUnreadNotifications = UserNotification::where('send_to', $userID)->where('company_id', $userCompany)->count();
$unreadNotifications = UserNotification::where('send_to', $userID)->where('company_id', $userCompany)->orderBy('created_at', 'desc')->take(5)->get();
$allNotifications = UserNotification::where('send_to', $userID)->where('company_id', $userCompany)->orderBy('created_at', 'desc')->get();
return $view->with([
'countUnreadNotifications' => $countUnreadNotifications,
'unreadNotifications ' => $unreadNotifications,
'allNotifications ' => $allNotifications
]);
}
AppServiceProvider
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use App\Models\Notification\UserNotification;
use App\Http\View\Composers\NotificationsComposer;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
View::composer(['layouts.header'], NotificationsComposer::class);
}
}
layouts\header.blade
<span class="dropdown-item dropdown-header">You have {{ $countUnreadNotifications }} unread notifications</span>
When I logged in I got this error:
Undefined variable: countUnreadNotifications (View: C:\xampp\htdocs\resources\views\layouts\header.blade.php)
How do I resolve this?
The error is here 'unreadNotifications '. the is a space before the parenthesis. So I removed the space 'unreadNotifications' and everything works fine. Thank you

Laravel FatalErrorException

I created a new view, route and controller in laravel and when i go to access the view i get an error, all other views are fine.
Error
FatalErrorException in shopsalescontroller.php line 11: syntax error, unexpected 'class' (T_CLASS), expecting ',' or ';'
this is my shop sales controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\shopsales
class shopsalescontroller extends Controller : (line 11)
{
public function index()
{
$storeNum = request('storeNum');
$results = shopsales::where('StoreNumber','=',$storeNum)->get();
return view('shopSales',compact('results'));
}
}
PHP version 5.6.3
Zend engine v2.6.0
Laravel framework version 5.2.45
You missed a semicolon on this line:
use App\shopsales;
Just remove the : (line 11) text from your code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\shopsales
class shopsalescontroller extends Controller {
public function index() {
$storeNum = request('storeNum');
$results = shopsales::where('StoreNumber', '=', $storeNum)->get();
return view('shopSales', compact('results'));
}
}

Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model

I am pretty new to Laravel and I am trying to add the post, create by a user into the database. But when I do so, following error comes:
Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save()
must be an
instance of Illuminate\Database\Eloquent\Model, string given,
called in C:\xampp\htdocs\lar\app\Http\Controllers\PostController.php on line
25 and defined
User model:
<?php
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts()
{
return $this->hasMany('App\Post');
}
}
Post Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user()
{
return $this->belongsTo('App\User') ;
}
}
PostController:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class postController extends Controller
{
public function postCreatePost(Request $request){
// Validation
$post = new Post();
$post->$request['body'];
$request->user()->posts()->save('$post');
return redirect()->route('dashboard');
}
}
Post Route:
Route::post('/createpost',[
'uses' => 'PostController#postCreatePost',
'as'=>'post.create'
]);
Form action:
<form action="{{route('post.create')}}" method="post">
Please tell me how to fix this.. How to fix this?
Thank you in advance.. :)
I think what you want is this:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class postController extends Controller
{
public function postCreatePost(Request $request){
// Validation
$post = new Post();
// here you set the body of the post like that
$post->body = $request->body;
// here you pass the $post object not as string
$request->user()->posts()->save($post);
return redirect()->route('dashboard');
}
}
You need to pass the $post object as an object to the save method. You was doing this: $user->posts()->save('$post') when you need to do this: $user->posts()->save($post).
Hope it helps.

Getting this error continuously: MethodNotAllowedHttpException in RouteCollection.php line 218:

When I tried to log in, on my website. It responded with the message:
FatalErrorException in UserController.php line 37:
Class 'App\Http\Controllers\Auth' not found
But, the file there is controllers folder in Http containing Auth where there are four files.
UserController:
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
//use Illuminate\Support\Facades\Flash;
use InvalidConfirmationCodeException;
use Flash;
//use Mail;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class UserController extends Controller
{
public function getDashboard()
{
return view('dashboard');
}
public function postSignUp(Request $request)
{
$email = $request['email'];
$name = $request['name'];
$password = bcrypt($request['password']);
$user = new User();
$user -> email = $email;
$user -> name = $name;
$user -> password = $password;
Auth::login('$user');
$user->save();
return redirect()->route('dashboard')
}
public function postSignIn(Request $request)
{
if (Auth::attempt(['email' => $request['email'], 'password' => $request['password']])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}
}
also, I am getting an error while signup:
MethodNotAllowedHttpException in RouteCollection.php line 218:
And I am trying to solve this error from a very long time but no success till now.
Please help me with both the errors.
change Auth::login($user) --> \Auth::login($user)
and let me know if you are getting anything in database after signup . :)
To get rid off
FatalErrorException in UserController.php line 37: Class
'App\Http\Controllers\Auth' not found
Add use Auth at the top
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use Auth;
Change Auth::login($User) to \Auth::login($user), your $user variable is incorrect as well.
Check your signup route, is it doing a POST (most likely it is) and you're sending a GET request?
You need add
use Auth;
to your Controller.
I highly doubt that the Auth class is inside your Controllers directory. The Auth you are looking for, is probably the Auth facade, can be included this way:
use Illuminate\Support\Facades\Auth;

Resources