Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在laravel eloquent中从另一个表访问列_Laravel_Eloquent - Fatal编程技术网

在laravel eloquent中从另一个表访问列

在laravel eloquent中从另一个表访问列,laravel,eloquent,Laravel,Eloquent,在多对多关系中,我有两个表,分别是Page和Country。我必须根据路线中传递的slug和locale显示页面。区域设置字段位于国家/地区表中。如何在PageController中访问它 这是两个表的模式 CREATE TABLE `countries` ( `id` int(11) NOT NULL, `cname` varchar(255) NOT NULL, `lname` varchar(255) NOT NULL, `locale` varchar(255) NOT NULL, `s

在多对多关系中,我有两个表,分别是Page和Country。我必须根据路线中传递的slug和locale显示页面。区域设置字段位于国家/地区表中。如何在PageController中访问它

这是两个表的模式

CREATE TABLE `countries` ( `id` int(11) NOT NULL, `cname` varchar(255) NOT NULL, `lname` varchar(255) NOT NULL, `locale` varchar(255) NOT NULL, `status` tinyint(1) DEFAULT NULL, `created_at` datetime DEFAULT NULL, `updated_at` varchar(45) DEFAULT NULL, `slug` varchar(45) DEFAULT NULL, PRIMARY KEY (`id`) )

CREATE TABLE `pages` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `description` varchar(255) NOT NULL, `slug` varchar(255) DEFAULT NULL, `created_at` datetime DEFAULT NULL, `updated_at` datetime DEFAULT NULL, `status` tinyint(1) DEFAULT NULL, PRIMARY KEY (`id`) )
这是我正在使用的方法

public function findBySlugAndLocale($slug,$locale)
{
    return $this->model->where('slug', str_slug(trim($slug)))->where('countries',str_slug(trim($locale)))->first();
}
在上面的方法中,slug和locale来自route,所以这里是我的route

  Route::get('/{slug}/{locale}', ['as' => 'show','uses' => 'HomeController@findBySlugAndLocale']);
我想要一个类似这样的问题

select * from `pages` where `slug` = home and `countries` = en 
以下是我的模型:

class Country extends  Basemodel {

protected $table = "countries";

public function pages() {
    return $this->belongsToMany('App\Models\Page', 'country_page', 'country_id', 'page_id')->withTimestamps();
}
}


}

如果我正确理解了您的问题,并且您希望根据国家/地区表中的区域设置查询pages表,则您需要使用whereHas:


希望这有帮助

我用join解决了上面的问题。。。这里是查询。。。它可能会帮助有同样问题的人

public function findBySlugAndLocale($slug,$locale)
{
    $results = $this->model
        ->join('country_page', 'pages.id', '=', 'country_page.page_id')
        ->join('countries', 'countries.id', '=', 'country_page.country_id')
        ->where('pages.slug', '=', $slug)
        ->where('countries.locale', '=', $locale)
        ->first();
    return $results;
}

请粘贴一些代码here@AddWebSolutionPvtLtd请仔细查看是否有任何错误?SQLSTATE[42S22]:未找到列:1054“where子句”中的未知列“countries”SQL:select*from pages`where slug=home and countries=en limit 1`您的模型或表关系在哪里?获取页面:null@NehaSacher您的数据库中是否确实有满足查询的数据?@NehaSacher我的答案和您的答案之间的唯一区别是什么答案是,您的答案似乎并没有将值封装在stru slug和trim中。但结果应该完全相同。
return $this->model
    ->where('slug', str_slug(trim($slug)))
    ->whereHas('countries', function ($query) use ($locale) {
        $query->where('locale', str_slug(trim($locale)));
    })->first();
public function findBySlugAndLocale($slug,$locale)
{
    $results = $this->model
        ->join('country_page', 'pages.id', '=', 'country_page.page_id')
        ->join('countries', 'countries.id', '=', 'country_page.country_id')
        ->where('pages.slug', '=', $slug)
        ->where('countries.locale', '=', $locale)
        ->first();
    return $results;
}