我想通过使用laravel 5.5中的简单代码删除if else
语句来尽量减少这种情况。有人可以帮助我吗?在一个laravel中连接两个不同的数据库5.5
public function shirts($type='')
{
if($type == 'glass') {
$shirt = Product::where('category_id','1')->get();
$products = Category::find(1);
} elseif ($type == 'ic') {
$shirt = Product::where('category_id','2')->get();
$products = Category::find(2);
} elseif ($type == 'cover') {
$shirt = Product::where('category_id','3')->get();
$products = Category::findOrFail(3);
} else {
$shirt = Product::all();
}
return view('front.shirt',compact('products','shirt'));
}
http://php.net/ manual/en/control-structures.switch.php –
我宁愿不在这里处理它,只是发送这个函数的类别ID为你想要的数据..和其他地方类别与ID或具有一些功能,它会为你。 – RohitS
您需要在模型中设置连接。创建一个私有的$连接变量,然后设置一个方法来获取它。然后在if/else或switch中需要时调用这些方法。如果你还没有这样做,你将需要更新你的config/database.php作为第二个连接,然后将第二个连接添加到你的.env文件 – developernator