简体   繁体   中英

Can't call a function on Laravel

I'm trying to create a form using BootForm on Laravel. This is the content of the function in the PostsController.

 public function update($id,$request)
{
    //
    $post =findOrFail($id);
    $post->update($request->all());
    redirect(route('news'));
}

The view is the following code:

    @extends ('layouts.app')

@section ('content')

    <h1>Edit</h1>

    {!! BootForm::openHorizontal (['url' => 'user', 'sm' => [2, 5], 'lg' => [2, 5], 'method'=> 'put']) !!}

    {!! BootForm::text('Titre', $post->title) !!}

    {!! BootForm::text('Slug', $post->slug) !!}

    {!! BootForm::textarea('Contenu', $post->content) !!}

    <p> <a class="btn btn-primary" href="{{route('news.update', $post) }}">Editer</a></p>

    {!! BootForm::close() !!}

@stop

Also there is the route I'm using this one :

Route::resource('news', 'PostsController');

So, when I click on the button, it redirects me on main directory (localhost:8000). The function in the controller is not called. No change in the post is noticed though. The problem is in the update function. Do I miss something on it? Some tips or help will be welcome.

Please replace your function with

public function update($id)
{
  $post = Post::findOrFail($id);

  $post->update(request()->all());

  return redirect(route('news.index'));
}

and please replace your view with this.

@extends('layouts.app')

@section('content')

  <h1>Edit</h1>

  <?php $formOptions = [
    'url' => 'user',
    'sm' => [2, 5],
    'lg' => [2, 5],
    'method'=> 'put'
  ]; ?>

  {!! BootForm::openHorizontal($formOptions)->action(route('news.update', $post)) !!}
  <input type="hidden" name="_method" value="PUT">
  {!! BootForm::text('Titre', $post->title) !!}
  {!! BootForm::text('Slug', $post->slug) !!}
  {!! BootForm::textarea('Contenu', $post->content) !!}
  {!! BootForm::submit('Editer') !!}
  {!! BootForm::close() !!}

@stop

I am not sure what url does in the options, but I have still added it since you have also... And instead of using <a href="....">...</a> , I am using {!! BootForm::submit() !!} {!! BootForm::submit() !!}

Let me know if you face any other issues.

try a named route like this one:

Route::resource('news' ['uses' => 'UpdatesController@news', as 'updateNews']);

then in the UpdatesController class, add something like this:

namespace App\Http\Controllers;
use App\Http\Controllers\controller;

class UpdateController extends Controller
{
    public news() {
        //your logics for updating here
    }
}

http://localhost:8000/news/ should now return the desired view, if your code in news() is correct that is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM