一个视图中的多个表单 Laravel 5.1

Multiple forms in one view Laravel 5.1

我有问题。有人可以告诉我我可以在一条路线上的一个 vie 中使用一对表格吗?我的意思是我在显示用户配置文件的 ProfileController 中的显示函数中使用 show.blade.php 中的 post 方法的三种形式。我在这个视图中 posting 了 3 种形式,在路线中我有这样的东西:

Route::post('profile/{id}', MessageController@store');// this is private message to other user

Route::post('profile/{id}', CommentController@store'); //this is comment to profile

Route::post('profile/{id}', CommentController@storeCommit'); // this is answer to comment profile

我不知道我在做什么 wrog,但这不起作用,我的意思是如果我删除 CommitStore 路由我可以发送消息并添加评论,但是如果我添加这个 storeCommit 我不能添加评论,因为我收到错误,字段来自 storeCommit 的不能为空。明天我从控制器和视图中添加代码。有知道的请帮帮我,谢谢

这是具有三种形式的视图,发送消息、向配置文件添加评论和在配置文件中添加对评论的回复:

@extends('layout')

@section('content')


<div>

    <!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist">
        <li role="presentation" class="active"><a href="#info" aria-controls="info" role="tab" data-toggle="tab">Informacje</a></li>
        <li role="presentation"><a href="#postacie" aria-controls="postacie" role="tab" data-toggle="tab">Postacie</a></li>
        <li role="presentation"><a href="#posty" aria-controls="posty" role="tab" data-toggle="tab">Posty</a></li>
        <li role="presentation"><a href="#komentarze" aria-controls="komentarze" role="tab" data-toggle="tab">Komentarze</a></li>
    </ul>

    <!-- Tab panes -->
    <div class="tab-content">
        <div role="tabpanel" class="tab-pane active" id="info">
            <table class="table table-bordered">
                <tr class="active">
                    <td> Avatar </td>
                    <td> Informacje </td>
                </tr>

                <tr class="info">
                    <td>

                        <img src="{{$user->avatar}}" width="150px" height="150px" alt="Avatar" class="img-circle"> <BR /><BR />
                        @if(!\Auth::guest() && $user->id == \Auth::user()->id)
                        <a style="text-align:right" href="/profile/{{$user->id}}/edit"<button type="button" class="btn btn-primary btn-xs">Edytuj profil</button></a>   
                        @endif  

                        @if(!\Auth::guest() && \Auth::id() != $user->id)
                        <button style="float:right" type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModal">
                            <span class="glyphicon glyphicon-pencil"></span>
                        </button>
                        @endif

                        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                            <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title" id="myModalLabel">Wyślij wiadomość do {{$user->name}}</h4>
                                    </div>
                                    <div class="modal-body">
                                        {!! Form::model(['method' => 'POST', 'action' => ['MessageController@store', $user->id]]) !!}
                                        <div class="form-group">
                                            {!! Form::text('title', null, ['class' => 'form-control', 'placeholder' => 'Wprowadź tytuł wiadomości' ]) !!}
                                        </div>

                                        <div class="form-group">
                                            {!! Form::textarea('message', null, ['class' => 'form-control', 'placeholder' => 'Wprowadź wiadomość' ]) !!}
                                        </div>

                                        {!! Form::hidden('mess_to_user', $user->id) !!}
                                        {!! Form::hidden('from_user_name', $user->name) !!}

                                    </div>
                                    <div class="modal-footer">
                                        {!! Form::submit('Wyślij wiadomość', ['class' => 'btn btn-primary']) !!}

                                    </div>
                                    {!! Form::close() !!}
                                    @include('errors.list')
                                </div>
                            </div>
                        </div>      
                    </td>

                    <td width="960">
                        {!!$user->showName($user->name)!!}

                        @if($user->is_online == 1)
                        <span class="label label-success">Online</span>
                        @else
                        <span class="label label-danger">Offline</span>     
                        @endif  

                        <BR />

                        @foreach($user->group as $group)
                        <strong>Grupa użytkownika:</strong> {{$group->name}}
                        @endforeach

                        <BR />
                        <strong>Dołączył dnia:</strong> {{$user->created_at->diffForHumans()}} <BR />
                        <strong>Wiek: </strong>{{$user->age}}
                        <strong>Płeć: </strong>{{$user->formatSex($user->sex)}} <BR />


                        <div class="panel panel-primary">
                            <div class="panel-heading" role="tab" id="headingTwo">
                                <h4 class="panel-title">
                                    <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                                        Sygnatura użytkownika {{$user->name}}
                                    </a>
                                </h4>
                            </div>
                            <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
                                <div class="panel-body">
                                    @if(empty($user->signature))
                                    Brak sygnatury
                                    @else
                                    {!! $user->signature !!}
                                    @endif  
                                </div>
                            </div>
                        </div>

                    </td>
                </tr>
            </table>

            <table class="table table-condensed">

                <!-- Tabela na KONTAKTY-->
                <tr>

                    <!--SOCIAL-->
                    <td>
                        <table class="table table-condensed">
                            <tr>
                                <td class="active">
                                    @if(empty($user->skype))
                                    <img src="http://a3.mzstatic.com/eu/r30/Purple6/v4/1e/0c/96/1e0c9683-1019-f450-f07e-017b6f0012c6/icon175x175.png" width="16" height="16" /> <span class="label label-warning">Niezdefiniowano</span>
                                    @else
                                    <img src="http://a3.mzstatic.com/eu/r30/Purple6/v4/1e/0c/96/1e0c9683-1019-f450-f07e-017b6f0012c6/icon175x175.png" width="16" height="16" /> {{$user->skype}}
                                    @endif  
                                </td>
                            </tr>

                            <tr>
                                <td class="active">
                                    @if(empty($user->facebook))
                                    <img src="http://blogs-images.forbes.com/peterhimler/files/2014/02/high-res-logo_facebook1.png" width="16" height="16" /> <span class="label label-warning">Niezdefiniowano</span>
                                    @else
                                    <img src="http://blogs-images.forbes.com/peterhimler/files/2014/02/high-res-logo_facebook1.png" width="16" height="16" /> <a href="{{$user->facebook}}">{{$user->facebook}}</a>
                                    @endif  
                                </td>
                            </tr>

                            <tr>
                                <td class="active">
                                    @if(empty($user->twitter))
                                    <img src="http://health.uri.edu/files/twitter.png" height="16" width="16" /> <span class="label label-warning">Niezdefiniowano</span>
                                    @else
                                    <img src="http://health.uri.edu/files/twitter.png" height="16" width="16" /> <a href="{{$user->twitter}}">{{$user->twitter}}</a>
                                    @endif  
                                </td>
                            </tr>
                        </table>
                    </td>

                    <!--GAME-->
                    <td width="50%">
                        <table class="table table-condensed">
                            <tr>
                                <td class="active">
                                    @if(empty($user->xfire))
                                    <img src="http://img06.deviantart.net/ebd6/i/2008/045/6/3/xfire_enhanced_icon_by_sparticusx.jpg" height="16" width="16" /> <span class="label label-warning">Niezdefiniowano</span>
                                    @else
                                    <img src="http://img06.deviantart.net/ebd6/i/2008/045/6/3/xfire_enhanced_icon_by_sparticusx.jpg" height="16" width="16" /> {{$user->xfire}}
                                    @endif  
                                </td>
                            </tr>

                            <tr>
                                <td class="active">
                                    @if(empty($user->steam))
                                    <img src="http://vignette3.wikia.nocookie.net/clickerheroes/images/5/54/Logo-Steam.png/revision/latest?cb=20150529052012" height="16" width="16" /> <span class="label label-warning">Niezdefiniowano</span>
                                    @else
                                    <img src="http://vignette3.wikia.nocookie.net/clickerheroes/images/5/54/Logo-Steam.png/revision/latest?cb=20150529052012" height="16" width="16" /> <a href="{{$user->steam}}">{{$user->steam}}</a> 
                                    @endif
                                </td>
                            </tr>

                            <tr>
                                <td class="active">
                                    @if(empty($user->origin))
                                    <img src="https://pbs.twimg.com/profile_images/525760420192083969/Tv_fRfVF_400x400.png" height="16" width="16" /> <span class="label label-warning">Niezdefiniowano</span>
                                    @else
                                    <img src="https://pbs.twimg.com/profile_images/525760420192083969/Tv_fRfVF_400x400.png" height="16" width="16" /> {{$user->origin}}
                                    @endif  
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>

        </div>
        <div role="tabpanel" class="tab-pane" id="postacie">
            <table class="table table-bordered">
                <tr class="active">
                    <td>
                        @forelse($user->player as $player)


                        <button type="button" style="width:200px; height:200px" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal{{$player->id}}">{!!$player->checkActive($player->active)!!}<BR />{!! Html::image('skin/Skin_'.$player->skin.'.png') !!}<BR/>
                            {{$player->formatName($player->name)}}  </button>

                            @empty
                            @if(!\Auth::guest() && $user->id == \Auth::user()->id)
                            <center><p><a href="/character/create"><h1><span class="glyphicon glyphicon-plus-sign"></span></h1></a></p></center>
                            @else
                            <center><h1><span class="label label-default">Brak postaci!</span></h1></center>    
                            @endif

                            @endforelse
                        </td>   
                    </tr>
                </table>
            </div>

            <div role="tabpanel" class="tab-pane" id="posty">...</div>
            <div role="tabpanel" class="tab-pane" id="komentarze">
                <BR />
                @if(!\Auth::guest())
                <div class="panel panel-warning">
                    <div class="panel-heading">
                        Dodaj komentarz do profilu użytkownika {!!$user->showName($user->name)!!}
                    </div>
                    <div class="panel-body">
                        {!! Form::model(['method' => 'POST', 'action' => ['CommentController@store', $user->id]]) !!}

                        <div class="form-group">
                            {!! Form::text('comment', null, ['class' => 'form-control', 'placeholder' => 'Wprowadź komentarz do tego profilu ' ]) !!}
                        </div>

                        {!! Form::hidden('to_user', $user->id) !!}

                        <div class="form-group">
                            {!! Form::submit('Dodaj komentarz', ['class' => 'btn btn-primary']) !!}
                        </div>

                        {!! Form::close() !!}
                        @include('errors.list')
                    </div>
                </div>
                @endif

                @forelse($user->queryComment($comments, $user->id) as $com)
                <div class="panel panel-primary">

                    <div class="panel-heading">
                        Komentarz od <a style="color:white" href="/profile/{{$com->user_id}}">{{$com->name}}</a> <span class="label label-warning">{{$com->created_at}}</span>
                        @if(!\Auth::guest())
                        <button style="float:right" type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModal{{$com->id}}">
                            <span class="glyphicon glyphicon-plus-sign"></span>
                        </button>
                        @endif
                    </div>

                    <div class="panel-body">
                        {{$com->comment}}
                    </div>

                    <div class="panel panel-success">
                        <div class="panel-heading" role="tab" id="headingOne">
                            <h4 class="panel-title">
                                <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse{{$com->id}}" aria-expanded="false" aria-controls="collapseOne">
                                    Pokaż wszystkie komentarze
                                </a>
                            </h4>
                        </div>

                        <div id="collapse{{$com->id}}" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
                            @forelse($user->queryCommit($commits, $com->id) as $commit)
                            <div class="panel-body">
                                <div class="panel panel-success">
                                    <div class="panel-heading">
                                        <h3 class="panel-title">Podkomentarz od <a href="/profile/{{$commit->user_id}}">{{$commit->user_name}}</a> <span class="label label-warning">{{$commit->created_at}}</span> </h3>
                                    </div>
                                    <div class="panel-body">
                                        {{$commit->body}}
                                    </div>
                                </div>
                            </div>
                            @empty
                            <div class="panel-body">
                                Brak podkomentarzy!
                            </div>  
                            @endforelse
                        </div>

                    </div>

                    <div class="modal fade" id="myModal{{$com->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="myModalLabel">Dodaj komentarz</h4>
                                </div>
                                <div class="modal-body">
                                    {!! Form::model(['method' => 'POST', 'action' => ['CommentController@storeCommit', $user->id]]) !!}

                                    <div class="form-group">
                                        {!! Form::text('body', null, ['class' => 'form-control', 'placeholder' => 'Wprowadź adnotację do wybranego komentarza ' ]) !!}
                                    </div>
                                    {!! Form::hidden('to_comment', $com->id) !!}
                                    {!! Form::hidden('to_user_com_id', $user->id) !!}
                                </div>
                                <div class="modal-footer">
                                    {!! Form::submit('Dodaj komentarz', ['class' => 'btn btn-primary']) !!}
                                </div>
                                {!! Form::close() !!}
                                @include('errors.list')
                            </div>
                        </div>
                    </div>

                </div>
                @empty
                <p>Brak komentarzy w profilu</p>

                @endforelse
                {!!$comments->render()!!}
            </div>
            @include('errors.list')

            @foreach($user->player as $p)
            @include('character.show')
            @endforeach


        </div>


        @stop

这是 MessageController 和 CommentController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\CommentRequest;
use App\Http\Requests\CommitRequest;
use App\Comment;
use App\Commit;


class CommentController extends Controller
{

    public function store(CommentRequest $request) 
    { 
        \Auth::user()->comment()->create([
            'comment' => $request->input('comment'),
            'to_user' => $request->input('to_user'),
            'from_name' => \Auth::user()->name
            ]);

        flash()->success('Komentarz został dodany!');
        return redirect('/profile/'.$request->input('to_user').'');
    } 

    public function storeCommit(CommitRequest $request)
    {
            Commit::create(
                [
                    'body' => $request->input('body'),
                    'to_comment' => $request->input('to_comment'),
                    'user_name' => \Auth::user()->name,
                    'from_user_id' => \Auth::id(),
                    'to_user_com_id' => $request->input('to_user_com_id')
                ]);

            flash()->success('Dodałeś adnotację do komentarza!');
            return redirect('/profile');   
    }
}



<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\MessageRequest;
use App\Http\Requests\AnswerRequest;
use App\Message;
use App\User;
use App\Answer;

class MessageController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');

    }
    public function index()
    {   
        $sent = Message::where('from_user', \Auth::id())->count();
        $got = Message::where('to_user', \Auth::id())->count();
        $read = Message::where('to_user', \Auth::id())->where('read', 1)->count();
        $notread = Message::where('to_user', \Auth::id())->where('read', 0)->count();
        $sentto = Message::where('from_user', \Auth::id())->latest('created_at')->take(1)->get();
        $gotfrom = Message::where('to_user', \Auth::id())->latest('created_at')->take(1)->get();

        return view('message.index', compact('sent', 'got', 'sentto', 'gotfrom', 'read', 'notread'));
    }

    public function store(MessageRequest $request)
    {
        \Auth::user()->message()->create([
            'title' => $request->input('title'),
            'message' => $request->input('message'),
            'to_user_id' => $request->input('mess_to_user'),
            'from_user_name' => \Auth::user()->name,
            'to_user_name' => $request->input('from_user_name')
            ]);

        flash()->success('Udało Ci się wysłać prywatną wiadomość!');
        return redirect('/profile/'.$request->input('mess_to_user').'');
    }

    public function show($id)
    {
        $message = Message::findOrFail($id);
        $answers = Answer::paginate(10);

        if($message->to_user == \Auth::id() || $message->from_user == \Auth::id())
        {
            if($message->to_user == \Auth::id())
            {
                Message::where('id', $id)->update(['read' => 1]);
            }

            return view('message.show', compact('message', 'answers', 'check'));
        }
        else
        {
            return back();
        }


    }

    public function got()
    {
        $messages = Message::where('to_user', \Auth::id())->latest('created_at')->paginate(10);
        return view('message.got', compact('messages'));
    }

    public function sent()
    {
        $messages = Message::where('from_user', \Auth::id())->latest('created_at')->paginate(10);
        return view('message.sent', compact('messages'));
    }

    public function storeAnswer(AnswerRequest $request)
    {

        Answer::create([
            'answer' => $request->input('answer'),
            'user_id' => \Auth::id(),
            'user_name' => \Auth::user()->name,
            'to_message' => $request->input('to_message')
            ]);

        flash()->success('Wiadomość prywatna została wysłana!');
        return redirect('/private/'.$request->input('to_message').'');
    }
}

Route.php

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
/*==========This is main controller=============*/
Route::get('/', 'Controller@index');
Route::get('about', 'Controller@about');
Route::get('contact', 'Controller@contact');
Route::get('teammate', 'Controller@teammate');

/*=========Questions Controll===================*/

Route::get('quest', 'QuestController@index');
Route::get('quest/create', 'QuestController@create');
Route::get('quest/{id}', 'QuestController@show');
Route::post('quest', 'QuestController@store');


/*=============Changelog controller==============*/
Route::get('change/create', 'ChangeController@create');
Route::get('change', 'ChangeController@index');
Route::post('change', 'ChangeController@store');

/*=============Admin Controller==================*/
Route::get('admin', 'AdminController@index');

Route::get('admin/group/create', 'AdminController@createGroup');
Route::post('admin/group', 'AdminController@storeGroup');

Route::get('admin/note/create', 'AdminController@createNote');
Route::post('admin/note', 'AdminController@storeNote');

Route::get('admin/section/create', 'AdminController@createSection');
Route::post('admin/section', 'AdminController@storeSection');

Route::get('admin/article/create', 'AdminController@createArticle');
Route::post('admin/article', 'AdminController@storeArticle');
Route::get('admin/article/{id}', 'AdminController@showArticle');
Route::get('admin/article/{id}/edit', 'AdminController@editArticle');
Route::post('admin/article/{id}', 'AdminController@updateArticle');

Route::get('admin/profile', 'AdminController@profile');
Route::get('admin/profile/{id}/edit', 'AdminController@editProfile');
Route::post('admin/profile/{id}', 'AdminController@updateProfile');

/*=============Profile Controller================*/
Route::get('profile', 'ProfileController@index');
Route::get('profile/{id}', 'ProfileController@show');
Route::get('profile/{id}/edit', 'ProfileController@edit');
Route::post('profile/{id}', 'ProfileController@update');
Route::post('profile/{id}, 'MessageController@store');
Route::post('profile/{id}, 'CommentController@store');


/*============Character Controller===============*/
Route::get('character/create', 'CharacterController@create');
Route::post('character', 'CharacterController@store');
Route::get('character/online', 'CharacterController@online');


/*============Private Message Controller==========*/
Route::get('private', 'MessageController@index');

Route::get('private/got', 'MessageController@got');
Route::get('private/sent', 'MessageController@sent');
Route::get('private/{id}', 'MessageController@show');
Route::post('private/{id}', 'MessageController@storeAnswer');

Route::controllers(
    [
        'auth' => 'Auth\AuthController',
        'password' => 'Auth\PasswordController',
    ]);

您正在使用同一条路线访问两个不同的功能。这就是为什么当您删除一个代码时,代码会起作用。

//This is wrong.
Route::post('profile/{id}', CommentController@store');
Route::post('profile/{id}', CommentController@storeCommit');

您可以将一个称为 "get",将另一个称为 "post",并在各自的函数中进行必要的更改以获取和处理数据。

Route::get('profile/{id}', CommentController@store');
Route::post('profile/{id}', CommentController@storeCommit');

否则,为功能提供两条不同的路线

Route::post('profile/{id}', CommentController@store');
Route::post('profile/storeCommit/{id}', CommentController@storeCommit');

已更新

尝试像这样更改 第三种 形式:

{!! Form::open(array('method'=>'post', 'route' => array('store.commit', $object->id))) !!} 

然后像这样更改路线:

Route::post('profile/storeCommit/{id}', ['uses' => 'CommentController@storeCommit', 'as' => 'store.commit']);

希望这对您有所帮助。