Laravel undefined variable in view, Laravel 5.2 using AdminLTE - laravel-5

I am getting an error in laravel 5.2
when I access it using the storeMessage function on my Controller
This is my ERROR STATEMENT:
ErrorException in 6e12af76911d23528f6e0ea587ccab13391eacf9.php line 37:
Undefined variable: thisPage (View: E:\xampp\htdocs\beasiswa\resources\views\layouts\partials\sidebar.blade.php) (View: E:\xampp\htdocs\beasiswa\resources\views\layouts\partials\sidebar.blade.php) (View: E:\xampp\htdocs\beasiswa\resources\views\layouts\partials\sidebar.blade.php)
And this is my code:
View Code
<?php $thisPage = "jenisbeasiswa"; ?>
#extends('layouts.app')
#section('htmlheader_title')
Portal Beasiswa
#endsection
#section('contentheader_title')
Broadcast Beasiswa via Telegram Messenger
#endsection
#section('main-content')
<form action="{{ url('/send-message') }}" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea name="message" id="message" class="form-control" placeholder="Enter your query" rows="10"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
#endsection
and this is my Controller Code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Telegram\Bot\FileUpload\InputFile;
use Telegram\Bot\Laravel\Facades\Telegram;
class TelegramBotController extends Controller
{
public function updatedActivity()
{
$activity = Telegram::getUpdates();
dd($activity);
}
public function sendMessage()
{
return view('beasiswa.telegram');
}
public function storeMessage(Request $request)
{
$request->validate([
'email' => 'required|email',
'message' => 'required'
]);
$text = "[HEADLINE] Informasi Portal Beasiswa\n"
. "<b>Email Address: </b>\n"
. "$request->email\n"
. "<b>Message: </b>\n"
. $request->message;
Telegram::sendMessage([
'chat_id' => env('TELEGRAM_CHANNEL_ID', ''),
'parse_mode' => 'HTML',
'text' => $text
]);
return redirect()->back();
}
}

Apparently sidebar.blade.php is referring to the $thisPage variable (which is declared at the top of your view code). My guess is, you are including the sidebar in the layouts.app template. But as you extend your layouts.app the $thisPage variable will get rendered in the sidebar first before the declaration comes in your view code. And thus, it will be undefined in the Sidebar.
So, you should try another approach. Why do you even want to set the $thisPage variable? Maybe you can pass it in from the controller?

Related

Email works with route using get request but email will not send with post request and there are no errors just a page refresh

