当我更新产品时,数据不会更新。
遵循代码
我的控制器
public function viewProduct( $name, $id )
{
$product = $this->queryProducts()
->where('produtos.id' , $id )
->first();
$product->count_view += 1;
$product->save();
}
我的型号
<?php
namespace App;
use Illuminate'Database'Eloquent'Model;
class Products extends Model
{
protected $guarded = ['id'];
protected $table = 'produtos';
const CREATED_AT = 'created';
const UPDATED_AT = 'modified';
protected $fillable = ['count_view', 'nome'];
如果我使用
dd( $product->save() )
我的返回为true,我尝试使用其他方法save()
$product->save( ['count_view' => 77 ] )
和/或
$product->update( ['count_view' => 77 ] )
但是不起作用
我发现了我的错误。。。
当我创建查询来搜索产品时,我与另一个表进行了连接,因此他没有更新。
为了解决这个问题,我说我只选择产品表,现在保存并更新作品!
private function queryProducts()
{
return Products::join('fornecedores', 'fornecedores.id', '=' ,'produtos.fornecedor_id')
->where( 'fornecedores.ativo', 1 )
->where( 'produtos.moderacao', 'P' )
->where( 'produtos.ativo' , 1 )
->select( 'produtos.*')
->limit( $this->_limitProducts );
}