简体   繁体   English

无法在Laravel上调用函数

[英]Can't call a function on Laravel

I'm trying to create a form using BootForm on Laravel. 我正在尝试在Laravel上使用BootForm创建一个表单。 This is the content of the function in the PostsController. 这是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). 因此,当我单击按钮时,它会将我重定向到主目录(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() !!} 我不确定这些选项中的url是什么,但是我仍然添加了它,因为您也...不使用<a href="....">...</a>而是使用{!! 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: 然后在UpdatesController类中,添加以下内容:

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. 如果news()代码正确,那么http://localhost:8000/news/现在应该返回所需的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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