我有两个表,产品和子产品。 我还有一个 View ,其中显示有关产品的信息(查看从数据库动态生成的数据)。在此 View 中,应显示所有子产品。我怎样才能做到这一点?因为我在 View 中循环遍历第一个查询,而不是在 Controller 中。
Controller :
public function showSpecificProduct($name)
{
$name = strtolower($name);
$product = \DB::select('select * from products where name = ? LIMIT 1', [$name]);
$subProducts = \DB::select('select * from subproducts where mainproduct = ?', [/* id from $product belongs here */]);
return view('products.single', ['products' => $product, 'subproducts' => $subProducts]);
}
查看:
@foreach ($products as $product)
<div class="col-md-4">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ $product->name }}</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<img src="{{ $product->img }}" alt="{{ $product->name }}" class="img-responsive">
</div>
</div>
<!-- /.box -->
</div>
@endforeach
此外,在 laravel 中执行第一个查询是否有更好的方法?因为我在只有 1 行的情况下使用 foreach 循环。
提前致谢
最佳答案
Controller
public function showSpecificProduct($name)
{
$product = Product::with('subproducts')->where('name', $name)->first();
return view('products.single')->with('products',$product);
}
查看
{{ $product->name }} // shows the name of the mainproduct no need for a foreach
要显示您必须执行 foreach 循环的子产品(如果它们很多)
@foreach (($product->subproducts as $subproduct)
{{ $subproduct->name}}
@endforeach
要做到这一点,您必须先在模型和迁移中设置关系
喜欢这个
1-迁移
产品迁移
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->text('name');
$table->timestamps();
});
}
子产品迁移
public function up()
{
Schema::create('subproducts', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('subproducts');
$table->text('name');
$table->timestamps();
});
}
如果您没有正确的外键设置,请刷新您的迁移
2-模型
将此添加到您的产品模型
编辑
public function subproducts() {
return $this->hasMany('App\Subproduct', 'product_id'); // you can this
}
子产品模型
public function product() { // i made a mistake here
return $this->belongsTo('App\Product');
}
现在可以随意使用 Eloquent ORM。
关于php - Laravel: Controller 中有 2 个数据库查询,而 1 个值取决于第一个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38034347/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式rubyshell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr