0

I'm trying to save data into db but its not saving and says that object not found, can anyone suggest me solution, i am following this tutorial: https://laracasts.com/series/laravel-from-scratch-2018/episodes/10

controller:

public function index()
{
    $projects = Project::all();

    return view('projects.index', compact('projects'));
}

public function create()
{
    return view('projects.create');
}

public function store()
{
    $project = new Project();
    $project->title = request('title');
    $project->description = request('description');
    $project->save();

    return redirect('/projects');
}

routes:

Route::get('/projects','ProjectsController@index');
Route::post('/projects','ProjectsController@store');
Route::get('/projects/create','ProjectsController@create');

create.blade.php:

<form method="POST" action="/projects">
    {{ csrf_field() }}
    <div>
        <input type="text" name="title" placeholder="Project title">
    </div>
    <div>
        <textarea name="description" placeholder="Project description"></textarea>
    </div>
    <div>
        <button type="submit">Create Project</button>
    </div>
</form>

index.blade.php:

@foreach($projects as $project)
    <li>{{ $project->title }}</li>
@endforeach
5
  • The code seems to be right... can you post the message error that you're receiving? Commented Mar 10, 2019 at 17:56
  • Object not found! The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error. If you think this is a server error, please contact the webmaster. Error 404 localhost Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.7 Commented Mar 10, 2019 at 18:11
  • It would help if you update the post with that error message, and include information like the url from your address bar and the url of the create page. It sounds like you might be redirecting to a url that doesn't match what you have configured for the website in Apache. Commented Mar 10, 2019 at 18:15
  • this is url of error page localhost/projects and this is url or create page localhost/laravel/project/public/projects/create Commented Mar 10, 2019 at 18:35
  • Can you edit your post and include those details in the question? All of the code you posted is correct - those errors are what show the real problem Commented Mar 10, 2019 at 19:11

2 Answers 2

3

You have missed out passing request parameter in the controller store()

public function store(Request $request)
{
    $project = new Project();
    $project->title = $request->title;
    $project->description = $request->description;
    $project->save();
    return redirect('/projects');
}

And also don't forget to include use Illuminate\Http\Request; above(outside) controller class.

Sign up to request clarification or add additional context in comments.

5 Comments

This isn't required when using request('title'), because the request() function retrieves the Request from the container directly (instead of using dependency injection on the controller method)
@programmer0001 - have you included Project class in controller?
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Project; class ProjectsController extends Controller { public function index() { $projects = Project::all(); return view('projects.index', compact('projects')); } public function create() { return view('projects.create'); } public function store() { $project = new Project(); $project->title = $request('title'); $project->description = $request('description'); $project->save(); return redirect('/projects.index') } }
I would suggest try first debugging the code. For example inside store method at first line put die('test') to see if the request is coming to that store() or not.
Also you can try one more. Modified - use App\Project as Project;
1

The Laravel code you've posted is correct under a properly configured website. The error from your comments:

Object not found! The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error. If you think this is a server error, please contact the webmaster. Error 404 localhost Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.7

is an Apache error page, which means it's not requesting a page from your laravel project at all. The data is probably saving in your database, but then you redirect away to a page that is outside your project, and Apache can't find it.

Your website is located at http://localhost/laravel/public, which means you need to access the projects page at http://localhost/laravel/public/projects. However, redirect('/projects') gives you an absolute path instead of a relative path, sending you to http://localhost/projects, which does not exist.

Solutions

Since this is a local development project, I'm going to skip the issues with the improper Apache configuration and focus on other ways to avoid the error.

Option 1

Use a named route:

Route::get('/projects','ProjectsController@index')->name('projects.index');

and use the name of the route for the redirect:

return redirect()->route('projects.index');

This should generate correct urls within your project.

Option 2

Use serve for development instead of Apache.

Open a terminal in your Laravel project directory and run this command:

php artisan serve

This will start PHP's built-in webserver at http://localhost:8000, skipping Apache entirely. During development this is perfectly fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.