2014-07-14 165 views
0

我敢肯定我錯過了一些非常明顯的東西,但看不到分部名稱出現在我的視圖中。Laravel模型查看 - 未定義變量

它使返回未定義變量:division_name

DIVISON.PHP(型號)

<?php 

class Division extends \Eloquent { 

    protected $table = 'divisions'; 

    public function scopeGetDivisionName($query, $slug) 
    { 
     return $query->select('division_name') 
      ->join('division_industry_job', 'division_industry_job.division_id', '=', 'divisions.id') 
      ->join('industryjobs', 'division_industry_job.industry_job_id', '=', 'industryjobs.id') 
      ->where('industryjobs.slug', '=', $slug); 

    } 


} 

JOBCONTROLLER.PHP(控制器)

public function showdivisionjob($slug) 
    { 

    $job = IndustryJob::where("slug", "=", $slug)->first(); 

    $division_name = Division::getDivisionName($slug)->firstOrFail(); 

    return View::make('job')->with('job', $job, $division_name); 

    } 

JOB.BLADE.PHP(查看)

{{$division_name->division_name}} 

{{$job->job_title}} 

回答

2

Y你不能正確地將變量傳遞給你的視圖。

// This won't work 
return View::make('job')->with('job', $job, $division_name); 

應該

return View::make('job')->with('job', $job) 
         ->with('division_name', $division_name); 

或者

return View::make('job', array(
    'job' => $job, 
    'division_name' => $division_name 
)); 
+0

轟!而已。謝謝! – user3189734

1

您也可以使用小型方法傳遞變量到您的視圖:

public function showdivisionjob($slug) 
{ 
    $job = IndustryJob::where("slug", "=", $slug)->first(); 
    $division_name = Division::getDivisionName($slug)->firstOrFail(); 
    return View::make('job', compact('job', 'division_name')); 

}