简体   繁体   English

两个表联接数据传递以在Laravel中查看

[英]Two tables join data passing to view in laravel

need to get data from status table in view. 需要从状态表中获取数据。 Tried to put alias, googled over 3 hours, I'm lost. 试图输入别名,用了3个小时的时间搜索了一下,我迷路了。 Any ideas how? 有什么想法吗?

Controler code: 控制器代码:

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

View code: 查看代码:

@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>

You should really implement models for your database tables. 您应该真正为数据库表实现模型。 While this isn't the advice you're asking for, it will greatly simplify your queries in this regard. 虽然这不是您要的建议,但是它将大大简化您在这方面的查询。

Your ActionPlan.php model: 您的ActionPlan.php模型:

use Illuminate\Database\Eloquent;

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

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

Your Status.php model: 您的Status.php模型:

use Illuminate\Database\Eloquent;

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

Your query: 您的查询:

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

Or: 要么:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM