Am trying to update a record in my database using an update button that is linked to a page where the update form is located but each time I click on the button, I get a 404 error (I think maybe the problem is that the page could not read the id of the requested post).
This is my route
Route::get('/pages/update/{id}','CallsController#edit');
Route::post('/pages/update/{id}','CallsController#update');
My CallsController
public function edit($id)
{
// $calls = Call::where('user_id', auth()->user()->id)->where('id', $id)->first();
// return view('pages.assignCall', compact('calls', 'id'));
$calls = Call::find($id);
return view('pages.assignCall')->with('calls', $calls);
}
public function update(Request $request, $id)
{
$calls = new Call();
$data = $this->validate($request, [
'call_details'=>'required',
]);
$data['id'] = $id;
$calls->updateCall($data);
return redirect('pages.pendingCalls');
}
I also have update.blade.php in the pages folder inside my views.
This is my update button
<a href="{{asset('/pages/update')}}>update</a>
You should not use the asset helpers to generate urls, but the url helpers. Also, you have to pass the id of the item you want to update. Because this is missing, you get a 404.
You should do something like this:
update
You can use laravel name route. If you use name route then your route should look like this
Route::get('/pages/update/{id}','CallsController#edit')->name('pages.update.view');
Route::post('/pages/update/{id}','CallsController#update')->name('pages.update');
And your update button should look like this
update
Related
I am getting a "404 | Not Found" error when i try to access a specific item from my database. The items with the specific ID's do exist in the database, but i am not even sure if that even has any influence on the problem.
My routing looks likes this:
Route::prefix('departments')->group(function() {
Route::get('/{id}', [DepartmentController::class, 'showDepartment']);
});
And the related controller looks like this:
public function showDepartment() {
return '';
}
}
I haven't yet finished the function. I just wanted to check if the routing even worked by returning an empty string.
So what am i doing wrong? Is it the routing or the controller?
According to the Laravel documentation, you have to define the parameter in the route then use it in the controller as a parameter.
in your controller:
public function showDepartment($id) {
return 'Hello';
}
The id is not bound to any model to fetch from the database to do that you can use Laravel route model binding
for example when you have a model named Department you write your route like this:
Route::get('/{department}', [DepartmentController::class, 'showDepartment']);
and in the controller:
public function showDepartment(Department $department) {
return 'Hello from depratment';
}
When your department exists it returns the response otherwise return 404.
You may need a Department model class. Then you can find the item from database by id $department = Department::findOrFail($id);
you are send parameter in route and this function without any parameter
route :
Route::prefix('departments')->group(function() {
Route::get('/{id}', [DepartmentController::class, 'showDepartment']);
});
your function in controller should be
public function showDepartment($id) {
$department = Department::findOrFail($id);
}
}
I'm creating a blog post with Laravel 5.5. Here I want to auto-generate accessible slug for a post upon saving. What I did here was:
'slug' => str_slug(request('title'))
It generates the slug value but the page url is not working. For e.g if I click 127.0.0.1:8000/title it should redirect me.
Controller
public function save(Request $request, Post $post)
{
$post= new Post;
$post->title = request('title');
$post->slig => str_slug(request('title'));
$post->save();
}
Route
Route::post('/', 'PostsController#save')->name('add_post');
we store the title and replace every space to dash '-' to auto-generate accessible slug for a post upon saving on this steps :
i use this code on controller
public function store(Request $request){$post->slug = str_replace(' ','-',strtolower($post->title));}
and on
public function show($slug)
{
//
$post=Post::where('slug',$slug)->first();
return view('posts.show', compact('post'));
}
and edit post link like this
link
I would look at using one of the Sluggable packages. https://packagist.org/?q=sluggable I have used the one by Spatie before and it works well.
Once you create your new entity/model and you then have a slug you will need to create a route to a controller which looks up the entity using the slug field.
$thing = Thing::whereSlug($request->get('slug'))->firstOrFail();
I've created a resource
Route::resource('page-category', 'PageCategoryController',['except'=>['create'] ]);
code in view:
Edit
and my edit method in PageCategoryController.php
public function edit($id)
{
$pcategory = PageCategory::find($id);
return view('admin.page-category.show')->withPcategory($pcategory);
}
when i click the button in the view it redirectes to my 404 view. When i hover over the button the link is like http://localhost:8000/page-category//edit . When i manually insert id number in link http://localhost:8000/page-category/1/edit it does takes me to edit page.
Try this according to routing guide on https://laravel.com/docs/5.4/controllers#resource-controllers
Edit
What if your controller becomes:
public function edit($id) {
$pcategory = PageCategory::find($id);
return view('admin.page-category.show')->compact('pcategory');
}
So this will send the $pcategory, in this case your page category with the id ($id), to your view.
From there you'll be able access to your route page-category.edit by sending it the id of the page category: $pCategory->id just like you did.
I'm just finished the intermediate laravel tutorial here: https://laravel.com/docs/5.2/quickstart-intermediate and am trying to push on a bit.
While I can fetch all the tasks via auth'd user id with:
public function index(Request $request)
{
$tasks = $request->user()->tasks()->get();
return view('tasks', [
'tasks' => $tasks,
]);
}
I want to create a view function... how, if I create a link to /task/7 to I query the info about the task with id 7 (for example) & send it to the view?
Define a route to match that /task/7 URL like so:
Route::get("/task/{taskId}", "TaskController#getView");
Then, in your TaskController, define getView function as:
public function getView($taskId){
$task = \App\Task::where("id", "=", $taskId)->first();
return view("tasks.view")->with(["task" => $task]);
}
This is the simplest way to pass a taskId to via a URL parameter, query your DB for the matching record and return a view to display information about it.
There's more involved, like validating the record exists for example, but this should get you on your way.
Brand new to Laravel!
As my title states I would like to be able to immediately redirect from storing a database record to its edit view.
I have a controller called PlannerController. Within this controller I have the standard store() and edit($id) methods. Here is the code for my store() method:
public function store()
{
$newIncome = new Income('fin_income');
$newIncome->user_id = '1';
$newIncome->income_id = Input::get('categories');
$newIncome->income_desc = "Test YO!";
$newIncome->income_date = date('Y-m-d');
$newIncome->amount = Input::get('amount');
$newIncome->save();
return Redirect::route('planner.edit');
}
When I redirect to the planner.edit view (the edit($id) controller method) I have not been able to figure out how to carry over the fin_income.id field, which is auto_incremented upon addition to the database. When I do the redirect like this, my redirect url is /planner/{planner}/edit.
Obviously I need something extra in the store() method but what is the best way to go about this?
have you tried:
return Redirect::route('planner.edit', ['id' => $newIncome->id]);
after you save() on the model, the auto incremented value will be set on your primary key property.