discounts

// model <?php namespace App; use Illuminate\Database\Eloquent\Model; use Carbon\Carbon; class Discount extends Model { protected $table = 'discounts'; protected $dates = ['valid_from', 'valid_to']; protected $fillable = ['valid_from', 'valid_to', 'amount', 'product_id',]; public function products(){ return $this->belongsTo(Product::class, 'product_id', 'id'); } public function scopeValid($query){ return $query->where('valid_from', '>=', Carbon::now()->toDateTimeString()) ->where('valid_to', '<', Carbon::now()->toDateTimeString()); } } /// Blade <div class="well"> Current time: <strong>{{$mytime->format('d-m-Y h:i a')}}</strong> <hr> @foreach($product->discounts as $tag) Start Discount: <strong>{{ $tag->valid_from->format('d-m-Y h:i a') }}</strong><br> End Discount: <strong>{{ $tag->valid_to->format('d-m-Y h:i a') }}</strong><br> @endforeach <hr> <strong>Discount Status</strong><br> @if ($product->discount >= $mytime) OK! @else Expired! @endif </div> // Controller public function show($slug) { $product = Product::where('slug', $slug)->firstOrFail(); $arrivaltime = Carbon::now()->addWeekdays(4); //$discounts = Discount::where('product_id', '=', $product->id)->get(); //FIX $discounts = $product->discounts()->valid(); $mytime = Carbon::now(); return view('admin.products.show', compact('arrivaltime', 'product', 'mytime', 'discounts')); }THIS

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.