Diaplay data from MySQL to sub folder views in Laravel 9 - php

I have created pages in view directory as views/mech/class
I want to display database data to 'views/mech/class.blade.php' page and using controller App/Controllers/mechplace.php
App/Controllers/mechplace.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Placements;
class mechplace extends Controller
{
public function rating1()
{
$users = "ram";
return view('mech.class')->with('users', $users);
}
}
views/mech/class.blade.php
{{$users}}
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\mechplace;
// Route::get('mech.mechanical-engineering-placement', 'mechplacement#rating1');
// Route::get('skills-development',[App\Http\Controllers\deptPlacements::class,'rating1']);
// Route::resource('skills-development', deptPlacements::class);
// Route::get('mech.mechanical-engineering-placement',App\Http\Controllers\deptPlacements#rating1');
Route::get('mech.class-dept', 'App\Http\Controllers\mechplace#rating1');
But I am getting error on /mech/class page in my website
Undefined variable $users
How to display the MysSQL database content to sub folders in under views folders ?
It's with pages under views folder but not working for sub folders in views in Laravel 9
I have taken out index and .htaccess from public folder and website is working from laravel main directory for eg . localhost/name/public to localhost/name/
I have given routing as mech/class-dept.php in web.php :
Route::get('mech/class-dept.php',function(){
return view('mech/class-dept');})->name('class');
URL will be localhost/mech/class-dept.php

