When i click on my delete form, i get this error in the browser link
http://estates.test/delete-favorite/5
and i get a
404 NOT FOUND
error, but i am not trying to redirect the delete function to any page, i just want the favorite properties should be deleted and redirect back
properties database table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('properties', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('location');
$table->string('land_size'); //eg. 4200 sq ft (square feet)
$table->integer('bedroom')->nullable();
$table->integer('swimming_pool')->nullable();
$table->integer('garage')->nullable();
$table->float('price');
$table->enum('property_type', ['For Sale', 'For Rent']);
$table->enum('payment_type', ['','Per Month', 'Per Year'])->nullable();
$table->enum('purchase_status', ['Sold', 'Not Sold'])->default('Not Sold');
$table->longText('description');
$table->mediumText('property_image')->nullable();
$table->mediumText('video_link')->nullable(); //eg.www.youtube.com/house
$table->boolean('status')->default(0);
$table->integer('property_category_id')->foreign('property_category_id')->references('id')->on('property_categories')->onDelete('cascade');
$table->integer('admin_created_id')->foreign('admin_created_id')->references('id')->on('admins')->onDelete('cascade');
$table->integer('admin_updated_id')->foreign('admin_updated_id')->references('id')->on('admins')->onDelete('cascade')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('properties');
}
};
favorites database table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->id();
$table->mediumText('session_id')->nullable();
$table->integer('property_id')->foreign('property_id')->references('id')->on('properties')->onDelete('cascade');
$table->integer('user_id')->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('favorites');
}
};
web.php
Route::get('/delete-favorite/{id}', [App\Http\Controllers\PropertyController::class, 'destroyFavorite']);
//Route::delete('/delete-favorite/{id}', [\App\Http\Controllers\Admin\PropertyController::class, 'destroyFavorite']);
PropertyController
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){
$favorites = Favorite::findorfail($id)->delete();
//DB::table('favorites')->where('id',$id)->delete();
Alert::success('Favorite item deleted successfully');
return back();
}
favorite_properties.blade.php file
@foreach($getFavoriteItems as $favorite)
<div class="row bg-hover">
<div class="my-pro-list">
<a href="{{ url('property_detail/'.$favorite->property_id) }}">
<div class="col-md-2 col-sm-2 col-xs-12">
<img id="sly" class="img-responsive" src="{{ $favorite->property->property_image == '' ? '/backend/assets/images/avatars/thumb-3.jpg': '/'.$favorite->property->property_image }}" alt="image"/>
</div>
</a>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="feature-p-text">
<h4>{{ $favorite->property->name }}</h4>
<p>Property Area, {{ $favorite->property->location }}</p>
<span><b>Status:</b> For Sale</span><br>
<div class="button-my-pro-list">
<a href="{{ url('property_detail/'.$favorite->property_id) }}">GHC {{ $favorite->property->price }}</a>
</div>
</div>
</div>
<div class="col-md-2 col-sm-2 col-xs-12">
<div class="select-pro-list">
<a href="{{ url('property_detail/'.$favorite->property_id) }}"><i class="icon-eye"></i></a>
<a href="{{ url('/delete-favorite/'.$favorite->id) }}"><i class="icon-cross"></i></a>
<!--<form method="POST" action="/delete-favorite/{{ $favorite->id }}" accept-charset="UTF-8">
<input type="hidden" name="_method" value="DELETE">
@csrf
<button class="btn btn-icon btn-hover btn-sm btn-rounded" type="submit" onClick="return confirm('Are you sure You want to Delete')" title="Delete">
<i class="anticon anticon-delete"></i>
</button>
</form>-->
</div>
</div>
</div>
</div>
@endforeach
I have try to use to delete, but it is not working, and i have try to use it is also giving the same error as form, the error is it is trying to redirect me to a page that it can't find, so i get 404 page not found error, but i just want to delete the properties in the favorite database table and redirect back, any solution please?