Laravel中的多级虚荣URL

时间:2014-06-12 12:03:04

标签: laravel-4 routing eloquent

我有一个系统,需要http://myurl.com/username并加载给定用户的个人资料页面。我还想添加另一个执行以下操作的层:

  1. 输入的网址为http://myurl.com/username/somevanity
  2. 应检查username
  3. 中是否存在users
  4. 如果是,请取id,并检查它是否存在于user_id表的homepage字段中。
  5. 如果存在,则应检查somevanity字段中是否存在vanity_url
  6. 如果是,则必须加载一个简单回显为feed_url
  7. 的页面

    重定向工作正常,home变量没有正确的数据。

    这是我的表结构:

    Columns:
    id int(10) UN AI PK 
    user_id int(11) 
    active varchar(1) 
    title varchar(60) 
    feed_url varchar(255) 
    created_at timestamp 
    updated_at timestamp 
    vanity varchar(50)
    

    这是我到目前为止所尝试的:

    Route::get('/{username}/{vanity}', function($username,$vanity)
        {
            $user = User::where('username', '=' , $username); //Check if user exists
    
        if($user->count()) 
        {
            $user = $user->first();
            $user = $user->id;
            $home = Homepage::where('user_id','=',$user)->where('vanity','=',$vanity)->get();
    
            if($home->count())
            {
                return View::make('homepage')->with('home',$home);
            }
            else
            {
                return Redirect::to('landing')->withErrors('Homepage not found!');
            }   
    
        }
        else
        {
            return Redirect::to('landing')->withErrors('Username not found!');
        }
        });
    

    但是,当我尝试从$home -

    转储homepage.blade.php
     @if (Session::has('home'))
            {{ $site =  Session::get('home') }}
     @endif
    
    
    <?php echo dd($site) ?> 
    

    我只得到以下信息,我假设是Eloquent的数据 -

       object(Illuminate\Database\Eloquent\Collection)[265]
      protected 'items' => 
        array (size=1)
          0 => 
            object(Blog)[263]
              protected 'fillable' => 
                array (size=2)
                  ...
              protected 'table' => string 'homepage' (length=8)
              protected 'connection' => null
              protected 'primaryKey' => string 'id' (length=2)
              protected 'perPage' => int 15
              public 'incrementing' => boolean true
              public 'timestamps' => boolean true
              protected 'attributes' => 
                array (size=8)
                  ...
              protected 'original' => 
                array (size=8)
                  ...
              protected 'relations' => 
                array (size=0)
                  ...
              protected 'hidden' => 
                array (size=0)
                  ...
              protected 'visible' => 
                array (size=0)
                  ...
              protected 'appends' => 
                array (size=0)
                  ...
              protected 'guarded' => 
                array (size=1)
                  ...
              protected 'dates' => 
                array (size=0)
                  ...
              protected 'touches' => 
                array (size=0)
                  ...
              protected 'observables' => 
                array (size=0)
                  ...
              protected 'with' => 
                array (size=0)
                  ...
              public 'exists' => boolean true
              protected 'softDelete' => boolean false
    

1 个答案:

答案 0 :(得分:1)

你必须在雄辩的数据库查询后调用->get()

$home = Homepage::where('user_id','=',$user)->where('vanity','=',$vanity)->get();
相关问题