从在 blade laravel 中具有一对多关系的另一个 table 访问数据
Accessing data from another table having one to many relationship in blade laravel
我有 Product 和 Book 的一对多关系。
我的控制器中的函数:
public function editProduct($p_id)
{
$books = Product::findOrFail($p_id);
return view('master.editProduct')->with(['books'=>$books]);
}
editProduct.blade.php
<!--Returns Name from product table-->
<div class="form-group">
<label>Book Title</label>
<input type="text" class="form-control" name="name" value = "{{ $books->name }}">
</div>
<!--Should return author from books table-->
<div class="form-group">
<label>Author</label>
<input type="text" class="form-control" name="author" " value = "{{ $books->books->author }}">
</div>
我也试过:
$books->books
为 p_id 返回了书籍 table 所有列的数据
$books->books->author
仅从书籍 table 中获取作者,但返回错误。我收到错误消息:属性 [作者] 在此 collection 实例上不存在。
在我看来,如何从书籍 table 中获取作者的价值?
产品型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $tables= "products";
protected $primaryKey = 'product_id';
public $incrementing = false;
protected $fillable = ['product_id','product_type','name', ...,];
protected $hidden =['created_at', 'updated_at'];
public function books(){
return $this->hasMany('App\Book', 'product_id');
}
}
图书模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
protected $tables= "books";
protected $fillable = ['product_id','author', 'edition',...];
protected $hidden =['created_at', 'updated_at'];
public function products(){
return $this->belongsTo('App\Product');
}
}
编辑表单字段
- 标题
- 作者
- 发表
.
.
.
标题存储在产品 table 中,我可以在我的表单中访问它
<div class="form-group">
<label>Book Title</label>
<input type="text" class="form-control" name="name" value = "{{ $books->name }}">
</div>
在同一个表单中作者未被访问。
两次查询解决方案:
$books = Product::find($p_id)->books()->get();
一个查询解决方案:
$books = Book::where('product_id', $p_id)->get();
显示数据:
@foreach ($books as $book)
{{ $book->name }}
@endforeach
Model: Product.php
public function books(){
return $this->hasMany('App\Book');
}
Model: book.php
public function product(){
return $this->belongsTo('App\Product');
}
In controller:
$books = Product::find($p_id)->books()->get();
In your view page
@foreach ($books as $book)
{{ $book->name }}
@endforeach
产品控制器
public function editProduct($p_id){
$products = Product::with(['books'])->where('product_id', $p_id)->get();
return view('master.editProduct', compact('products'));
}
查看
我在表单中使用了两个 foreach 循环来访问产品 table 中的名称和书籍 table 中的作者,如下所示:
@foreach($products as $product)
<form>
...
<div class="form-group">
<label>Book Title</label>
<input type="text" class="form-control" id="name" name="name" value = "{{ $product->name }}">
</div>
@foreach($product->books as $books)
<div class="form-group">
<label>Author</label>
<input type="text" class="form-control" id="author" name="author" " value = "{{ $books->author }}">
</div>
@endforeach
...
</form>
@endforeach
我有 Product 和 Book 的一对多关系。
我的控制器中的函数:
public function editProduct($p_id)
{
$books = Product::findOrFail($p_id);
return view('master.editProduct')->with(['books'=>$books]);
}
editProduct.blade.php
<!--Returns Name from product table-->
<div class="form-group">
<label>Book Title</label>
<input type="text" class="form-control" name="name" value = "{{ $books->name }}">
</div>
<!--Should return author from books table-->
<div class="form-group">
<label>Author</label>
<input type="text" class="form-control" name="author" " value = "{{ $books->books->author }}">
</div>
我也试过:
$books->books
为 p_id 返回了书籍 table 所有列的数据
$books->books->author
仅从书籍 table 中获取作者,但返回错误。我收到错误消息:属性 [作者] 在此 collection 实例上不存在。 在我看来,如何从书籍 table 中获取作者的价值?
产品型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $tables= "products";
protected $primaryKey = 'product_id';
public $incrementing = false;
protected $fillable = ['product_id','product_type','name', ...,];
protected $hidden =['created_at', 'updated_at'];
public function books(){
return $this->hasMany('App\Book', 'product_id');
}
}
图书模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
protected $tables= "books";
protected $fillable = ['product_id','author', 'edition',...];
protected $hidden =['created_at', 'updated_at'];
public function products(){
return $this->belongsTo('App\Product');
}
}
编辑表单字段
- 标题
- 作者
- 发表 . . .
标题存储在产品 table 中,我可以在我的表单中访问它
<div class="form-group">
<label>Book Title</label>
<input type="text" class="form-control" name="name" value = "{{ $books->name }}">
</div>
在同一个表单中作者未被访问。
两次查询解决方案:
$books = Product::find($p_id)->books()->get();
一个查询解决方案:
$books = Book::where('product_id', $p_id)->get();
显示数据:
@foreach ($books as $book)
{{ $book->name }}
@endforeach
Model: Product.php
public function books(){
return $this->hasMany('App\Book');
}
Model: book.php
public function product(){
return $this->belongsTo('App\Product');
}
In controller:
$books = Product::find($p_id)->books()->get();
In your view page
@foreach ($books as $book)
{{ $book->name }}
@endforeach
产品控制器
public function editProduct($p_id){
$products = Product::with(['books'])->where('product_id', $p_id)->get();
return view('master.editProduct', compact('products'));
}
查看 我在表单中使用了两个 foreach 循环来访问产品 table 中的名称和书籍 table 中的作者,如下所示:
@foreach($products as $product)
<form>
...
<div class="form-group">
<label>Book Title</label>
<input type="text" class="form-control" id="name" name="name" value = "{{ $product->name }}">
</div>
@foreach($product->books as $books)
<div class="form-group">
<label>Author</label>
<input type="text" class="form-control" id="author" name="author" " value = "{{ $books->author }}">
</div>
@endforeach
...
</form>
@endforeach