dongzhong7443 2013-03-21 10:25
浏览 15

从视图中获取价值

I have two table User & Article the relationship between tables are

Model:

class Article extends Eloquent {

  public static $table = 'article';

  public function User()
    {
    return $this->has_one('user', 'id');
    }

and

class User extends Eloquent {

  public static $table = 'user';

  public function Article()
    {
       return $this->belongs_to('article', 'id_user');
    }

I want to get name value from User directly on Article view but don't works with error Trying to get property of non-object

My Controller:

public function action_index()
    {

    $Article = Article::order_by('id')->paginate(10);

    return View::make('article.index')->with('$articles', $Article);
    }

My View:

@foreach ($articles->results as $Arti)
      <tr>
       <td>{{$Arti->id}}</td>
       <td>{{$Arti->tag}}</td>
       <td>{{$Arti->user->name }}</td>  <------ ERROR
       <td>{{$Arti->content}}</td>
       <td>{{$Arti->date}}</td>
       <td>
  • 写回答

1条回答 默认 最新

  • doulan3436 2013-04-12 14:12
    关注

    Have a look at the below, a few things are different to yours...

    1. Article belongs_to User (not has_one)
    2. User has_many Article (not belongs_to)
    3. Your relationships should be named in lowercase, plural for has_many (i.e. articles or user)
    4. Your relationship subjects should be class names (i.e. Article or User)
    5. Foreign keys should be name relationship_id, i.e. user_id
    6. Add ::with() to your query to eager load relationships
    7. When you paginate you need to access ->results in your view

    class Article extends Eloquent {
    
        // 3: lowercase 'user'
        public function user()
        {
            // 1: Article belongs to User
            // 4: class name 'User'
            // 5: Foreign key on article table is user_id
            return $this->belongs_to('User');
        }
    
    }
    
    // models/user.php
    
    class User extends Eloquent {
    
        // 3: lowercase plural 'articles'
        public function articles()
        {
            // 2: User has many Articles
            // 4: class name 'Article'
            return $this->has_many('Article');
        }
    
    }
    
    // controllers/articles.php
    
    class Article_Controller extends Base_Controller {
    
        public $restful = true;
    
        public function get_index()
        {
            // 6: Eager load the user relationship, ::with('user')
            $articles = Article::with('user')->order_by('id')->paginate(10);
            return View::make('articles.index', compact('articles'));
        }
    
    }
    
    // views/articles/index.blade.php
    // 7: access $articles->results from the paginator
    
    @foreach ($articles->results as $article)
    
        <h1>{{ $article->title }}</h1>
        <p>By {{ $article->user->name }}</p>
    
    @endforeach
    
    {{ $articles->links() }}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大