I have verified emails are sending via mailgun with a public function mail using a get request with no data on the page. Trying to send an email from a contact page with a POST request simply refreshes the page with no errors but email does not send.
I have set the .env and config.mail / config.services and successfully send mail using a get request in web.php.
First I used Terminal php artisan make:mail. Then I created two routes for a contact page (GET / POST), created a contact view (works), send the data to PagesController.php, run a validation of the three inputs, pass the data to Mail::send. On click of submit button the page refreshes with no errors (validation not working) and email doesn't send.
PagesController.php:
Namespace(
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
use App\Mail\Welcome;
use Mail;
class PagesController extends Controller
{
public function getContact(){
return view('pages/contact');
}
public function postContact(Request $request){
$this->validate($request,[
'email' => 'required|email',
'subject' => 'min:3',
'message' => 'min: 3'
]);
$data = array(
'email' => $request->email,
'subject' => $request->subject,
'bodyMessage' => $request->message
);
\Mail::send('email.Test', $data, function($message) use ($data){
$message->from($data['email']);
$message->to('visionquest.jesse#gmail.com');
$message->subject($data['subject']);
});
}
Web.php:
Route::get('contact', 'PagesController#getContact');
Route::post('contact', 'PagesController#postContact');
Test.blade.php:
<html>
<h1> Email from: {{ $email }}</h1>
<p> {{ $subject }} </p>
<br><br>
<hr>
<p>{{ $bodyMessage}}</p>
contact.blade.php:
#extends('main')
#section('title', '| Contact')
#section('content')
<div class="row">
<div class="col-md-12">
<h1>Contact Me</h1>
<hr>
<form acion="{{ url('contact') }}">
{{ csrf_field() }}
<div class="form-group">
<label name="email">Email:</label>
<input id="email" name="email" class="form-control">
</div>
<div class="form-group">
<label name="subject">Subject:</label>
<input id="subject" name="subject" class="form-control">
</div>
<div class="form-group">
<label name="message">Message:</label>
<textarea id="message" name="message" class="form- control">Type your message here...</textarea>
</div>
<input type="submit" value="Send message" class="btn btn-success">
</form>
</div>
</div>
#endsection
All I want to do is pull the information from the form and send it via email to a set address. I have been working on this for over week and now that I am not getting errors I have no idea what to do.
You can add this above the form in your blade file. You would be able to see the validation errors if any :
#if ($errors->any())
#foreach ($errors->all() as $error)
<div>{{$error}}</div>
#endforeach
#endif
Also change :
<form acion="{{ url('contact') }}">
To
<form action="{{ url('contact') }}" method="POST">
put some method on your form : method="POST" because once you will submit it, it will route to the GET route not the POST route since you did not declare a method on your form

Laravel undefined variable: search

Version of Laravel 5.0 and following is my code:
My form:
<form method="get" action="{{ url('result')}}" class="search-wrap" >
<div class="form-group">
<input type="text" class="form-control search" placeholder="Search" name="key"/>
<button class="btn btn-primary submit-search text-center" type="submit">
<i class="icon-search"></i>
</button>
</div>
</form>
My controller:
use Illuminate\Http\Request;
use App\Http\Requests;
use App\products;
public function searchName(Request $request){
$key = $request->key;
$data = product::where('name','like','%'.$key.'%')->get();
return view('footwear/result',['search'=>$data]);
}
My route:
Route::get('result', 'myControler#searchName');
My view:
<?php
var_dump($search);
?>
I receive the following error: (Undefined variable: search)
Problem is in your routes, why don't you
Try:
Route::post('result', 'myControler#searchName');
or
Route::any('result', 'myControler#searchName');
change method to post in form too, and try.
NOTE: I should have added this as comment but my rep is less than 50.

ReflectionException in Route.php line 280: Method App\Http\Controllers\UserController::Signup() does not exist

I am a new programmer on laravel. I am currently using laravel 5.2. I ran into this error while i was trying to input data into a form i created as welcome.blade.php. I have check the route and it seems very okay. I dont what i may be doing wrong. This is problem display
ReflectionException in Route.php line 280:
Method App\Http\Controllers\UserController::Signup() does not exist
UserController
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function postSignUp(Request $request)
{
$email = $request['email'];
$first_name = $request['first_name'];
$password = bcrypt($request['password']);
$user = new User();
$user->email = $email;
$user->first_name = $first_name;
$user->password = $password;
$user->save();
return redirect()->back();
}
public function postSignIn(Request $request)
{
}
}
Routes
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
});
Route::post('/signup', [
'uses' => 'UserController#Signup',
'as' => 'signup',
]);
});
welcome.blade.php
#extends('layouts.master')
#section('title')
Welcome!
#endsection
#section('content')
<div class="row">
<div class="col-md-6">
<h2>Sign Up</h2>
<form action="{{route('signup')}}" method="post">
<div class="form-group">
<label for="email">Your Email</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
<label for="first_name">Your First Name</label>
<input class="form-control" type="text" name="first_name" id="first_name">
</div>
<div class="form-group">
<label for="password">Your Password</label>
<input class="form-control" type="text" name="password" id="password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{Session::token()}}">
</form>
</div>
<div class="col-md-6">
<h2>Sign In</h2>
<form action="#" method="post">
<div class="form-group">
<label for="email">Your Email</label>
<input class="form-control" type="text" name="email" id="email"></div>
<div class="form-group">
<label for="password">Your Password</label>
<input class="form-control" type="text" name="password" id="password"></div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
#endsection
You're using diffrent function name according to route, both will be same.
Replace your function name with this:-
postSignUp to SignUp
Thanks for your contribution as suggested i change the postSignUp to SignUp in the userController
public function postSignUp(Request $request)
change to
public function SignUp(Request $request)
also in the route I change the
'uses' => 'UserController#Signup',
change to
'uses' => 'UserController#SignUp',

How to create a user in Laravel?