First thing first , see your route, I think you call a wrong controller.
before
Route::get('mech.class', 'App\Http\Controllers\deptPlacements#rating1');
after
Route::get('mech.class', [App\Http\Controllers\ mechplacements::class, 'rating1');
And than inside your controller.
public function rating1()
{
// you can put what ever query here
$users = User::where('status', true)->get();
return view('mech.class')->with('users', $users);
}
note you can also use compact()
return view('mech.class', compact('users'));
And inside your views/mech/class.blade.php
#foreach ($users as $user)
<p>{{ $user->name }}</p>
#endforeach
You can do display how you want.
Some notes:
First related to Controller naming convention, best to follow this standard = https://webdevetc.com/blog/laravel-naming-conventions/#section_naming-controllers
So you might consider to write your controller file name to MechPlacementController
And call it like this inside routes/web.php = App\Http\Controllers\ MechPlacementController::class
Another thing to point out
You have to differentiate between route path and route name.
Do you really mean to this page is accessible from http://localhost:8000/mech.class ?
Probably better if you use "-" sign or "/" , for example
Route::get('mech/class', 'App\Http\Controllers\deptPlacements#rating1')->name('mech.class');
Than you can access this path in http://localhost:8000/mech/class.
And if you want to use some where inside blade , you can use route('mech.class') which will generate route path /mech/class.

Related

Laravel Action Route not Define

I have unique problem. I already create Route on web.php, on Controller, and Blade. and working on my localhost. but after publish to real server, I jus got error Action url\controller#function not define. this is my code.
Route::get('/timetableschedule', 'ManagementController#EmployeeTimeTable');
this is my web.php
public function EmployeeTimeTable(Request $request){
$session = $request->session()->get('user.id');
$companysession = $request->session()->get('user.location');
$locationdata = DB::table('company')
->join('companyrole','companyrole.company_id','=','company.Company_id')
->join('users','users.id','=','companyrole.user_id')
->where('users.id',$session)
->get();
$userlist = db::table('users')
->join('companyrole','users.id','companyrole.user_id')
->where('companyrole.company_id',$companysession)
->whereNotNull('users.NPK')
->select('users.id','users.name','users.NPK')
->orderby('users.name')
->get();
$timesetting = db::table('time_leaving')
->where('company_id',$companysession)
->get();
$leaveschedule = db::table('employee_leaving')
->join('users','employee_leaving.user_id','users.id')
->where('employee_leaving.company_id',$companysession)
->select('employee_leaving.leaving_date','users.name')
->get();
return view('Management.employeetimetable',['location'=>$locationdata,'UserList'=>$userlist,'ListTimeSet'=>$timesetting,'LeavingSchedule'=>$leaveschedule]);
}
this is my controller
<li>
<a href="{{action('ManagementController#EmployeeTimeTable')}}" class="dropdown-item">
<p>Employee Timetable</p>
</a>
</li>
and this is code on blade to call controller#function
this step same as another controller, web, and blade.php. but, just for this rout get me a trouble. thank you
You have a problem with your route cache, simply do this:
php artisan cache:clear
php artisan route:cache
if you want to do same on the server you can define a route in web.php for that like this:
Route::get('/clear/route', 'ConfigController#clearRoute');
and make ConfigController.php like this
class ConfigController extends Controller
{
public function clearRoute()
{
\Artisan::call('route:clear');
}
}
and then go to that route on server example http://your-domain/clear/route
If your controller is on a subfolder, and not in app\Http\Controllers you will need to provide the fully qualified class name of the controller to the action helper :
action('App\Http\Controllers\Admin\ManagementController#EmployeeTimeTable')
by default action helper will look for : App\Http\Controllers\ManagementController
note that this syntax will not work as you might expect :
action('Admin\ManagementController#EmployeeTimeTable')
because laravel will not add App\Http\Controllers if there is a \ in the action name parameter

How to Create Dynamic Menu bar in Laravel using Database

initially, I created the static navigation bar but now I want to improve navigation bar as Dynamically.
in there im try function inside the Controller but it was not success because that view manage only one route.i need to that view in every pages of my web.
SideMenuController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\admins;
use DB;
class SideMenuController extends Controller {
public function index(){
$details = DB::SELECT("SELECT Name FROM admins");
return view('layout', compact('details'));
}
}
web.php:
Route::get('/user/test','SideMenuController#index');
test.blade.php :
Order
#foreach($details as $value)
{{ $value->Name }}
#endforeach
Error Exception
(3/3) ErrorException Undefined variable: details (View:
C:\xampp\htdocs\ERP\ERP_LAR\resources\views\test.blade.php) (View:
C:\xampp\htdocs\ERP\ERP_LAR\resources\views\test.blade.php)
For first take a look at the blade engine that is in Laravel.
The idea is that you will create a value in an controller and past this to the view. Here you will extract the value and place the right value to the right place. With blade you can even make a separate navigation.php and yield them in every view you need it.
There are many ways to solve the problem.
You can make a separate php file in your project like "navbar.php" and include it in by <?php include('navbar.php'); ?>
you can also use classes but it is harder.

How do I use a controller for a "partial" view in Laravel?

Here is my situation. I have a layout.blade.php which most of my pages use. Within this file, I have some partial pieces that I include, like #include('partials.header'). I am trying to use a controller to send data to my header.blade.php file, but I'm confused as to exactly how this will work since it is included in every view that extends layout.blade.php.
What I am trying to do is retrieve a record in my database of any Game that has a date of today's date, if it exists, and display the details using blade within the header.
How can I make this work?
I think to define those Game as globally shared is way to go.
In your AppServiceProvider boot method
public function boot()
{
view()->composer('partials.header', function ($view) {
view()->share('todayGames', \App\Game::whereDay('created_at', date('d')->get());
});
// or event view()->composer('*', Closure) to share $todayGames accross whole blade
}
Render your blade as usual, partial.header blade
#foreach ($todayGames as $game)
// dostuffs
#endforeach
In Laravel you can create a service class method that acts like a controller and use #inject directive to access this in your partial view. This means you do not need to create global variables in boot(), or pass variables into every controller, or pass through the base view layout.blade.php.
resources/views/header.blade.php:
#inject('gamesToday', 'App\Services\GamesTodayService')
#foreach ($gamesToday->getTodayGames() as $game)
// display game details
#endforeach
While it's different value you retrieved belong of the game chosen, you can do something like that:
Controller
$data = Game::select('id', 'name', 'published_date')->first();
return view('game')->with(compact('data'));
layout.blade.php
<html><head></head><body>
{{ $date }}
</body></html>
game.blade.php
#extend('layout')
#section('date', $data->date)
#section('content')
#endsection
The better solution would be this
Under your app folder make a class named yourClassNameFacade. Your class would look like this.
class yourClassNameFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'keyNameYouDecide';
}
}
Then go to the file app/Providers/AppServiceProvider.php and add to the register function
public function register()
{
$this->app->bind('keyNameYouDecide', function (){
//below your logic, in my case a call to the eloquent database model to retrieve all items.
//but you can return whatever you want and its available in your whole application.
return \App\MyEloquentClassName::all();
});
}
Then in your view or any other place you want it in your application you do this to reference it.
view is the following code:
{{ resolve('keyNameYouDecide') }}
if you want to check what is in it do this:
{{ ddd(resolve('keyNameYouDecide')) }}
anywhere else in your code you can just do:
resolve('keyNameYouDecide'))

