Undefined variable in Laravel 8 - php

I have issue with the undefined variable in Laravel 8, I actually don't understand why this happen because I was trying and playing with the Laravel 8.
I created the same method and same coding but somehow when I tried to run the coding for page about I got the error that said
ErrorException
Undefined variable: about (View: D:\Server\htdocs\app\resources\views\pages\about.blade.php)
Why is this happening ? I don't understand. Because I use the exact same coding for my other pages and it works but when I try to open the about page it suddenly give me the error when other pages are perfectly fine with no error.
PagesController
class PagesController extends Controller
{
public function index()
{
$title = "Welcome to my blog";
// return view ('pages.index', compact('title')); // first method
return view ('pages.index')->with('title',$title); // 2 method
}
public function about()
{
$about = "About Page";
return view ('pages.about')->with('about',$about);
}
public function services()
{
$data = array(
'title' =>'Services' // array
);
return view ('pages.service')-> with($data);
}
}
about.blade.php
#extends('layouts.app')
#section('content')
<h1>{{$about}} </h1>
<p> This is about pages </p>
#endsection
index.blade.php
#extends('layouts.app')
#section('content')
<h1>{{$title}} </h1>
<p> This is tutorial </p>
#endsection
Just to show you the same coding i use for index and about
My route if anyone asking
Route::get('/', [PagesController::class,'index']);
Route::get('/about', [PagesController::class,'about']);
Route::get('/services', [PagesController::class,'services']);
index.blade.php
about.blade.php

public function about ()
{
$ about = "About Page";
return view ('pages.about') -> compact ('about');
}
Replace it as is and try it: your error will be solved.

php artisan route:cache
Clearing the route cache helped me.
My variable was added later and data was output from the cache

for people still suffering from this issue , head over to web.php and make sure you dont have a duplicated route.

Related

How to redirect to two different views in Laravel?

I'm a newbie to the Laravel framework. What I want to do is to redirect the users to a wait page firstly, then, they'll be redirected to their homepage after the wait screen.
I wrote some code for this but unfortunately it does not work. I searched about it but couldn't find a solution.
Here is my code:
function order(Request $req){
$data = [['user_id'=>Auth::id(),'order_note'=>$req->order_note, 'sugar'=>$req->order_sugar, 'amount'=> $req->amount, 'place_id'=>$req->order_place, 'beverage_id'=>$req->order_beverage]];
Orders::insert($data);
$this->redirectToWait(); // this is another function in the same controller.
sleep(10);
return redirect()->route('home');
}
function redirectToWait(){
return redirect()->route('wait');
}
This code only returns to the home route, it skips the redirectToWait() function.
Thank you for your help :)
You can't do this only using PHP & Laravel.
You have to use JavaScript to load first page then second page.
Controller:
function order(Request $req){
$data = [['user_id'=>Auth::id(),'order_note'=>$req->order_note, 'sugar'=>$req->order_sugar, 'amount'=> $req->amount, 'place_id'=>$req->order_place, 'beverage_id'=>$req->order_beverage]];
Orders::insert($data);
return view('wait-step-view-here');
}
wait-step-view-here.blade.php
...[SOME_HTML_HERE]
<script>
setTimeout(function(){
window.location.replace("{{ route('home') }}");
}, 10000);
</script>
...[SOME_HTML_HERE]

Laravel does not return data to the view

My Laravel-5.4 project was working fine. but now it's driving me crazy when trying to return data from controller to the view:
Requested url is :
Route::middleware(['web'])->group(function () {
Route::prefix('service')->group( function () {
Route::get('{service_slug}', 'ServiceController#findBySlug')->name('service.find_by_slug');
});
});
Controller action is as below:
function findBySlug ($slug)
{
return back()->withMessage('test message');
}
Here in the blade view i can not catch $message variable, in fact there is no $message variable in the view.
#php
if (isset($message))
dd($message);
#endphp
How can i fix it?...thanks.
You are redirecting. You are 'flashing' data to the session when you redirect 'with` data like that. You have to get it from the session.
Also there is nothing related to passing something from the controller to a view in your post.
Docs 5.4 - Responses - Redirecting with flashed session data

Why is the controller not redirecting to proper view in Laravel 5.4

