我是 laravel 的新手,我目前正在 laravel 5.2 中做一个研究项目。 Thr 项目只是一个简单的网站,有 3 个页面(主页、关于我们、联系我们
),从数据库中获取每个页面的内容,主页有幻灯片。为此,我下载了一个免费的响应式 html 模板并将其分为 header.blade.php
、slideshow.blade.php
、footer.blade.php
并将其存储在 view
文件夹下名为 includes
的文件夹中。主布局文件 default.blade.php
存储在 view
文件夹下名为 layouts
的文件夹中。我在主布局文件 default 中包含了这些
形成完整的网页模板。header.blade.php
、slideshow.blade.php
和 footer.blade.php
.blade.php
我的default.blade.php
文件代码如下:
<!DOCTYPE html>
<head>
@include('guest.includes.head')
</head>
<body>
<div class="wrap-body">
<header>
@include('guest.includes.header')
</header>
@if(Request::is('/'))
@include('guest.includes.slideshow')
@endif
@yield('content')
<footer class="zerogrid">
@include('guest.includes.footer')
</footer>
</div>
</body>
</html>
在 header.blade.php 中,我有一些信息,如电话号码、电子邮件、网站地址,这些信息是从数据库中获取的,并在我的所有 3 个页面上显示该数据以及来自 db 的每个页面内容。为此,我在 AppServiceProvider.php
的 boot
方法中使用了 share()
函数来在我的所有网页上共享和显示我的标题数据。
我当前的 slideshow.blade.php
代码如下所示:
<div class="zerogrid">
<div class="callbacks_container">
<ul class="rslides" id="slider4">
<li>
<img src="assets/images/banner1.jpg" alt="">
<div class="caption">
<h2>Title 1</h2></br>
<p>Description 1</p>
</div>
</li>
<li>
<img src="assets/images/banner2.jpg" alt="">
<div class="caption">
<h2>Title 2</h2></br>
<p>Description 2</p>
</div>
</li>
</ul>
</div>
</div>
在这里你可以看到我已经硬编码了上面的幻灯片项目详细信息(slideimage, title, description
)。到目前为止,这部分工作正常,我正在通过 header.blade.php
从 db 到服务器获取 header 数据到所有页面,每个页面内容通过 Routes/Controller 和上述幻灯片从 db 获取。
我的routes.php
代码如下:
Route::get('/', 'GuestController@home');
Route::get('aboutus', 'GuestController@aboutus');
Route::get('contactus', 'GuestController@contactus');
我的 Controller 页面(GuestController.php
)代码如下:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class GuestController extends Controller
{
public function home() {
$result=DB::table('contents')->where('menuname','home')->get();
return view('guest.home')->with('data',$result);
}
public function aboutus() {
$result=DB::table('contents')->where('menuname','aboutus')->get();
return view('guest.aboutus')->with('data',$result);
}
public function contactus() {
$result=DB::table('contents')->where('menuname','contactus')->get();
return view('guest.contactus')->with('data',$result);
}
}
现在我想让幻灯片也变得动态。我的意思是从数据库本身获取幻灯片项目的详细信息(幻灯片图像、标题、描述)。为此,我创建了一个名为 slideshow
的表,其中包含字段 slideimage
、title
、description
。但我不知道如何从 db 中的两个独立表(slideshow
和 contents
)获取数据并将其显示到单个网页。
(在我的例子中,实际上在我的主页中,它将从数据库中获取 3 组数据。第一组数据是来自 headers
表的标题数据,它将显示标题数据,如电话、电子邮件、网站等到我的所有页面,包括通过 share()
函数的主页,第二组数据是通过路由/ Controller 页面从内容表中获取的主页内容。这两个已经在工作,现在我想获取幻灯片的数据也来自 db 中的 slideshow
表,这是我要在主页上显示的第三组数据。)
这个需求有什么解决方案吗?请帮忙!提前致谢...
最佳答案
当我按照上面的问题为我的要求搜索各种解决方案时,我找到了一个简单的方法来满足从单个 View 中的两个表中获取和显示数据
的要求可以完成只需修改上面给出的 Controller 页面代码 (GuestController.php
) 中的 home()
方法,如下所示:
public function home() {
$homecontent=DB::table('contents')->where('menuname','home')->get();
$slideshow=DB::table('slideshow')->get();
return view('guest.home')->with('homecontent',$homecontent)->with('slideshow',$slideshow);
}
然后在 slideshow.blade.php
中循环 slideshow
变量的结果,例如:
<ul class="rslides" id="slider4">
@foreach($slideshow as $row)
<li>
<img src="assets/images/{{$row->slideimage}}" alt="">
<div class="caption">
<h2>{{$row->title}}</h2></br>
<p>{{$row->description}}</p>
</div>
</li>
@endforeach
</ul>
和幻灯片一样,在主页文件中循环播放 homecontent
变量的内容,如下所示:
@foreach($homecontent as $row)
<article>
<div class="art-header">
<a href="#"><h3>{{$row->contenttitle}}</h3></a>
</div>
<div class="art-content">
<img src="assets/images/{{$row->contentimage}}" />
<p align="justify">{{$row->contentdescription}}</p>
</div>
</article>
@endforeach
不需要对任何其他页面进行其他更改,它将在主页本身内显示幻灯片和主页内容,以及包括主页在内的所有页面上的 header
表中的共享标题数据。
关于php - 如何将 db 中两个表中的数据路由、获取和显示到 laravel 5.2 中的单个网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35560944/