How to call a function from another route in laravel php - php

im trying to call a function from another route in laravel php.
I have the route "welcome"
Route::get('/', function () {
return view('welcome');
});
where im calling this extends (sidebar)
#extends('layouts.guestnavbar')
i want to see if my table notifications is empty or not so i made this controller
class GuestNavbarController extends Controller
{
public function isempty(){
$notif = Notification::first();
if(is_null($notif)) {
return view('layouts.guestnavbar')->with("checkempty", "empty");
}else {
return view('layouts.guestnavbar')->with("checkempty", "not empty");
}
}
}
and i called the variable {{ $checkempty }} in my route guestnavbar
Route::get('/guestnavbar', [GuestNavbarController::class, 'isempty']);
and it works when im in the route guestnavbar
but doesnt work when im in the route welcome because i call the function in the route guestnavbar and in welcome he doesnt recognize the variable: checkempty
i need this function to be in the guestnavbar because i have to call it on other pages, not just in welcome page
I appreciate any help.

You don't need isempty inside controller, just add method isempty inside Notification model, you can use something like this inside Notification model:
public static function isEmpty(){
return Notification::first() ? true : false;
}
And then where you need to check if notification table is empty just call Notification::isEmpty()

Related

Laravel route resolving to a different method

I'm trying to set up a basic Laravel CRUD application, and I'm getting stuck setting up the pages for each action.
When I visit the route case/create, it opens the page for show instead.
routes/web.php
use App\Http\Controllers\HospitalCase as HospitalCase;
Route::controller(HospitalCase::class)->group(function() {
Route::get('/cases','index')->middleware('auth')->name('cases');
Route::get('/case/{id}','show')->middleware('auth');
Route::get('/case/create','create')->middleware('auth');
Route::post('/case/create','store')->middleware('auth');
Route::get('/case/edit/{$id}','edit')->middleware('auth');
Route::post('/case/edit/{$id}','update')->middleware('auth');
Route::get('/case/delete/{$id}','destroy')->middleware('auth');
});
HospitalCase.php controller
class HospitalCase extends Controller
{
function index()
{
echo 'index';
}
function create()
{
echo 'create';
}
function show($id)
{
echo 'show';
}
function store()
{
// validation rules
}
function edit($id)
{
return view('case/edit');
}
function update($id)
{
}
function destroy($id)
{
}
}
This is what I see on the browser:
I have been trying to figure this out for hours and can't think of what I'm doing wrong.
PS: The auth middleware is using laravel breeze (unmodified)
The reason it's showing the show route is because you defined
Route::get('/case/{id}','show')->middleware('auth');
before it, therefore, it's matching case/create as show('create')
Try defining the route afterwards.
Route::get('/case/create','create')->middleware('auth');
Route::post('/case/create','store')->middleware('auth');
Route::get('/case/{id}','show')->middleware('auth');
Just want to reiterate what #TimLewis has suggested, I think you need to put this route:
Route::get('/case/create','create')->middleware('auth');
Above this route:
Route::get('/case/{id}','show')->middleware('auth');
But you could try using Laravel’s route resource so you don’t need to write out all the routes -
use App\Http\Controllers\HospitalCaseController;
Route::resource('case', HospitalCaseController::class);

Laravel 8 return blank page

