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 .
Related
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);
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()
i have tried to passing data to multiple blade in controller but get error. Here bellow my code
public function index()
{
$news = DB::table('beritas')
->select('id','judul_berita','created_at')
->get();
return view (['berita.daftar-berita', 'more-menu.berita'])->with(compact('news'));
}
How to pass data to multiple blades in laravel and with a single route?
If u want pass data to multiple blades u can share it in the Constructor like so:
public function __construct(){
$this->middleware(function ($request, $next) {
$news = DB::table('beritas')>select('id','judul_berita','created_at')->get();
View::share('news', $news);
return $next($request);
});
}
and now u can use news variable in all you blades that using the same controller.
i hope it's will help you
Probably the right place to this is in the boot method of some service provider, for example, AppServiceProvide.
//AppServiceProvider.php
public function boot()
{
view()->share('someVariable',$someVariable);
}
This will make someVariable available to all of blade views. This is useful for template level variables.
how can i use same route for two different controller function methods in laravel
first controller
public function index()
{
$comproducts = Comproduct::paginate(6);
$items = Item::orderBy('name')->get();
return view('computer', compact(['comproducts', 'items']));
}
second controller
public function index()
{
return view('search.index');
}
i want to use these two different controller functions for one route.
This is my route name
Route::get('/computer', [
'uses' => 'ComputerProductsController#index',
'as' => 'computer.list'
]);
laravel needs somehow to identify which exactly method you want. for example you can pass the parameter, which will identify which method to call.
public function index(Request $request)
{
// if param exists, call function from another controller
if($request->has('callAnotherMethod')){
return app('App\Http\Controllers\yourControllerHere')->index();
}
$comproducts = Comproduct::paginate(6);
$items = Item::orderBy('name')->get();
return view('computer', compact(['comproducts', 'items']));
}
You can't. If you want to add search functionality to your first controller's index page, you should determine which page to show inside your controller.
A possible example controller:
public function index(Illuminate\Http\Request $request)
{
// If the URL contains a 'search' parameter
// (eg. /computer?search=intel)
if ($request->has('search')) {
// Do some searching here and
// show the search results page
return view('search.index');
}
$comproducts = Comproduct::paginate(6);
$items = Item::orderBy('name')->get();
return view('computer', compact(['comproducts', 'items']));
}
I'm currently studying laravel and for my small project, and I'm having a small issues now.
I'm trying to handle php input in url i.e http://example.com/?page=post&id=1
I currently have this in my controller for post.blade.php
public function post($Request $request)
{
$page = $request->input('page');
$id_rilisan = $request->input('id');
$post = Rilisan::where('id_rilisan', '=', $id_rilisan)->first();
if($post = null)
{
return view('errors.404');
}
return view('html.post')
->with('post', $post);
}
and this is the controller
Route::get('/', 'TestController#index');
Route::get('/{query}', 'TestController#post' );
How to process the php input to be routed to controller? I'm very confused right now, I've tried several other method for the Route::get
This route Route::get('/', 'TestController#index') directs user to the index route. So, if you can't change URL structure and you must use this structure, you should get URL parameters in the index route like this:
public function index()
{
$page = request('page');
$id = request('id');
You can use it a as parameter on your controller :-) see this answer please: https://laravel.io/forum/07-26-2014-routing-passing-parameters-to-controller
for example query parameter in route would be $query parameter in controller method :-)
So like this:
Route::get('/{query}', 'TestController#post' );
//controller function
public function controllerfunc($query){}
Why do you need to use query parameters in url. You can simply use this structure http://example.com/posts/1
Then your routes will look like this:
Route::get('/posts/{post}', 'PostsController#show');
And you will be able to access Post model instantly in your show method.
Example:
public function show(Post $post) {
return view('html.post', compact('post'));
}
Look how small is your code now.