In the below snippet, I have an html form in my example page with the form's action attribute set to exampleController URI in my Routes as follows:
htmlPage.blade.php
<form action="/exampleController" method="POST">
{{csrf_field()}}
// rest of form stuff goes here
</form>
#if(isset($result))
<table>
<td>{{ $result }}</td>
</table>
#endif
Web.php
Route::get('/gotoform', 'PageController#showform');
Route::post('/exampleController', 'PageController#processData');
Controller.php
class PageController extends Controller
{
public function showform()
{
return view('htmlPage');
}
}
public function processData(Request $request)
{
$result = $request['htmlInputData'];
return view('htmlPage')->with(compact('result');
}
}
Everything is fine from here and data is being processed and displayed correctly on the page but I was wondering why the URL address the controller returned for the $result variable is mydomain.com/exampleController and not mydomain.com/htmlPage which is the html page responsible for displaying the results of the operation.
Also, since the resulting URL address is mydomain.com/exampleController (which is not what I expected), its returning the following error when manually refreshed:
MethodNotAllowedHttpException in RouteCollection.php line 233:
Can somebody please enlighten me what did I missed?
thanks in advance.
EDIT:
by changing to Route::any the MethodNotAllowedHttpException error has gone away but still the URL returned by the controller is not right.
This happens because you return a View (view('htmlPage')).
if you want to access this url mydomain.com/htmlPage you need to redirect not load a View.
Use this return redirect(url('/htmlPage'))->with(compact('result');
In order to load this successfully you need to have another route :
Route::get('/htmlPage', 'PageController#loadhtmlPage');
and also to load the view in this url you have to do this in your controller:
public function loadhtmlPage(Request $request)
{
return view('htmlPage');
}

Passing a url value to view thru routes

I have a url this http://localhost/laravel/public/page/sign-up now..
I want to get the value of the url which is "sign-up" in the view..
Its not including the sign-up view.. I'm just new in laravel..
and before I posted here, I have done my research and found nothing relevant
in my master.blade.php I am using this
If anybody could enlighten me what is going on here and what is lacking.. I would really appreciate it
View
#if ($page == 'sign-up')
#include('pages/unregistred/sign-up')
#endif
Routes:
Route::get('page/{action}', function($action){
return View::make('layouts.master')->with('page', $action);
});
You can use Named routes:
Route::get('page/{action}', ['as' => 'sign-up', function($action){
return View::make('layouts.master')->with('page', $action);
}]);
And in the layout use:
#if (Route::currentRouteNamed('sign-up'))
#include('pages/unregistred/sign-up')
#endif
Also you can use Route::is() method for matching:
#if (Request::is('page/sign-up'))
Another way if the prefix of the page is /page/:
Request::segment(2); // return "sign-up"
Other info are in the docs: Request Information
I suggest you to use the route to show the correct view and not include them in the layout using conditions.
You can find a guide to the layout here: Blade templating
Try something like this:
Route:
Route::get('page/{type}', 'PageController#index');
Controller:
class PageController extends BaseController {
public function index($type)
{
return View::make('page.register', array('page' => $type));
}
}

Laravel route throwing: cannot find localhost

I'm trying to understand RESTfull controllers and Laravel 3 routing. I created a restfull controller Articles and now want to create the following methods:
GET: index
GET: write // (new but new is reserved)
GET: edit
POST: create
PUT: update
DELETE: destroy
However before I even started I keep getting redirected when pressing New article link in my view that should redirect to articles/write instead I get redirected to blank page with chrome error: Chrome can't find localhost.
My controller:
<?php
class Articles_Controller extends Base_Controller {
public $restful = true;
public function get_index()
{
return View::make('articles.index', array('articles' => Article::all()));
}
public function get_write()
{
return View::make('articles.new');
}
public function post_create()
{
return 'Created';
}
}
My routes:
?php
Route::get('/', 'home#index');
// Articles
Route::controller('articles');
My view:
<h1>Todays articles:</h1>
<?php
if(sizeof($articles) == 0)
{
echo 'No articles published';
}
?>
<br /><br />
<?= HTML::Link('articles/write', 'New article') ?>
Try using
HTML::link_to_action('articles#write', 'New article');
and set up your root URL right in the application/config/app.php to http://localhost or http://127.0.0.1!

Categories