In controller;
public function list_of_approved_policies(){
dd("I am here");
die();
}
In route
Route::get('/listofapprovedpolicies','MerchantDashboardController#list_of_approved_policies');
When I calling /listofapprovepolicies Laravel return just white blank page. How can i solve this problem ?
try to change the route's declared sintax to
use App\Http\Controllers\MerchantDashboardController;
.....
Route::get('/listofapprovedpolicies',[MerchantDashboardController::class, 'list_of_approved_policies')->name('policies_list');
and in controller make the dump
public function list_of_approved_policies()
{
dd('dumped');
}

Laravel: How do I submit a form and stay in the same view?

I have in Laravel a view in which already foreach loops have been incorporated. At the bottom of the page is a small form. Now I want to send this form to save the data into the database.
At the same time I want to stay in the same view. If I enter the same page in the form, I get an error message. If I want to go back to the view via the controller, I also get an error.
In this error, the output of the data in the loop that was previously passed by another controller no longer works. - What can I do?
Undefined variable: data (View: /srv/users/smartfinance/apps/smartfinance/public/laravel/resources/views/home.blade.php)
<?php $__currentLoopData = $data; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
I hope you understand my problem and can give me a hint how to solve it.
These are the two controller:
class checkDataController extends Controller
{
public function data()
{
$data = DB::table('test')->where('booking_date', '=', $today)->get();
return view('home', compact("data"));
}
}
class AddContractController extends Controller
{
public function addNewData(Request $request)
{
$bez = $request->bez;
return view('home');
}
}
Typically this is done by redirecting back to the same page you submitted from. Here is what that would look like in your controller:
public function addNewData(Request $request)
{
$bez = $request->bez;
return back();
}
For a better user experience, you should also add a message to the view with the form:
#if(session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
And make one small change to your controller:
return back()->with('status', "Successfully submitted {$bez}!");
Your mistake is that you try to display the same view from two different controller methods. Normally, a view is only being used by one controller method. Other methods can redirect to this method in order to (re-)display the same view. This way, the logic of retrieving data for the view is only required in one place.
class CheckDataController extends Controller
{
// route 'check.data'
public function data()
{
$data = DB::table('test')->where('booking_date', '=', $today)->get();
return view('home', compact("data"));
}
}
class AddContractController extends Controller
{
// route 'contract.create'
public function addNewData(Request $request)
{
Contract::create($request->input()); // unsafe, only for demonstration...
return redirect()->route('check.data');
}
}
As you can see, instead of return view('home'), we are returning redirect()->route('check.data'). This will redirect the user to the other controller with the defined route. Of course this means you are actually executing two controller methods within one user action, but that's common practice.
More information about redirects can be found in the official documentation.
An advise for you my friend don't return a view from a post method.
I suppose the addNewData() method is a method that comes from the post route. so when you return a view after a post method you don't provide the data for your page. so laravel throws an error and complains about the missing variable. so what you must do is redirect to the route that views the page. So your method would look something like this:
public function addNewData(Request $request)
{
$bez = $request->bez;
return redirect('YOUR ROUTE TO VIEW THE PAGE (URL)');
}

Laravel AdminLTE - How to call method and use its data in view

I am using Laravel AdminLTE and I have it all configured, there is just one part I do not understand. I made my route like so:
Route::get('/admin/painlevel', function () {
return view('painlevel');
});
and I have this method in app/Http/Controllers/v1/PainLevelController.php
public function index()
{
return PdTpainlevel::select('pkpainlevel as id', 'painlevel_name as name')->get();
}
How would I call that method and display the data in my painlevel view?
Your current route is merely returning the view('painlevel') directly.
You need to update your route to:
Route::get('/admin/painlevel', 'V1\PainLevelController#index');
In your controller:
public function index()
{
$data = PdTpainlevel::select('pkpainlevel as id', 'painlevel_name as name')->get();
return view('painlevel', compact('data'));
}
You might want to start glancing through the documentations, start with Route, Controller and View
route create like this
Route::get('/administrator', 'administrator\LoginController#index');
and controller create like this
public function index()
{
$data['title']="Admin | DashBoard";
$data['name']="Dilip Singh Shekhawat";
view('administrator/menu_bar',$data);
return view('administrator/dashboard',$data);
}
its working .

Laravel returns error while the object exists

I have an global __construct() function inside my basecontroller, what looks like this:
public function __construct() {
$alerts = Alert::orderBy('id', 'DESC')->whereNull('deleted_at')->get();
return View::share('alert', $alerts);
}
This works on every page except on 2 pages...
My route looks like this:
Route::resource('/', 'WebsiteController');
Route::get('nieuws/{id}', 'NewsController#show');
Route::resource('login', 'LoginController');
Route::resource('auth', 'LoginController#auth');
Route::resource('logout', 'LoginController#logout');
Route::resource('foto', 'PictureController');
Route::resource('studio', 'PictureController#showStudioPics');
//special routing for the mirrors
Route::get('spiegels', 'PictureController#getPicList');
Route::get('spiegels/{dirName}', 'PictureController#showPicList');
//end of special routing
Route::resource('calendar', 'CalendarController');
Route::get('history', function()
{
return View::make('home.history');
});
Route::get('publicity', function()
{
return View::make('home.publicity');
});
The pages that aren't working are the following:
publicity
history
The error I get is Undefined variable: alert but that's weird, because inside my view I have this:
#foreach($alert as $alert)
<div class="alert alert-{{ $alert->type }}" role="alert" style="margin-bottom:-10px;">{{ $alert->message }}</div>
#endforeach
And on all the other pages it does work.
What do I wrong?
The two views that aren't working are not using any controller and thus are not extending the BaseController. If you create a controller for them which extends the Base one instead of having the function inside the route file then your issue is resolved.
Do this as follows:
1) Create a controller "ContentControler.php" which extends BaseController exactly like your other controllers do already.
Then have two methods:
public function getHistory()
{
return view('home.history');
}
public function getPublicity()
{
return view('home.publicity');
}
and in your routes:
change this:
Route::get('history', function()
{
return View::make('home.history');
});
Route::get('publicity', function()
{
return View::make('home.publicity');
});
to this
Route::get('history', 'App\Http\Controllers\ContentController#getHistory')
Route::get('publicity', 'App\Http\Controllers\ContentController#getPublicity')
*** In Laravel 4 Controllers do not fall under the App\Http\Controllers namespace so in routes you only need to declare the controllerName#methodName without the namespace which is a new feature of Laravel 5.

Categories