【发布时间】:2017-03-31 03:17:44
【问题描述】:
我正在尝试将标记功能添加到我的网络应用程序中,但它似乎只工作了一半。我可以标记一个特定的资源,添加我标记的原因以及其他 cmets 就好了。一旦我查看了所有标记的资源,原因和 cmets 就会保存并更正,但我标记的资源都是相同的,在本例中为“救世军”。例如,我有 4 个被标记的资源,它们都显示了不同的被标记原因,但即使我没有标记“救世军”,资源本身也是一样的。
这是我的一些代码,希望你能帮助我弄清楚为什么 Eloquent 没有更新我在数据库中的项目:
资源模型
class Resources extends Model
{
protected $table = 'Resources';
protected $primaryKey = 'Resource_ID';
public $timestamps = false;
protected $fillable = [
'Name',
'Description',
'Misc_Info',
'Flagged'
];
protected $guarded = [];
//referencing the pivot table for Flagged and Resources
public function flags()
{
return $this->belongsToMany('App\Models\Flagged', 'FlaggedResources', 'Resource_ID', 'Flag_ID');
}
class Flagged extends Model
{
protected $table = 'Flagged';
protected $primaryKey = 'Flag_ID';
public $timestamps = false;
protected $fillable = [
'Flag_Reason',
'Other_Comments'
];
protected $guarded = [];
//references the pivot table for Flagged and Resources
public function resource()
{
return $this->belongsToMany('App\Models\Resource', 'FlaggedResources');
}
}
class FlaggedResources extends Model
{
protected $table = 'FlaggedResources';
public $timestamps = false;
protected $fillable = [
'Resource_ID',
'Flag_ID'
];
protected $guarded = [];
}
public function index()
{
$resource = Resources::where('Flagged', 1)->with('flags')->get();
$flag = Flagged::all();
return view('pages.flags', ['flagged' => $resource], ['flag' => $flag]);
}
public function addFlag($id)
{
$resource = Resources::where('Resource_ID', '=', $id)->update(array('Flagged' => 1));
$flag = Flagged::create(Request::all());
$resource->save();
$flag->save();
return back();
}
Flagged=1 它会显示在视图中,如果Flagged=0 它不会显示。
标志视图
<table class=" display table table-hover table-bordered" , id="flaggedResources">
<thead>
<th>Name</th>
<th>Description</th>
<th>Flagged Reason</th>
<th>Other Comments</th>
<th>Functions</th>
</thead>
<tbody>
@foreach ($flagged as $flags) @foreach ($flag as $flaggedRes)
<tr>
<td>{{ $flags->Name }}</td>
<td>{{ $flags->Description }}</td>
<td>{{ $flaggedRes -> Flag_Reason }}</td>
<td>{{ $flaggedRes -> Other_Comments }}</td>
@endforeach @endforeach
</tbody>
</table>
当我检查 MySQLWorkbench 以检查是否进行了任何更改时,我的 Resources 表没有任何更改,但数据已输入到我的 Flagged 表中。之前尝试开发标记功能的开发人员将该 1 硬编码为“救世军”,这就是为什么只有那个在数据库中被标记的原因。此外,没有数据被插入到数据透视表中。
如果有人可以帮助我解决这个问题,我将不胜感激,我已经被这个问题困扰了大约一个月。
编辑 我已经添加了一些资源和标记 SQL 表的图片,希望它能帮助诊断问题
编辑 2
我知道为什么它没有更新 Resources 表,我有另一个名为 PostFlag 的方法,它只是插入 Flag_Reason 和 Other_Comments 而没有对 Resources 做任何事情,并且路由正在调用 @ 987654338@。现在我很困惑如何设置我的路线。在我编辑PostFlag 方法以更新Resource 表后,我现在拥有的路线是
Route::post('resource', ['as' => 'resource', 'uses'=>'FlagsController@postFlag']);
如果我不触摸路线,我会在加载标记资源视图时收到Missing argument 1 for App\Http\Controllers\FlagsController::postFlag() 错误。如果我编辑路线以获取像 Route::post('resource/{Resource_ID}', ['as' => 'resource', 'uses'=>'FlagsController@postFlag']); 这样的 ID,我会得到一个 Missing required parameters for [Route: resource] [URI: resource/{Resource_ID}])。我是否需要设置获取路线,如果需要,我该如何设置?
【问题讨论】:
标签: php sql laravel eloquent laravel-5.2