我有一个系统,需要http://myurl.com/username
并加载给定用户的个人资料页面。我还想添加另一个执行以下操作的层:
http://myurl.com/username/somevanity
。username
表users
id
,并检查它是否存在于user_id
表的homepage
字段中。 somevanity
字段中是否存在vanity_url
。feed_url
重定向工作正常,但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
答案 0 :(得分:1)
你必须在雄辩的数据库查询后调用->get()
:
$home = Homepage::where('user_id','=',$user)->where('vanity','=',$vanity)->get();