Eloquent: 博客表之间的关系

Eloquent: relationships between blog tables

有人会在 Eloquent 中有一个使用关系的实际示例,如下所示:在视图中有多个 Post 的类别。有什么实际例子可以让我研究我的逻辑吗?我在这里看到了几个,但是 none 符合我上面想要的。

型号Post:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function category()
    {
        return $this->belongsTo('App\Category');
    }

    public function tags()
    {
        return $this->belongsToMany('App\Tag');

    }

    public function comments()
    {
        return $this->hasMany('App\Comment');
    }
}

控制器:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Post;
use Mail;
use Session;
use App\Category;

class PagesController extends Controller {

    public function getIndex() {

        $posts = category::find(1)->posts()->orderBy('created_at', 'desc');
        return view('v1.index')->withPosts($posts);
        // $posts = Post::orderBy('created_at', 'desc')->limit(3)->get();
        // $categorias = Category::find(1);
        // return view('v1.index')->withPosts($posts)->withCategorias($categorias);
    }


    public function getContact() {
        return view('v1.contato');
    }

    public function postContact(Request $request) {
        $this->validate($request, [
            'email' => 'required|email',
            'subject' => 'min:3',
            'message' => 'min:10']);

        $data = array(
            'email' => $request->email,
            'subject' => $request->subject,
            'bodyMessage' => $request->message
            );

        Mail::send('emails.contact', $data, function($message) use ($data){
            $message->from($data['email']);
            $message->to('hello@devmarketer.io');
            $message->subject($data['subject']);
        });

        Session::flash('success', 'Your Email was Sent!');

        return redirect('/');
    }


}

模特类别:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table = 'categories';

    public function posts()
    {
        return $this->hasMany('App\Post');
    }
}

查看索引

<div class="col-sm-6">
    <div id="home-slider">

        @foreach($posts as $post)
            <div class="post feature-post">
                <div class="entry-header">
                    <div class="entry-thumbnail">
                        <img class="img-responsive" src="{{ asset('imgs/'.$post->image) }}" width="572" height="350"  alt="" />
            <div class="catagory world"><a href="#">{{ $post->category->name }}</a></div>
                </div>
                <div class="post-content">
                    <h2 class="entry-title">
                        <a href="{{ route('posts.show', $post->id) }}">{{ $post->title }}</a>
                    </h2>
                </div>
            </div><!--/post-->
        @endforeach



    </div>
</div>

首先获取你要展示的分类

$category = Category::find(1);

然后通过跳过前 3

获取与该类别相关的所有 post
$posts = Post::where('category_id', $category->id)
->skip(3)
->take(6)
->get();

将它们传递给您的视图

return view('v1.index', compact('posts'));

在您看来,无论您想要什么,都可以按照您已经在做的方式 blade 循环播放它们。

@foreach($posts as $post)

@endforeach