Change laravel welcome file name

I changed the files path after i installed laravel framework like this:
from:
resources/views/welcome.bandle.php
to
resources/views/admin/index.php
and the routes file to:
Route::get('/admin', function () {
return view('admin/index');
});
the url is working
but all the larvael render not working
like this:
what i need to do?
tnx a lot.
You need to add the .blade.php extension to the files you want to parse using "Blade Engine", that will remove all the tags you have within curly braces.
Next, you need to write your route like this:
Route::get('/admin', function(){
return view('admin.index');
})->name('admin.index')->middleware('auth');
It is a good convention naming the routes for easy access across the application, that way you can simply reference it in the blade views like this:
Admin page
That way you will have the dynamic route no matter from where in the file structure you call it.
Or you can also use your Controller to display such view. By this you'll write your routes more cleaner. Let's say we have an AdminController that handles all admin processes and functions. Put your dashboard.blade.php inside views/admin directory.
The route:
Route::get('/admin', 'AdminController#index');
The controller:
class AdminController extends Controller
{
public function index()
{
return view('admin.dashboard'); // in views->admin->dashboard.blade.php
//add some data here
}
}
Just keep 'blade' in view file name if you don't plan to use controller, e.g.:
resources/views/admin/index.blade.php

laravel - home route

I'm learning Laravel, and for my first project I'd like to create my portfolio. However, the first task I have to do is confusing me.
So I created my templates, layout.blade.php and home.blade.php. That makes sense to me, but now how do I tell Laravel, or how do I route to home.blade.php?
I'm looking for an explanation rather then just code. I'm trying to learn.
Actually, a view in MVC application is just a part of the application and it's only for presentation logic, the UI and one doesn't call/load a view directly without the help of another part (controller/function) of the application. Basically, you make a request to a route and that route passes the control over to a controller/function and from there you show/load the view. So it's not a tutorial site and it's also not possible to explain about MVC here, you should read about it and for Laravel, it's best place to understand the basics on it's documentation, well explained with examples, anyways.
In case of Laravel, you should create a controller/class or an anonymous function in your apps/routes.php file and show the view from one of those. Just follow the given instruction step by step.
Using a Class:
To create a route to your Home Controller you should add this code in your app/routes.php
// This will call "showWelcome" method in your "HomeController" class
Route::any('/', array( 'as' => 'home', 'uses' => 'HomeController#showWelcome' ));
Then create the HomeController controller/class (create a file in your controllers folder and save this file using HomeController.php as it's name) then paste the code given below
class HomeController extends BaseController {
public function showWelcome()
{
// whatever you do, do it here
// prepare some data to use in the view (optional)
$data['page_title'] = 'Home Page';
// finally load the view
return View::make('home', $data);
}
}
If you have {{ $title }} in your home.blade.php then it'll print Home Page. So, to use a view you need a controller or an anonymous function and load the view from the controller/function.
Using an anonymous function:
Also, you can use an anonymous function instead of a controller/class to show the view from directly your route, i.e.
Route::any('/', function(){
// return View::make('home');
// or this
$data['page_title'] = 'Home Page'; // (optional)
return View::make('home', $data);
});
Using this approach, whenever you make a request to the home page, Laravel will call the anonymous function given in/as route's callback and from there you show your view.
Make sure to extend the the master/main layout in sub view (home):
Also, remember that, you have following at the first line of your home.blade.php file
#extends('layouts.layout')
It looks confusing, you may rename the main layout (layout.blade.php) to master.blade.php and use following in your home.blade.php instead
#extends('layouts.master')
Read the doc/understand basics:
You should read Laravel's documentation properly, (check templates to understand blade templating) and also read some MVC examples, that may help you too understand the basics of an MVC framework (you may find more by googling) and some good posts about MVC on SO.
Check it routing in Laravel.
You need to use route file and controllers
Create needed function in your Controller file and create a template file for example
class UserController extends BaseController {
/**
* Show the profile for the given user.
*/
public function showProfile($id)
{
$user = User::find($id);
return View::make('user.profile', array('user' => $user));
}
}
you need to create view file views/user/profile.blade.php
View::make('user.profile', array('user' => $user)) == views/user/profile.blade.php
And you should read it http://laravel.com/docs/responses and also this http://laravel.com/docs/quick#creating-a-view

Categories