send me your url from the browser and including your routes and controller code
404 NOT FOUND
am new to laravel 8, as i was working crud, now everything seem to be fine, but the issue i keep on getting 404 NOT FOUND.
web.php
my url >>> localhost:8000/contact
web.php
@Mohammad160980 add ``` on a line by itself just before and after your code
@Sinnbeck could you please be more specific
@Mohammad160980 paste your code inside this ``` open it and paste your code then close it as you have opened it
Your code paste it here!
```
Your code
```
my route
@Mohammad160980 Press the button which is left side of 1 number key in your keyboard three time and paste your code then press 3 times to close it and post your code then ..Click on this link to understand my word.. https://cdn5.vectorstock.com/i/1000x1000/35/89/laptop-keyboard-computer-isolated-black-key-button-vector-28503589.jpg
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ContactController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::resource('/contacts', ContactController::class);
//Route::get('/contact', [ContactController::class, 'contact']);```
```<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Contact; //Data is coming from the database via Model.
class ContactController extends Controller
{
public function index()
{
$contacts = Contact::all();
return view ('contacts.index')->with('contacts', $contacts);
}
public function create()
{
return view('contacts.create');
}
public function store(Request $request)
{
$input = $request->all();
Contact::create($input);
return redirect('contact')->with('flash_message', 'Contact Addedd!');
}
public function show($id)
{
$contact = Contact::find($id);
return view('contacts.show')->with('contacts', $contact);
}
public function edit($id)
{
$contact = Contact::find($id);
return view('contacts.edit')->with('contacts', $contact);
}
public function update(Request $request, $id)
{
$contact = Contact::find($id);
$input = $request->all();
$contact->update($input);
return redirect('contact')->with('flash_message', 'Contact Updated!');
}
public function destroy($id)
{
Contact::destroy($id);
return redirect('contact')->with('flash_message', 'Contact deleted!');
}
}```
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ContactController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::resource('/contacts', ContactController::class);
//Route::get('/contact', [ContactController::class, 'contact']);```
```<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Contact; //Data is coming from the database via Model.
class ContactController extends Controller
{
public function index()
{
$contacts = Contact::all();
return view ('contacts.index')->with('contacts', $contacts);
}
public function create()
{
return view('contacts.create');
}
public function store(Request $request)
{
$input = $request->all();
Contact::create($input);
return redirect('contact')->with('flash_message', 'Contact Addedd!');
}
public function show($id)
{
$contact = Contact::find($id);
return view('contacts.show')->with('contacts', $contact);
}
public function edit($id)
{
$contact = Contact::find($id);
return view('contacts.edit')->with('contacts', $contact);
}
public function update(Request $request, $id)
{
$contact = Contact::find($id);
$input = $request->all();
$contact->update($input);
return redirect('contact')->with('flash_message', 'Contact Updated!');
}
public function destroy($id)
{
Contact::destroy($id);
return redirect('contact')->with('flash_message', 'Contact deleted!');
}
}```
```<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
protected $table = 'contacts';
protected $primaryKey = 'id';
protected $fillable = ['name', 'address', 'mobile'];
}```
@Mohammad160980 Great! Now show me your view file code where you have used route in anchor tag send it !
@Mohammad160980 Are you accessing the this route directly from the browser.
Route::resource('/contacts', ContactController::class);
Layout.blade.php
<html>
<head>
<title>Contact Laravel 8 CRUD</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>```
index.blade.php
```@extends('contacts.layout')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="card">
<div class="card-header">Contacts</div>
<div class="card-body">
<a href="{{ url('/contact/create') }}" class="btn btn-success btn-sm" title="Add New Contact">
<i class="fa fa-plus" aria-hidden="true"></i> Add New
</a>
<br/>
<br/>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Address</th>
<th>Telephone</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach($contacts as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->address }}</td>
<td>{{ $item->mobile }}</td>
<td>
<a href="{{ url('/contact/' . $item->id) }}" title="View Student"><button class="btn btn-info btn-sm"><i class="fa fa-eye" aria-hidden="true"></i> View</button></a>
<a href="{{ url('/contact/' . $item->id . '/edit') }}" title="Edit Student"><button class="btn btn-primary btn-sm"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Edit</button></a>
<form method="POST" action="{{ url('/contact' . '/' . $item->id) }}" accept-charset="UTF-8" style="display:inline">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-danger btn-sm" title="Delete Contact" onclick="return confirm("Confirm delete?")"><i class="fa fa-trash-o" aria-hidden="true"></i> Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection```
Create.blade.php
```@extends('contacts.layout')
@section('content')
<div class="card">
<div class="card-header">Contactus Page</div>
<div class="card-body">
<form action="{{ url('contact') }}" method="post">
{!! csrf_field() !!}
<label>Name</label></br>
<input type="text" name="name" id="name" class="form-control"></br>
<label>Address</label></br>
<input type="text" name="address" id="address" class="form-control"></br>
<label>Mobile</label></br>
<input type="text" name="mobile" id="mobile" class="form-control"></br>
<input type="submit" value="Save" class="btn btn-success"></br>
</form>
</div>
</div>
@stop```
Edit.blade.php
```@extends('contacts.layout')
@section('content')
<div class="card">
<div class="card-header">Contactus Page</div>
<div class="card-body">
<form action="{{ url('contact/' .$contacts->id) }}" method="post">
{!! csrf_field() !!}
@method("PATCH")
<input type="hidden" name="id" id="id" value="{{$contacts->id}}" id="id" />
<label>Name</label></br>
<input type="text" name="name" id="name" value="{{$contacts->name}}" class="form-control"></br>
<label>Address</label></br>
<input type="text" name="address" id="address" value="{{$contacts->address}}" class="form-control"></br>
<label>Mobile</label></br>
<input type="text" name="mobile" id="mobile" value="{{$contacts->mobile}}" class="form-control"></br>
<input type="submit" value="Update" class="btn btn-success"></br>
</form>
</div>
</div>
@stop```
Show.blade.php
```@extends('contacts.layout')
@section('content')
<div class="card">
<div class="card-header">Contactus Page</div>
<div class="card-body">
<div class="card-body">
<h5 class="card-title">Name : {{ $contacts->name }}</h5>
<p class="card-text">Address : {{ $contacts->address }}</p>
<p class="card-text">Phone : {{ $contacts->mobile }}</p>
</div>
</hr>
</div>
</div>```
@Mohammad160980 change your anchor tag from this
<a href="{{ url('/contact/create') }}" class="btn btn-success btn-sm" title="Add New Contact">
to this
<a href="{{ route('contacts.create') }}" class="btn btn-success btn-sm" title="Add New Contact">
yes, but the page error
@Mohammad160980 Change your form tag also from this
<form action="{{ url('contact/' .$contacts->id) }}" method="post">
to this chage it and try again
<form action="{{ route('contact.update' ,$contacts->id) }}" method="post">
@Mohammad160980 message me on my email id shivamyadav4046@gmail.com and install anydesk in your system
@Shivamyadav i did like you said now i got this Target class [App\Http\Controllers\App\Http\Controllers\ContactController] does not exist.
@Mohammad160980 okay run this command on your project directory
php artisan route:list
and what it gives after running this command send it here by copying
lets look at your routes:
The first:
Route::resource('/contacts', ContactController::class);
doesnt specify an action. The only time you can do this is if you have a controller with an single action. This action will need to be named __invoke()
https://laravel.com/docs/9.x/controllers#single-action-controllers
your second route,
Route::get('/contact', [ContactController::class, 'contact'])
specifies an action and you have defined this as 'contact' In your controller you haven't defined a method called action. If you want this to map to your CRUD action,
public function create()
{
return view('contacts.create');
}
Then your route is
Route::get('/contact', [ContactController::class, 'create'])
Please or to participate in this conversation.