public function postClicks( $id){
// This works increments
$click = Product::FindorFail($id);
$click->increment('clicks');
$click->update();
// this doesnt work
$clicksave = new Click;
$clicksave = $click;
$clicksave->customer_id = Auth::guard('customer')->user()->id;
$clicksave->save();
// this is from the product table and clicks section
}
AJAX
<script>
$('.cardbutton-page').click(function(e){
e.preventDefault;
var product = $(this).closest('#cardproduct');
var product_id = product.attr('data-product-id');
registerProductClicked(product_id);
});
function registerProductClicked(product_id){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: 'post',
dataType: 'JSON',
url: '/product/'+ product_id + '/click',
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(JSON.stringify(xhr.responseText));
}
});
}
</script>
Route::post('/product/{id}/click', 'ClicksController@postClicks');
答案 0 :(得分:2)
请确保在您的Click
模型上将customer_id
设置为可填充。
即
Click extends Model
{
protected $fillable = ['customer_id'];
}
并尝试Click::create(['customer_id' => Auth::guard('customer')->user()->id]);