2016-07-12 1 views
0

보기에서 상태 테이블의 데이터를 가져와야합니다. 앨리어스를 넣어 봤는데 3 시간 넘게 봤는데 길을 잃었 어. 어떤 아이디어?두 테이블이 laravel에서 보려는 데이터 전달에 합류합니다.

CONTROLER 코드 :

$action= DB::table('actionplan') 
    ->join('status','actionplan.status','=','status.id') 
    ->select('status.name as testas','actionplan.*') 
    ->where([['user_id','=',$name],['done','<>',1]])->get(); 

보기 코드 :

@foreach($teams as $item) 
<th scope="row">1</th> 
<td>{{$item->name}}</td> 
<td>{{$item->start_date}}</td> 
<td>{{$item->end_date}}</td> 
<td>{{$item->comment}}</td> 
<td>{{$item->status()->name}}</td> 
+2

모델 간의 관계를 만들면 더 간단 해집니다. hasMany, hasOne, belongsTo 등 – Poria

+0

@Poria 여기에는 모델이 없습니다. – apokryfos

+0

코드에서 별칭을 정의하고 있지만 사용하고 있지는 않습니다. 그것은'{{$ item-> testas}}'' – apokryfos

답변

0

당신은 정말 데이터베이스 테이블에 대한 모델을 구현해야합니다. 이것은 당신이 요구하는 충고가 아니지만, 이와 관련하여 질의를 크게 단순화 할 것입니다.

귀하의 ActionPlan.php 모델 :
use Illuminate\Database\Eloquent; 

class ActionPlan extends Model 
{ 
    protected $table = 'actionplan'; 

    public function status() 
    { 
     return $this->hasOne(App\Status::class, 'id', 'status'); 
    } 
} 

귀하의 Status.php 모델 :

use Illuminate\Database\Eloquent; 

class Status extends Eloquent 
{ 
    protected $table = 'status'; 
} 

질문 :

$actions = Action::whereHas('status') 
    ->where('user_id', '=', $name) 
    ->where('done', '<>', 1) 
    ->get(); 

또는 :

$actions = Action::whereHas('status') 
    ->whereUserId($name) 
    ->having('done', '<>', 1) 
    ->get(); 
관련 문제