I am trying to use the ajax function (load) to load pages in my laravel. Here is my html code
#section('content')
<nav class="" id="menu">
<ul class="nav nav-tabs" id="sub_menu">
<li class="active">Home</li>
<li>Create School</li>
<li>Create User</li>
</ul>
</nav> <div id="page_loader"></div>
#endsection
Here is my controller Code
Route::get('pages/user', ['uses' => 'businessController#getCreateUser', 'as' => 'business.pages.user']);
Route::get('pages/school', ['uses' => 'businessController#getCreateSchool', 'as' => 'business.pages.school']);
Route::get('pages/business', ['uses' => 'businessController#getBusiness', 'as' => 'business.pages.home']);
Here is my jquery code:
$(document).ready(function(){ var trigger = $('#menu #sub_menu li a'), content = $('#page_loader');
trigger.on('click', function(){
var $this = $(this), target = $this.attr('href');
content.load(target);
return false;
});
});
The error displaying on my page console is :
jquery-3.2.1.min.js:4 GET http://127.0.0.1:8000/pages/pages/home 404 (Not Found)
You're using relative references in your navigation links so if you're at http://127.0.0.1:8000/pages/ then any link like pages/home will direct you to http://127.0.0.1:8000/pages/pages/home
Use laravel's helpers instead:
#section('content')
<nav class="" id="menu">
<ul class="nav nav-tabs" id="sub_menu">
<li class="active">Home</li>
<li>Create School</li>
<li>Create User</li>
</ul>
</nav> <div id="page_loader"></div>
#endsection
This way Laravel takes care of things for you.
Related
I want to generate URL with two parameters. In my web.php I created a route:
Route::get('/pojedinacni-turnir/{godina}/kolo/{kolo}', [
'uses' => 'Frontend\PojedinacniTurnirController#show',
'as' => 'pojedinacni.turnir',
]);
where are my two parameters are godina and kolo.
In my Controller I create show function with two parameters: id from godina and id from kolo.
Here is code from my controller:
<?php
namespace App\Http\Controllers\Frontend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Sezona;
use App\TurnirPojedinacni;
class PojedinacniTurnirController extends Controller
{
public function show($id_sezona, $id_kolo)
{
$sezone = Sezona::orderBy('godina', 'desc')->get();
$sezona = Sezona::findOrFail($id_sezona);
$kola = TurnirPojedinacni::where('sezona_id', $id_sezona)->orderBy('id', 'asc')->get();
$kolo = TurnirPojedinacni::findOrFail($id_kolo);
return view('frontend.pojedinacni_turnir', compact('sezone', 'sezona', 'kola', 'kolo'))->render();
}
}
When I try to check my URL i get this message:
Missing required parameters for [Route: pojedinacni.turnir] [URI: pojedinacni-turnir/{godina}/kolo/{kolo}]. (View: C:\WebSites\TkPazin\TK_Pazin\resources\views\frontend\pojedinacni_turnir.blade.php)
I don't understand error because i try URL with two parameters. I created URL with id's that exist like this:
{{ route('pojedinacni.turnir', ['godina' => 1, 'kolo' => 1]) }}
and even then i get the same error message.
Update: added blade file
#extends('layouts.frontend')
#section('title', 'TK Pazin | Pojedinačni turnir')
#section('css')
<link href="/css/style_pojedinacni_turniri.css" rel="stylesheet"/>
#endsection
#section('content')
<!-- Page Content -->
<div class="container">
<!-- Page Heading -->
<h1 class="my-4" style="text-align:center; color: #ba3631;">Pojedinačni turniri {{ $sezona->godina }}</h1>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
#foreach($sezone as $sezona)
<li class="breadcrumb-item">{{ $sezona->godina }}</li>
#endforeach
</ol>
</nav>
<div class="row mb-5">
<div class="col-12 col-md-2 mb-5">
<div class="list-group">
#foreach($kola as $kolo)
<button type="button" class="list-group-item list-group-item-action">{{ $kolo->naziv }}</button>
#endforeach
</div>
</div>
</div>
</div>
<!-- /.container -->
#endsection
You blade file shows that you have only added 1 parameter
#foreach($sezone as $sezona)
<li class="breadcrumb-item">{{ $sezona->godina }}</li>
#endforeach
Add the additional parameter
{{ $sezona->godina }}
I wont to make direct page with anchor tag in blade template engine, but its not work.
_sidebar.blade.php
<li class="active treeview">
<a href="{{ url('/') }}">
<i class="fa fa-dashboard"></i> <span>Menu Utama</span>
</a>
</li>
<li class="treeview">
<a href="{{ url('data_jamaah') }}">
<i class="fa fa-users"></i>
<span>Data Jamaah</span>
</a>
routes.php
Route::get('/', 'PagesController#getIndex');
Route::get('data_jamaah', 'PagesController#getJamaah');
PagesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Inbox;
class PagesController extends Controller
{
public function getIndex() {
return view('pages.index');
}
public function getJamaah() {
return view('pages.data_jamaah');
}
}
The problem is When i click a nothing to happen, thanks for your help
Edit:
This is main.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
#include('partials._head')
#yield('stylesheets')
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
#include('partials._nav')
#include('partials._sidebar')
#yield('content')
<footer class="main-footer">
#include('partials._footer')
</footer>
</div>
#include('partials._javascript')
#yield('javascripts')
</body>
</html>
Your issue is coming from of how you are using adminLTE Sidebar.
When you specify treeview in your class, you will have a dropdown list. And the anchor will be deactivated.
So try something like that :
<li class="active">
<a href="{{ url('/') }}">
<i class="fa fa-dashboard"></i> <span>Menu Utama</span>
</a>
</li>
<li>
<a href="{{ url('data_jamaah') }}">
<i class="fa fa-users"></i>
<span>Data Jamaah</span>
</a>
</li>
I think you can try this hope this work for you:
Route::get('/', 'PagesController#getIndex')->name('home');
Route::get('data_jamaah', 'PagesController#getJamaah')->name('dataJamaah');
<li class="active treeview">
<a href="{{route('home')}}">
<i class="fa fa-dashboard"></i> <span>Menu Utama</span>
</a>
</li>
<li class="treeview">
<a href="{{route('dataJamaah')}}">
<i class="fa fa-users"></i>
<span>Data Jamaah</span>
</a>
Laravel has it's own helper function to create the routes, which you can see here:
https://laravel.com/docs/5.4/helpers#method-route
Edit:
You also need to have a name for your routes to work from the helper function.
On your routes.php:
Route::get('/', ['uses' => 'PagesController#getIndex', 'as' => 'homepage']);
Route::get('data_jamaah', ['uses' => 'PagesController#getJamaah', 'as' => 'data.jamaah']);
You need to change the href from url() to route().
Change
From:
<a href="{{ url('/') }}">
To:
<a href="{{ route('homepage') }}">
and for other route
From:
<a href="{{ url('data_jamaah') }}">
To:
<a href="{{ route('data.jamaah') }}">
And it will work perfectly fine.
If we specify the name the laravel routes automatically gets the route and renders for you.
I know this is probably something really stupid but I just can't seem to fix it.
I'm getting this error
NotFoundHttpException in Handler.php line 131: No query results for model [App\Modules\Menus\Models\Menu].
And can't seem to fix it. At the moment it shouldn't even be asking for the Menu model.
Here is my route.php
Route::get('/signup', [
'uses' => 'OpenController#signup',
'as' => 'signup'
]);
Here is my OpenController.php
<?php
namespace App\Modules\Open\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Modules\Menus\Models\Menu;
use App\Modules\Authors\Models\Story;
class OpenController extends Controller
{
public function index(){
$menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
$menu = Menu::where('id', 1)->orWhere('title', 'home')->firstOrFail();
return view('open::index', compact('menus_child', 'menu'));
}
public function content($id){
$menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
$menu = Menu::where('id', $id)->firstOrFail();
$layout = $menu->type;
$stories = Story::where('type', 'public')->get();
return view('open::public/'.$layout, compact('menus_child', 'menu', 'stories'));
}
public function signup(){
echo "sign up";
die();
}
}
Here is my signup.blade.php
#extends('templates::layouts.public')
#section('content')
<h1>signup blade</h1>
#stop
Here is my public.blade.php layout
<!DOCTYPE html>
<html>
<head>
<title>Website</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Cherry+Swash|Crafty+Girls|Homemade+Apple|Italianno|Parisienne|Ranga|Rochester" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
{!! Html::style('css/bootstrap.min.css') !!}
{!! Html::style('css/style.css') !!}
{!! Html::script('js/jquery-3.1.1.min.js') !!}
{!! Html::script('js/bootstrap.min.js') !!}
{!! Html::script('js/main.js') !!}
</head>
<body>
<div class="container">
<div id="main">
<div id="header">
<div id="logo">
<div class="row">
<div class="col-lg-6">
<h1>Website</h1>
</div>
<div class="col-lg-6">
<div class="slogan">
{!! Html::link('/signup', 'Signup') !!} / {!! Html::link('/login', 'Login') !!}
</div>
</div>
</div>
</div>
#include('menus::menu')
</div>
<div id="site_content">
<div id="content">
#yield('content')
</div>
</div>
</div>
</div>
</body>
</html>
And this is my menu.blade.php
<div id="menubar">
<ul class="nav navbar-nav" id="menu">
#foreach($menus_child as $grandfather)
#if($grandfather->menu_id)
<li>
#elseif($grandfather->title == 'Home')
<li class="parent {{ menu_active([$grandfather->id]) }}">
#elseif(count($grandfather->menusP()->where('menu_id', '>', 0)->get()))
<li class="dropdown {{ menu_active([$grandfather->id]) }}">
#else
<li class="parent {{ menu_active([$grandfather->id]) }}">
#endif
#if(count($grandfather->menusP()->where('menu_id', '>', 0)->get()))
{!! HTML::decode(HTML::link($grandfather->id, $grandfather->title.'<span class="caret"></span>', array('class' => 'dropdown-toggle')))!!}
#else
{!! HTML::link($grandfather->id, $grandfather->title) !!}
#endif
#if(count($grandfather->menusP))
<ul class="dropdown_menu">
#foreach($grandfather->menusP as $father)
#if($father->menu_id)
<li class="parent_child">
#else
<li>
#endif
{!! HTML::link($father->id, $father->title) !!}
#endforeach
</ul>
#endif
#endforeach
</ul>
</div>
I found out what caused the issue. It was how I had my routes placed. So all I did was put my signup route on top of my routes and that solved my problem
#Gus, #Isis
This post helped me solve a very similar (or possibly same) issue. In my particular case, I immediately realized it was simple user error - and this may help explain why this may happen...
Consider visiting the following URL given the subsequent routes:
GET app.com/team/add-favorite
[bad] Routes:
// Bad Routing Logic
Route::get('/team/{team}', 'TeamController#show');
Route::get('/team/add-favorite', 'UserTeamController#default');
Route::get('/team/add-favorite/{conference}', 'UserTeamController#index');
Obviously, the 2nd route ( /team/add-favorite ) will never be called because it's simply going to route to /team/{team} with the [team] parameter filled with a value of "add-favorite".
My solution was to also simply re-arrange the order of the routes, like so...
[better] Routes:
// Still not good practice, but works
Route::get('/team/add-favorite', 'UserTeamController#default');
Route::get('/team/add-favorite/{conference}', 'UserTeamController#index');
Route::get('/team/{team}', 'TeamController#show');
This is still definitely not best practice, but will at least allow each of the routes to resolve properly.
app.com/team/add-favorite will now match the first route, while something like app.com/team/17 will still fall to the 3rd listed route and be handed to the TeamController with an appropriate ID.
Again - I don't recommend routing this way with "shared" endpoints as it could definitely still cause problems. i.e - better hope there were never a team with and ID = 'add-favorite'. Highly improbable, but you get the point...
Hope this helps!
I'm new in Laravel and need to pass data to navbar in header.
This are my pages
Main:
<div class="container">
<header>
#include('user.includes.header')
</header>
<main>
#yield('content')
</main>
<footer>
#include('user.includes.footer')
</footer>
</div>
Header:
<div id="navigation">
<nav class="navbar navbar-default">
<ul class="nav navbar-nav navbar-left">
<li class="dropdown">
#foreach($categorias as $cat)
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" title="{{$cat->nome}}">
{{$cat->nome}}
<span class="caret"></span>
</a>
#endforeach
</li>
</ul>
</nav>
</div>
IncludesController:
class IncludesController extends Controller
{
public function Header(){
$categorias = Categoria::all();
return compact('categorias');
}
}
}
I want to pass data form Controller to header.
I tried this route:
Route::get('header', 'IncludesController#Header');
but doesn't work and with this route:
Route::get('/', 'IncludesController#Header');
only shows the data and o want to all HTML page.
In IncludesController u need to return :
class IncludesController extends Controller
{
public function Header(){
$categorias = Categoria::all();
return view('main',compact('categorias');
}
}
}
I hope your main.blade.php file is directly under resources/views folder.
This is untested but it might probably help...
You should be returning your content view from your controller, the data to pass to that view is the second parameter.
return view('yourContentView', compact('categorias'));
As long as your content view #extends('main'), any data passed to your content view would also be made available to both your header and footer views.
Another solution would be to setup a view composer for both your header and footer views. This will fire each time those views are loaded and automatically inject data into that view...
You can read documentation on the view composers at https://laravel.com/docs/5.2/views#view-composers
I've this piece of markup. This piece has been loaded through ajax and appended inside a div.
Content of file user-bar.php:
<div id="u-bar">
<ul id="u-nav" class="nav">
<li id="notifications-list" class="dropdown" data-time="" >
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="icon-comment"></i>
Notifications
<b class="caret"></b>
</a>
<ul class="dropdown-menu" >
<li>Notification One</li>
<li>Notification two</li>
<li class="divider"></li>
<li>Show All Notifications</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="icon-user"></i>
Profile
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li>View Profile</li>
<li>Settings</li>
</ul>
</li>
<li>Logout</li>
<div class="clear"></div>
</ul>
</div>
I've scripts.js file as below which is included in the index.php file as <script type="text/javascript" src="js/scripts.js"></script>
$(function(){
loadUserBar();
$('#notifications-list').live('click', function(){
console.log('Test');
$('#notifications-list .icon-comment').removeClass('new');
});
});
function loadUserBar(){
$('#user-area').load('php/user-bar.php', function(){
initBootstrapElems(); //Initializing All popover elements
});
}
index.php file has the div#user-area where the ajax returned markup is inserted.
After the whole page has been loaded, when I click on the list-item #notifications-list, nothing happens. But when I typed in $('#notifications-list').click() directly in the console, the log does appears and the removeClass does occur.
What could be wrong here?
You're right, there's a conflict with dropdown bootstrap (simulated at http://jsbin.com/ijiqec/2/edit).
For some reason (don't ask me why) changing the use of live by on fixed the issue.
Use this piece of code:
$('#notifications-list').on('click',...
Try to put the handler for click on the a tag event.
$('#notifications-list a').live('click', function(){ ... })
Update:
You can't use the same id for all li tags.
You need to change the li.#notifications-list to a class if you have that on each tag and then update the js:
$('.notifications-list a').live('click', function(){ ... })