我正在使用laravel 5.3 我有一个表格可以帮助我创建新项目。我还可以显示我在DB中具有所有属性的所有项目。 现在,我需要在页面左侧显示包含以下内容的响应导航菜单:
Domaines: Domaine 1: 项目1 项目2 Domaine 2: 项目A. 项目B
我希望一旦我点击项目,就会显示该项目的所有信息。
这是“index.blade.php”当我只是试图显示没有任何ordre的项目时:
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Projects CRUD</h2>
</div>
<div class="pull-right">
@permission('pro-create')
<a class="btn btn-success" href="{{ route('pros.create') }}"> Create New Project</a>
@endpermission
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th>No</th>
<th>title</th>
<th>code</th>
<th>domain_id</th>
<th width="280px">Action</th>
</tr>
@foreach ($pros as $key => $pro)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $pro->title }}</td>
<td>{{ $pro->code }}</td>
<td>{{ $pro->domain_id}}</td>
<td>
<a class="btn btn-info" href="{{ route('pros.show',$pro->id) }}">Show</a>
@permission('pro-edit')
<a class="btn btn-primary" href="{{ route('pros.edit',$pro->id) }}">Edit</a>
@endpermission
@permission('pro-delete')
{!! Form::open(['method' => 'DELETE','route' => ['pros.destroy', $pro->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
@endpermission
</td>
</tr>
@endforeach
</table>
{!! $pros->render() !!}
@endsection
这就是我试图添加的“响应导航菜单”:
<div class="nav-side-menu">
<div class="brand">Menu</div>
<i class="fa fa-bars fa-2x toggle-btn" data-toggle="collapse" data-target="#menu-content"></i>
<div class="menu-list">
<ul id="menu-content" class="menu-content collapse out">
<li>
<a href="#">
<i class="fa fa-dashboard fa-lg"></i> Dashboard
</a>
</li>
<li data-toggle="collapse" data-target="#domains" class="collapsed active">
<a href="#"><i class="fa fa-gift fa-lg"></i> Domains <span class="arrow"></span></a>
</li>
<ul class="sub-menu collapse" id="domains">
<li class="active"><a href="#">Domain 1</a></li>
<li><a href="#">Domain 2</a></li>
<li><a href="#">Domain 3</a></li>
<li><a href="#">Domain 4</a></li>
<li><a href="#">Domain 5</a></li>
</ul>
<li data-toggle="collapse" data-target="#responsible" class="collapsed">
<a href="#"><i class="fa fa-globe fa-lg"></i> Responsibles <span class="arrow"></span></a>
</li>
<ul class="sub-menu collapse" id="responsible">
<li>Mr.name1</li>
<li>Mr.name2</li>
<li>Mr.name3</li>
</ul>
</div>
</div>
我的问题是如何在我的数据库和视图之间建立关系?如何从DB(项目表)中检索数据并在视图中显示它?
答案 0 :(得分:1)
您为表创建了一个模型&#34;示例&#34;
<强>模型强>
class Example extends Model {
protected $table = 'Example';
protected $fillable = ['column1', 'column2'];
protected $primaryKey = 'id';
}
请勿忘记在/config/database.php
现在你有了你的模型你可以创建一个控制器,我建议你resource controller。
使用App \ Model \ Example; class ExampleController扩展Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
// This is the one we need, because it will be shown on GET domain.com/example
$examples = Example::query()->get();
return view('exampleview', ['passed_data' => $examples]);
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}
定义您的路线。因为您要显示前端,所以选择routes/web.php
并定义
路由::资源(&#39;示例&#39;,&#39; ExampleController&#39;);
现在,您还需要将刀片设置为resources\views
exampleview.blade.php
(在控制器中定义)
您可以在刀片语法中使用传递的数据: 的 exampleview.blade.php 强> @foreach($ passed_data as $ single_example) {{$ example}} @endforeach
要根据您所在的视图更改菜单,请定义
@stack('menustack') inside your menu blade
并从&#34; lower&#34;推送新元素刀片
@push('menustack')
<div> new menu div </div>
@endpush
答案 1 :(得分:0)
您需要在app / Http目录中创建 nav.php ,例如:
应用程序/ HTTP / nav.php
<?php
//pass your view file path like *index*
View::composer('index', function($view){
//For navigation menu items...
$navigation = DB::table('navigation')
->select('Domain1', 'Domain2', 'Domain3', 'Domain4', 'Domain5')
->get();
return $view->with('navigations' => $navigations);
});
?>
并在routes / web中定义nav.php路由,如:
// For navigation menu route
**require 'nav.php';**
希望这适合你!