Php 拉威尔秀刀片-试图获得财产'&引用';非对象
下午, 我正在尝试使用teams.show.blade显示联盟中的一支球队,但我一直收到一个错误“试图获取非对象的属性'displayName'。我尝试了每一种代码排列,试图毫无收获地显示数据。任何协助都将不胜感激 我希望URL是domain.com/league/{ID}/team/{abbrName} 路线为domain.com/league/1/team/MIA for Miami等 我已经对代码进行了排序,直到我想显示我的teams.index.blade中显示的单个团队 团队的一部分。显示。刀片Php 拉威尔秀刀片-试图获得财产'&引用';非对象,php,laravel,eloquent,Php,Laravel,Eloquent,下午, 我正在尝试使用teams.show.blade显示联盟中的一支球队,但我一直收到一个错误“试图获取非对象的属性'displayName'。我尝试了每一种代码排列,试图毫无收获地显示数据。任何协助都将不胜感激 我希望URL是domain.com/league/{ID}/team/{abbrName} 路线为domain.com/league/1/team/MIA for Miami等 我已经对代码进行了排序,直到我想显示我的teams.index.blade中显示的单个团队 团队的一部分。
@extends('layouts.home')
@section('content')
<div class="page-heading" style="background-image:url('/images/teams/banners/{{$team->displayName}}.jpg');">
<div class="container">
<div class="row">
<div class="col-md-10 offset-md-1">
<h1 class="page-heading__title">{{$team->cityName}} {{$team->displayName}}<span class="highlight"> Overview</span></h1>
<ol class="page-heading__breadcrumb breadcrumb">
<li class="breadcrumb-item"><a href="/">Home</a></li>
<li class="breadcrumb-item"><a href="/leagues">League</a></li>
<li class="breadcrumb-item"><a href="/leagues/{{$team->leagueId}}/teams">Teams</a></li>
<li class="breadcrumb-item active" aria-current="page" style="color:#ffffff;">{{$team->displayName}}
</li>
</ol>
</div>
</div>
</div>
</div>
TeamsController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Team;
use App\Models\League;
class TeamsController extends Controller
{
function index(League $league){
$teams = $league->team;
return view('teams.index', compact('league','teams'));
}
public function show($abbrName){
$team = Team::find($abbrName);
return view('teams.show')->with('team', $team);
}}
团队模式
namespace app\Models;
use Illuminate\Database\Eloquent\Model;
class Team extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'teaminfo';
public $primaryKey = 'teamId';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* The attributes that are not mass assignable.
*
* @var array
*/
protected $guarded = [];
/**
* The attributes that are hidden.
*
* @var array
*/
protected $hidden = [];
/**
* Fillable fields for a Profile.
*
* @var array
*/
protected $fillable = [
'teamId',
'abbrName',
'cityName',
'displayName',
'divName',
'offScheme',
'defScheme',
'ovrRating',
'injuryCount',
'primaryColor',
'secondaryColor',
'userName',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'teamId' => 'integer',
'abbrName' => 'string',
'cityName' => 'string',
'displayName' => 'string',
'divName' => 'string',
'offScheme' => 'string',
'defScheme' => 'string',
'ovrRating' => 'string',
'injuryCount' => 'string',
'primaryColor' => 'string',
'secondaryColor' => 'string',
'userName' => 'string',
];
/**
* Get the profiles for the league.
*/
public function profile()
{
return $this->hasMany('App\Models\Profile');
}
public function league()
{
return $this->belongsTo('App\Models\League');
}}
你必须改变这个
style="background-image:url('/images/teams/banners/{{$team['displayName']}}.jpg');"
使用
Team::find()
时,它使用模型的主键(teamId
)。这基本上可以转化为这个查询Team::where('id',$abbrName)->first()
,这显然会失败
在你的情况下,你应该使用
$team = Team::where('abbrName', $abbrName)->firstOrFail();
这可能很傻,但你有
return view('teams.index', compact('league','teams'));
在控制器中,然后在刀片服务器中使用team,而不使用foreach。也要修正你的打字错误
teams.show.blade:
style="background-image:url('/images/teams/banners/{{$team->displayName}}.jpg');">
找到了。
你的路线
Route::get('/league/{league}/team/{team}',['uses'=>'TeamsController@show', 'as'=>'teams.show']);
正在通过两个参数$league和$team
你的方法只是寻找一个
尝试:TeamsController@show
public function show($league, $team){
$team = Team::find($team);
return view('teams.show')->with('team', $team);
}}
答案和解决方案是卡梅伦的。谢谢大家的回复
public function show($league, $team){
$team = Team::find($team);
return view('teams.show')->with('team', $team);
}}
请先
dd($team)
然后检查它是否为空$abbrName
包含什么内容??是teamId
还是abbrName
?我要求提供变量值的数据库快照。从哪里发送到这条路线??变量包含什么?变量包含一个缩写ie:Miami Dolples=MIAThis。当我试图格式化代码以在stackoverflow上显示时,这是一个简单的复制粘贴错误,我已经纠正了这个错误。我写的代码确实有{{team->displayName}},我使用了{{$team['displayName']}},它显示了一个空白页面,返回视图team.index blade可以按照我的要求工作。它显示了与个人联赛相对应的32支球队。我只是无法获得球队。show blade要显示联盟球队,我尝试了提供的代码,但不幸失败,说明页面未找到错误。我使用了一位同事建议的find方法。你确定你的团队中有一个条目吗abbrName
其中isMIA
?是的,所有团队都有,但例如Miami有一个abbrName(MIA)、displayName(dolphines)、cityName(Miami)变量$abbrName
在调试时包含什么:dd($abbrName)
?它只显示与联盟ID相对应的****号****。即domain.com/League/*******号************/team/MIAThank感谢您的帮助,但不幸的是,此操作失败。在返回视图之前,您可以添加($team);吗?
public function show($league, $team){
$team = Team::find($team);
return view('teams.show')->with('team', $team);
}}