雄辩:博客表格之间的关系

时间:2017-01-23 03:06:59

标签: laravel laravel-5.2 laravel-eloquent

有人会有一个在Eloquent中使用关系的实际例子如下:我有一个包含多个类别的博客,在这些类别中我将有几个帖子,就像我在显示一个包含多个Post in Views的类别一样。我学习逻辑的任何实际例子?我在这里见过几个,但没有一个符合我的要求。

模特职位:

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>

1 个答案:

答案 0 :(得分:0)

首先获取您要显示的类别

$category = Category::find(1);

然后跳过前3个

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

将它们传递给您的视图

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

在您看来,无论您想要什么,都可以按照您现在的方式使用刀片循环它们。

@foreach($posts as $post)

@endforeach