如何将变量从控制器发送到窗体中使用的另一个控制器

时间:2018-02-27 15:25:16

标签: php laravel laravel-5 laravel-5.6

我有两个控制器,一个用于帖子,另一个用于部分

我想在create.blade.php中创建帖子并显示我在使用变量时从addSection.blade.php创建的部分我得到未定义的变量!

我想要实现的目标是创建帖子,在创建帖子页面中我想显示部分,以便用户可以选择帖子并将帖子发布到特定部分。

class PostController extends Controller

{


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //

        $posts = Post::orderBy("created_at","desc")->paginate(2);
         return view('index')->with('posts',$posts);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('/create');
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $request->validate([
            'progName' => 'required|unique:posts|max:255',
            'description' => 'required',
            'postImg' => 'image|nullable|max:1024',
        ]);

        if ($request -> hasFile('postImg')) {
            # code...
            $fileNameWithExtention = $request ->file('postImg')->getClientOriginalName();
            $fileName = pathinfo($fileNameWithExtention,PATHINFO_FILENAME);
            $extention = $request -> file('postImg')->getClientOriginalExtension();
            $fileNameStore = $fileName . '__'.time().'.' .$extention;
            $path =  $request ->file('postImg')->storeAs('public/postimage',$fileNameStore);
        }else{
            $fileNameStore = 'noimage.jpg';
        }

        $post = new Post;
        $post -> progName = $request->input('progName');
        $post -> version = $request->input('version');
        $post -> OperatingSystems = $request->input('OperatingSystems');
        $post -> description = $request->input('description');
        $post -> link = $request->input('link');
        $post -> postImage =  $fileNameStore;
        $post -> UserIdForPost = auth()->user()->id;
        $post -> save();



        return redirect('/home/') -> with(['success' => 'Done ^__^ ' ]);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


}



also i hade make anothe Controller to Handel sections ( is this right ? )






    class sections extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {
            $sections = Section::orderBy("created_at","desc")->paginate(2);
            return view('/addSections')->with('sections',$sections);
        }

        /**
         * Show the form for creating a new resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function create()
        {
            //
            return view('/addSections');
        }

        /**
         * Store a newly created resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        public function store(Request $request)
        {
            //
            $sections = new Section;
            $sections -> sectionName = $request->input('sectionName');
            $sections -> save();
            return redirect('/home/') -> with(['success' => 'Done ^__^ ' ]);

        }

        /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function show($id)
        {
            //
            $sections =  Section::find($id);
            return view('/create')->with('sections',$sections);

        }

        /**
         * Show the form for editing the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function edit($id)
        {
            //
        }

        /**
         * Update the specified resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function update(Request $request, $id)
        {
            //
        }

        /**
         * Remove the specified resource from storage.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function destroy($id)
        {
            //
        }
    }

这是我的addSection.blade.php

<form action="{{action('sections@store')}}" method="post" enctype="multipart/form-data">

    <div class="card border-secondary col-4">
        <div class="card-header">Add Sections</div>
        <div class="card-body">

            <div class="form-group ">

                <label for="sectionName"><h4 class="card-title" style="text-align: center">Section Name</h4></label>
                <input name="sectionName" type="text" class="form-control" placeholder="program name...">


            </div>
            <button type="submit" class="btn btn-warning">Click Me</button>
        </div>
    </div>

    {{ csrf_field() }}

</form>

并在另一页(create.blade.php)中使用此操作 这个页面我用来创建帖子

action="{{action('PostController@store')}}

0 个答案:

没有答案