关于Laravel中一些数据模型关系的快速查询.
我有一个属于用户的产品,但用户可以拥有许多产品.但是产品也可以让很多人对该产品提供报价;
class Product extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
public function offer(){
return $this->hasMany('App\Offer');
}
Run Code Online (Sandbox Code Playgroud)
但是产品也可以有不同的子类型的产品,例如,一个可能是车辆或手机,每个在我的迁移中都有不同的列,所以我假设我需要变形很多
在 Product.php
public function imageable()
{
return $this->morphTo();
}
Run Code Online (Sandbox Code Playgroud)
在 Vehicle.php
public function products()
{
return $this->morphMany('App\Product', 'imageable');
}
Run Code Online (Sandbox Code Playgroud)
但是,在致电时:
$product = Product::findOrFail($id);
Run Code Online (Sandbox Code Playgroud)
当我尝试做任何事情时,我得到null $product->vehicle
我将数据库中车辆的ID添加到imageable_id产品表中,并将产品表imageable中的车辆类型添加到"车辆".
我在这做错了什么?
我对Swift相对较新,我在使用swift原生URLRequest从API中提取一些JSON时遇到了麻烦 - 这是在操场上.我测试了API端点,它在Postman中返回数据就好了
这是我的代码:
//: Playground - noun: a place where people can play
import Foundation
import XCPlayground
import PlaygroundSupport
import UIKit
let urlString = "http://example.test/industry"
let session = URLSession.shared
let url = URL(string: urlString)!
var request = URLRequest(url: url)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
session.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) in
if let responseData = response
{
do{
/*let json = try JSONSerialization.jsonObject(with: responseData, options: [])
print(json)*/
}catch{
print("Could not serialize")
}
}
})
PlaygroundPage.current.needsIndefiniteExecution = true …Run Code Online (Sandbox Code Playgroud) 我已经按照 Laravel 文档中的说明进行分页,appends([])但是我在这些参数的持久性方面遇到了一些麻烦。
比如说,我传递home?category=Cars&make=Tesla给我的观点。用它们分页获取请求的最佳方法是什么?
现在我已经将类别作为参数传递给视图(其中类别是我用 抓取 findOrFail 的模型request('category');)
$category_name = $category_model->name;
Run Code Online (Sandbox Code Playgroud)
然后在我看来是这样的:
{{ $vehicles->appends(['category' => $category_name])->links() }}
Run Code Online (Sandbox Code Playgroud)
但是当我在分页中的页面之间切换时,这个 $category_name 值似乎不会持续存在。什么是实现我想要的推荐方法?
谢谢。
我在Laravel中收到以下降价邮件:
# Welcome to Offer Site
Thanks for listing your product, {{$user->name}}!
@component('mail::button', ['url' => 'https://www.example.com/product/view/{{$product->id}}', 'color'=> 'orange'])
View it here
@endcomponent
Run Code Online (Sandbox Code Playgroud)
但是,发送邮件时的呈现URL为 https://www.example.com/product/view/%3C?php%20echo%20e(%24product-%3Eid);%20?%3E
这可能非常简单,但是很难说...我该如何确保将变量作为参数正确插入URL栏中,这是在ProductAdded邮件的构建中:
return $this->markdown('emails.product-added-email');
Run Code Online (Sandbox Code Playgroud)
这就是我传递给ProductAddedMail的内容:
\Mail::to($user)->send(new \App\Mail\ProductAdded($user, $product));
变量工作正常。
有任何想法吗?