web.php
Route::post('/addfavorite/{id}', [App\Http\Controllers\PropertyController::class, 'addFavorite']);
properties.blade.php file
<!-- Gallery -->
<section id="property" class="padding bg_gallery">
<div class="container">
<div id="property-gallery" class="cbp listing1">
@foreach($properties as $property)
<div class="cbp-item latest sale">
<div class="property_item">
<div class="image">
<a href="{{ url('property_detail/'.$property->id) }}"><img id="dose" src="{{ $property->property_image == '' ? '/backend/assets/images/avatars/thumb-3.jpg': '/'.$property->property_image }}" alt="latest property" class="img-responsive"></a>
<div class="price clearfix">
<span class="tag pull-right">GHC {{ $property->price }}
@if($property->payment_type != '')
{{ $property->payment_type }}
@else
@endif</span>
</div>
<span class="tag_t">{{ $property->property_type }}</span>
</div>
<div class="proerty_content">
<div class="proerty_text">
<h3 class="captlize"><a href="{{ url('property_detail/'.$property->id) }}">{{ $property->name }}</a></h3>
<p>{{ $property->location }}</p>
</div>
<div class="property_meta transparent">
@if($property->land_size != '')
<span><i class="icon-select-an-objecto-tool"></i>{{ $property->land_size }}</span>
@else
@endif
@if($property->bedroom != '')
<span><i class="icon-bed"></i>{{ $property->bedroom }} Bedroom(s)</span>
@else
@endif
<span style="color:blue;"><i class="icon-safety-shower"></i>{{ $property->property_category->name }}</span>
</div>
<div class="favroute clearfix">
<p><i class="icon-calendar2"></i> {{ $property->created_at->toFormattedDateString() ?? '' }} </p>
<ul class="pull-right">
<li>
<form action="{{ url('addfavorite/'.$property->id) }}" method="post">
@csrf
<button type="submit"><i class="icon-like"></i></button>
</form>
</li>
<li><a href="#" class="share_expender" data-toggle="collapse"><i class="icon-share3"></i></a></li>
</ul>
</div>
<div class="toggle_share collapse" id="seventy">
<ul>
<li><a href="javascript:void(0)" class="facebook"><i class="icon-facebook-1"></i> Facebook</a></li>
<li><a href="javascript:void(0)" class="twitter"><i class="icon-twitter-1"></i> Twitter</a></li>
<li><a href="javascript:void(0)" class="vimo"><i class="icon-vimeo3"></i> Vimeo</a></li>
</ul>
</div>
</div>
</div>
</div>
@endforeach
</div>
@if(isset($_GET['sort']))
<div class="col-sm-12 text-center top20">
{{ $properties->appends(['sort'=>$_GET['sort']])->links() }}
</div>
@else
<div class="col-sm-12 text-center top20">
{{ $properties->links() }}
</div>
@endif
</div>
</section>
<!-- Gallery End -->
PropertyController
<?php
namespace App\Http\Controllers;
use App\Models\Property;
use App\Models\PropertyCategory;
use App\Models\Blog;
use App\Models\Admin;
use App\Models\Favorite;
use Illuminate\Support\Str;
use Carbon\Carbon;
use RealRashid\SweetAlert\Facades\Alert;
use Session;
use Auth;
use DB;
use Illuminate\Http\Request;
class PropertyController extends Controller
{
public function index(){
$pg = "property";
$properties = Property::with(['property_category'])->where('status','1')->where('purchase_status','Not Sold');
//Checking or sorting properties by prices and name
if(isset($_GET['sort']) && !empty($_GET['sort'])){
if($_GET['sort']=="property_latest"){
$properties->orderby('properties.id','Desc');
}else if($_GET['sort']=="price_lowest"){
$properties->orderby('properties.price','Asc');
}else if($_GET['sort']=="price_highest"){
$properties->orderby('properties.price','Desc');
}else if($_GET['sort']=="name_a_z"){
$properties->orderby('properties.name','Desc');
}else if($_GET['sort']=="name_z_a"){
$properties->orderby('properties.name','Asc');
}
}
$properties = $properties->inRandomOrder()->paginate(3);
return view('properties',compact('properties','pg'));
}
public function newProperties(){
$pg = "newproperty";
$newproperties = Property::with(['property_category'])->where('status','1')->where('purchase_status','Not Sold')->latest()->paginate(3);
return view('new_properties',compact('newproperties','pg'));
}
public function details($id){
$pg = "property";
$property = Property::with(['property_category','property_images'])->where(['id'=>$id,'status'=>1])->where('purchase_status','Not Sold')->first();
return view('property_detail',compact('property','pg'));
}
public function categoryProperties($slug){
if(PropertyCategory::where('slug', $slug)->exists()){
$pg = "property";
$propertycategory = PropertyCategory::where('slug', $slug)->first();
$catproperties = Property::where('property_category_id',$propertycategory->id)->where('status',1)->paginate(2);
return view('cat_properties',compact('catproperties','propertycategory'));
}else{
abort(404);
}
}
public function searchProperty(Request $request)
{
$propertycategories = PropertyCategory::where('status','1')->get();
$query = $request->input('query');
//you can search property by name, address, location, but we are searching by name
$properties = Property::with(['property_category'])->where('name','LIKE',"%$query%")->where('status','1')->where('purchase_status','Not Sold')->get();
return view('search_property',compact('properties','query','propertycategories'));
}
public function addFavorite(Request $request, $id){
$pg = "favorite";
if($request->isMethod('post')){
$data = $request->all();
//Get property id
$property = Property::find($id);
$property_id = $property->id;
//Generate Session Id at cart table if not exist and if user has not login
$session_id = Session::get('session_id');
if(empty($session_id)){
$session_id = Session::getId();
Session::put('session_id',$session_id);
}
//If properties already exist in cart table
if(Auth::check()){
//if user is login
$session_id = "";
$user_id = Auth::user()->id;
$countProperties = Favorite::where(['property_id'=>$property_id,'user_id'=>$user_id])->count();
}else{
//If user is not login
$user_id = 0;
$countProperties = Favorite::where(['property_id'=>$property_id,'session_id'=>$session_id])->count();
}
if($countProperties>0){
Alert::warning('Property Already exist in cart!');
return back();
}
//Add properties to cart table
$item = new Favorite;
$item->session_id = $session_id;
$item->user_id = $user_id;
$item->property_id = $property_id;
$item->save();
Alert::success('Property Added to favorite successfully! <a href="/favorite_properties">View Favorite</a>');
return back();
}
}
public function favoriteProperty(){
$pg = "favorite";
if(Auth::check()){
//If user is logged in / pick auth id of user
$getFavoriteItems = Favorite::with(['property'=>function($query){
$query->select('id','name','price','property_type','location','property_image','property_category_id');
}])->where('user_id',Auth::user()->id)->orderby('id','Desc')->get();
}else{
//If user is not logged in / pick session id
$getFavoriteItems = Favorite::with(['property'=>function($query){
$query->select('id','name','price','property_type','location','property_image','property_category_id');
}])->where('session_id',Session::get('session_id'))->orderby('id','Desc')->get();
}
return view('favorite_properties',compact('getFavoriteItems','pg'));
}
public function destroyFavorite($id){
DB::table('favorites')->where('id',$id)->delete();
Alert::success('Favorite item deleted successfully');
return back();
}
}
But when the properties displays on the browser, anytime i click on add to favorites of the first property, it does not work, but when you click on the add to favorite of the other properties, it works perfectly, and this add to favorite function is working on all my pages with the same code, except the first property in the properties page, and when i inspect the form does not show, but shows for the other properties
<li>
<input type="hidden" name="_token" value="T83Q3dqRaqA49nKQdKN1XOvW6Gmhs4ZDO78VHtMx">
<button type="submit">
<i class="icon-like"></i>
</button>
</li>
But the others come like this
<li>
<form action="http://emmadocestates.test/addfavorite/2" method="post">
<input type="hidden" name="_token" value="T83Q3dqRaqA49nKQdKN1XOvW6Gmhs4ZDO78VHtMx"> <button type="submit">
<i class="icon-like"></i>
</button>
</form>
</li>
What could be the issue?, do i have to do something in my browser?