Вопрос

So, I'm trying to iterate through and check the post id in my likes lookup table to the id stored in the posts table. But I'm not getting any return at all. I just have absolutely no idea what is wrong at this point.

posts.blade.php

<div class="posts"> 
@foreach(Post::orderBy('created_at', 'DSC')->get() as $post)
    <div class="col-md-8" style="background-color: #fff; margin: 10px;">
        <center><h2> {{ $post->title }} </h2></center>
        <textarea class="form-control" readonly="true" style="cursor: text; width: 500px; padding: 10px;"> {{$post->body }}</textarea>

        <div>
            Posted by user: {{ $post->user->username }}
        </div>

        <div>
            Total Likes: {{ $post->likes()->count() }}
            @foreach(Like::where('post_id', '>', $post->post_id) as $l)
                <li> {{ $l->user()->username }} </li>
            @endforeach
        </div>
    </div>
@endforeach
</div>

Like.php Model

<?php

class Like extends Eloquent
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'likes';

    protected $fillable = array('user_id', 'post_id');

    public $timestamps = false;

    public function user()
    {
        return $this->belongsTo('User');
    }

    public function post()
    {
        return $this->belongsTo('Post');
    }
    }
?>

Post.php Model

 <?php

class Post extends Eloquent
{

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'posts';

    protected $fillable = array('user_id', 'title', 'body', 'type');

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array();

    public function user()
    {
        return $this->belongsTo('User');
    }

    public function likes()
    {
        return $this->hasMany('Like');
    }

}
Это было полезно?

Решение

Regarding your query, I think you have two mistakes:

  1. You need to append get() after your where($condition)
  2. You are querying for post ids greater than your post id. Shouldn't be that an equal sign?

It should be something like this:

$likes = Like::where('post_id', '=', $post->post_id)->get();

Then, as I pointed out in my comment, I'd recommend that you do all your model related stuff in a repository class, use that class in your controller, and pass the result to the view.

Your repository class:

class PostRepository {

    public function byDate()
    {
        // return posts by date
    }

}

Your controller:

class MyAwesomeController {

    protected $postRepository;

    ...

    public function awesomeSection()
    {
        $posts = $this->postRepository->byDate();

        return View::make('awesome.view')->with('posts' => $posts);
        // or
        return View::make('awesome.view', compact('posts'));
    }

}


Further reading:

Другие советы

avoid your business logic in your view. If your application is small, You can do it in your controller as Manuel said.

However, In your code a get() method is missing. Hope this will fix the problem.

        @foreach(Like::where('post_id', '>', $post->post_id)->get() as $l)
            <li> {{ $l->user()->username }} </li>
        @endforeach
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top