I am creating a CMS App where the user should register first.
I created a RegisterController where I define index() method which returns the view register.blade.php!
When the user clicks Register button, the request should pass to create() method alongside /register URL and create a user.
Since, I already defined that /register should open register.blade.php, then how can I run another method to create a user under the same URL?
I also don't want to use php artisan make:controller RegisterController --resource.
RegisterController.php
<?php
namespace App\Http\Controllers\Authentication;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class RegisterController extends Controller
{
public function index() {
return view('auth.register');
}
protected function validator(array $data) {
return $data->validate([
'fname' => 'required|string|max:255',
'lame' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6',
]);
}
protected function create(Request $request) {
$this->validator($request->all)->validate();
}
}
register.blade.php
#extends('master')
#section('title')
Register - CMS APP
#endsection
#section('styles')
<link rel="stylesheet" href="{{ asset('css/auth.css') }}">
#endsection
#section('register')
<form class="form-signin" method="post" accept-charset="utf8" action="/register/create">
#csrf
<img class="mb-4" src="http://logo.kenh.net/logo/bootstrap-4.svg.png" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal">Please Register</h1>
<label for="inputFirstName" class="sr-only">First Name</label>
<input name="fname" type="string" id="inputFirstName" class="form-control" placeholder="First Name" autofocus value="{{old('fname')}}">
#if($errors->has('fname'))
<div class="alert-danger">
<p>First Name is required</p>
</div>
#endif
<label for="inputLastName" class="sr-only">Last Name</label>
<input name="lname" type="string" id="inputLastName" class="form-control" placeholder="Last Name" autofocus value="{{old('lname')}}">
#if($errors->has('lname'))
<div class="alert-danger">
<p>Last Name is required</p>
</div>
#endif
<label for="inputEmail" class="sr-only">Email address</label>
<input name="email" type="email" id="inputEmail" class="form-control" placeholder="Email address" autofocus value="{{old('email')}}">
#if($errors->has('email'))
<div class="alert-danger">
<p>Email is required</p>
</div>
#endif
<label for="inputPassword" class="sr-only">Password</label>
<input name="password" type="password" id="inputPassword" class="form-control" placeholder="Password">
#if($errors->has('password'))
<div class="alert-danger">
<p>Password is required</p>
</div>
#endif
<button class="btn btn-lg btn-success btn-block" type="submit">Register</button>
<br>
Login
</form>
#endsection
Routes(web.php):
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('register', 'Authentication\RegisterController#index');
Route::get('/login', function() {
return view('auth.login');
})->name('login');
Why not use Laravel Auth which will build the auth database, views and logic for you?
php artisan make:auth
https://laravel.com/docs/5.6/authentication
EDIT:
Ok, so If I understand correctly, you want your post and get pointing to the same route url?
Route::match(array('GET','POST'),'/register', 'RegisterController#index');
{
//
});
You can try the routing in this form:
Route::get('/user', 'UserController#index');
This might work for you.
Also, don't forget to go through the official documentation of Laravel regarding Routing.
https://laravel.com/docs/5.6/routing
This will help you alot.
The form is sending a post request to a certain route which in this case is /register but you have not supplied that route yet.
Route::post('register', 'Authentication\RegisterController#create');
Additionally, your form action can just be action="register"

Laravel routes, form won't submit data into the database

beginner trying to submit a basic form into the database using laravel. I know I should be using 'get' to display the registerform but when I do that I get the 'methodnotallowedhttpexception error.
Currently when I enter data into the form and press submit it just refreshes the page. Any help would greatly be appreciated, thanks
web.php
Route::post('registerForm', 'AuthController#viewregisterForm');
Route::post('registerUser', 'AuthController#registerUser');
AuthController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
//use Illuminate\Support\Facades\Auth;
//use App\Http\Requests;
use App\User;
class AuthController extends Controller
{
function viewregisterForm()
{
return view('register/registerForm');
}
function registerUser(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'password' => 'required',
'dateofbirth' =>'required',
]);
//create a Film object
$user = new $User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = $request->password;
$user->dateofbirth = $request->dateofbirth;
$user->role = 1;
$user->save();
return redirect('all');
}
}
?>
registerForm.blade
#extends('layouts.master')
#section('title', 'Register user')
#section('content')
<form action="{{url('registerForm')}}" method="POST">
{{ csrf_field() }}
<h1>Register user</h1>
<div>
<label for="title">Enter name</label>
<input type="text" name="name" id="name">
</div>
<div>
`enter code here`<label for="title">Enter email</label>
<input type="text" name="email" id="email">
</div>
<div>
<label for="title">Enter password</label>
<input type="text" name="password" id="password">
<label for="title">Enter date of birth</label>
<input type="text" name="dateofbirth" id="dateofbirth">
</div>
<input type="submit" name="submitBtn" value="Add User">
</form>
#endsection
You need to change the action of your form submit
from
<form action="{{ url('registerForm') }}" method="POST">
to
<form action="{{ route('register-user') }}" method="POST">
and change the route from
Route::post('registerUser', 'AuthController#registerUser');
to
Route::post('registerUser', ['as' => 'register-user', 'uses' => 'AuthController#registerUser']);
Try to change your routes to this:
Route::get('registerForm', 'AuthController#viewregisterForm');
Route::post('registerForm', 'AuthController#registerUser');

Resources