繁体   English   中英

Laravel:从两个表中选择,表2中的列=使用eloquent的值

[英]Laravel : Select from two tables where column in table two =value using eloquent

我有两个表table 1 = NewsCollection表2 = NewsConllectionTranslation

这是模特

NewsCollection

class NewsCollection extends \Eloquent
{
    use \Dimsav\Translatable\Translatable;

    public $translatedAttributes = ['title', 'content'];
    public $translationModel = 'NewsCollectionTranslation';

    public function newsTrans()
    {
        return $this->hasMany('NewsCollectionTranslation', 'news_collection_id');
    }
}

NewsConllectionTranslation

class NewsCollectionTranslation extends \Eloquent
{
    public $timestamps = false;
    protected $table = 'news_collection_translations';
    protected $fillable = ['title', 'content'];

    public function transNews()
    {
        return $this->belongsTo('NewsCollection', 'news_collection_id');
    }
}

这是节目控制器

public function show($title)
    {
        $news = NewsConllectionTranslation::with('newsTrans')->where('title', $title)->first();
        return View::make('portal.news.show', compact('news'));
    }

我需要做的是

->where('title', $title)->first();

应该从NewsConllectionTranslation中选择,我不想丢失翻译,所以我不想先从NewsConllectionTrnslation选择

像这样改变你的功能

 public function show($title)
    {
        $news = NewsConllectionTranslation::with(['newsTrans' => function ($query) use($title) {
                $query->where('title', $title)->first();
            }])


        return View::make('portal.news.show', compact('news'));
    }

你应该试试这个:

$news = NewsConllectionTranslation::whereHas('newsTrans', function ($query) use ($title) {
    $query->where('title', $title);
})->first();

暂无
暂无

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

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