我有一个应用程序,我正在尝试添加第二个模块。
基本上我正在克隆已经在使用我的数据库的现有模块。除了不同的表名和不同的控制器/类/型号名称之外,所有代码都与原始/工作模块完全相同。
我有3个与新模块相关的新表,就像旧模块investments
,investment_categories
和investment_vendors
一样。
每次我尝试使用我的新模块时,它都希望将供应商和类别名称值上传到investments
表,然后将其上传到相应的investment_categories
和investment_vendor
表。
我不相信问题出在我的新模块代码上,而是作曲家内的其他地方
我不希望我需要运行composer dump-autoload
,因为我总是编辑我的模块而不运行它(但这是我第一次更新一个查询/写入另一个表)。
我需要运行其他东西才能让它发挥作用吗?
我在日志中看到的以下错误是:
[2018-01-24 21:13:21] production.ERROR:PDOException:SQLSTATE [42S22]:找不到列:1054 / var / www // vendor /中的'字段列表'中的未知列'category_name' laravel /框架/ SRC /照亮/数据库/ Connection.php:390
控制器文件:
<?php
namespace MC\Modules\Investments\Controllers;
use MC\Http\Controllers\Controller;
use MC\Modules\Investments\Models\Investment;
use MC\Modules\Investments\Models\InvestmentCategory;
use MC\Modules\Investments\Models\InvestmentVendor;
class InvestmentController extends Controller
{
use ReturnUrl;
public function index()
{
$this->setReturnUrl();
$investments = Investment::defaultQuery()
->keywords(request('search'))
->categoryId(request('category'))
->vendorId(request('vendor'))
->sortable(['investment_date' => 'desc'])
->paginate(config('mc.defaultNumPerPage'));
return view('investments.index')
->with('investments', $investments)
->with('displaySearch', true)
->with('categories', ['' => trans('mc.all_categories')] + InvestmentCategory::getList())
->with('vendors', ['' => trans('mc.all_vendors')] + InvestmentVendor::getList())
}
创建控制器文件:
<?php
namespace MC\Modules\Investments\Controllers;
use MC\Http\Controllers\Controller;
use MC\Modules\Investments\Models\Investment;
class InvestmentCreateController extends Controller
{
public function create()
{
return view('investments.form')
->with('editMode', false)
->with('currentDate', DateFormatter::format(date('Y-m-d')))
}
public function store()
{
$record = request()->except('attachments');
$record['investment_date'] = DateFormatter::unformat($record['investment_date']);
$record['amount'] = NumberFormatter::unformat($record['amount']);
$record['tax'] = ($record['tax']) ? NumberFormatter::unformat($record['tax']) : 0;
$investment = Investment::create($record);
return redirect($this->getReturnUrl())
->with('alertSuccess', trans('mc.record_successfully_created'));
}
}
型号:
<?php
namespace MC\Modules\Investments\Models;
use Illuminate\Database\Eloquent\Model;
class Investment extends Model
{
use Sortable;
protected $table = 'investments';
protected $guarded = ['id'];
protected $sortable = ['investment_date', 'investment_categories.name', 'description', 'amount'];
public static function boot()
{
parent::boot();
}
/*
|--------------------------------------------------------------------------
| Relationships
|--------------------------------------------------------------------------
*/
public function attachments()
{
return $this->morphMany('MC\Modules\Attachments\Models\Attachment', 'attachable');
}
public function category()
{
return $this->belongsTo('MC\Modules\Investments\Models\InvestmentCategory');
}
public function vendor()
{
return $this->belongsTo('MC\Modules\Investments\Models\InvestmentVendor');
}
/*
|--------------------------------------------------------------------------
| Accessors
|--------------------------------------------------------------------------
*/
public function getAttachmentPathAttribute()
{
return attachment_path('investments/' . $this->id);
}
public function getFormattedAmountAttribute()
{
return CurrencyFormatter::format($this->amount);
}
public function getFormattedTaxAttribute()
{
return CurrencyFormatter::format($this->tax);
}
public function getFormattedInvestmentDateAttribute()
{
return DateFormatter::format($this->investment_date);
}
/*
|--------------------------------------------------------------------------
| Scopes
|--------------------------------------------------------------------------
*/
public function scopeCategoryId($query, $categoryId = null)
{
if ($categoryId)
{
$query->where('category_id', $categoryId);
}
return $query;
}
public function scopeDefaultQuery($query)
{
return $query->select('investments.*', 'investment_categories.name AS category_name',
'investment_vendors.name AS vendor_name', 'clients.unique_name AS client_name')
->join('investment_categories', 'investment_categories.id', '=', 'investments.category_id')
->leftJoin('investment_vendors', 'investment_vendors.id', '=', 'investments.vendor_id');
}
public function scopeVendorId($query, $vendorId = null)
{
if ($vendorId)
{
$query->where('vendor_id', $vendorId);
}
return $query;
}
}
需要注意的一点是,我确实在表格中有现有数据,并且从我的网页查看时查询工作正常,但问题是在尝试编辑或插入新数据/记录时。
从上面的模型中可以看出,
投资。*','investment_categories.name AS category_name
该查询因该命令而起作用
我找不到确定如何拍摄"investment_categories.name as category_name"
的其他任何地方
你能告诉我为什么它试图将category_name
值写入investments
表而不是investment_categories
表的name
列吗?