Skip to content

Instantly share code, notes, and snippets.

@hayata19791218
Created March 23, 2023 12:26
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class Cart extends Model
{
use HasFactory;
protected $fillable = [
'stock_id',
'user_id'
];
public function showCart()
{
$user_id = Auth::id();
$data['my_carts'] = $Cart->where('user_id',$user_id)->get();
return $data;
}
public function stock()
{
return $this->belongsTo('\App\Models\Stock');
}
public function addCart($stock_id)
{
$user_id = Auth::id();
$cart_add_info = Cart::firstOrCreate(['stock_id' => $stock_id,'user_id' => $user_id]);
if($cart_add_info->wasRecentlyCreated){
$message = 'カートに追加しました';
}else{
$message = 'カートに登録済みです';
}
return $message;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment