如何将 MySQL 查询转换为 Eloquent 关系?
How to convert MySQL query to Eloquent Relationships?
我真的不知道要搜索哪些词或术语。我还阅读了 laravel 5.7、https://laravel.com/docs/5.7/eloquent-relationships#many-to-many-polymorphic-relations.
中的文档
但是我还是找不到我想要的东西。
我期待的结果是MySQL:
SELECT id as product_id, (SELECT name FROM products WHERE id = transactions.product_id), created_at FROM transactions WHERE user_id = 1
这是 mysql 查询的结果:
我已经有一个模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Transactions extends Model
{
protected $table = 'transactions';
protected $fillable = [
'user_id', 'product_id'
];
public function product()
{
return $this->hasOne('App\Products')->select('name');
}
}
?>
然后在我的控制器中:
public function transactions()
{
$transactions = new Transactions::where('user_id',Auth::id())->product;
return view('transactions', ['transactions' => $transactions]);
}
你应该使用 belongsTo()
关系。交易属于一个产品,产品有多个交易。
此外,您可以(或者最好应该)将模型重命名为单数。那么你不需要使用 protected $table
.
不一定要select只name
.
交易模式:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Transaction extends Model
{
protected $fillable = ['user_id', 'product_id'];
public function product()
{
return $this->belongsTo('App\Product');
}
}
控制器:
public function transactions()
{
$transactions = Transaction::with('product')
->where('user_id', Auth::id())
->get();
return view('transactions', ['transactions' => $transactions]);
}
查看:
@foreach($transactions as $transaction)
echo $transaction->product_id; //attribute product_id of transaction
echo $transaction->product->id; //attribute id of product
echo $transaction->product->name; //attribute name of product
@endforeach
我真的不知道要搜索哪些词或术语。我还阅读了 laravel 5.7、https://laravel.com/docs/5.7/eloquent-relationships#many-to-many-polymorphic-relations.
中的文档但是我还是找不到我想要的东西。
我期待的结果是MySQL:
SELECT id as product_id, (SELECT name FROM products WHERE id = transactions.product_id), created_at FROM transactions WHERE user_id = 1
这是 mysql 查询的结果:
我已经有一个模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Transactions extends Model
{
protected $table = 'transactions';
protected $fillable = [
'user_id', 'product_id'
];
public function product()
{
return $this->hasOne('App\Products')->select('name');
}
}
?>
然后在我的控制器中:
public function transactions()
{
$transactions = new Transactions::where('user_id',Auth::id())->product;
return view('transactions', ['transactions' => $transactions]);
}
你应该使用 belongsTo()
关系。交易属于一个产品,产品有多个交易。
此外,您可以(或者最好应该)将模型重命名为单数。那么你不需要使用 protected $table
.
不一定要select只name
.
交易模式:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Transaction extends Model
{
protected $fillable = ['user_id', 'product_id'];
public function product()
{
return $this->belongsTo('App\Product');
}
}
控制器:
public function transactions()
{
$transactions = Transaction::with('product')
->where('user_id', Auth::id())
->get();
return view('transactions', ['transactions' => $transactions]);
}
查看:
@foreach($transactions as $transaction)
echo $transaction->product_id; //attribute product_id of transaction
echo $transaction->product->id; //attribute id of product
echo $transaction->product->name; //attribute name of product
@endforeach