mardi 27 février 2018

how to send variable from controller to another controller that is used in form

I have two controllers one for Posts and another for section

I want to create posts form create.blade.php and display sections that I have created from addSection.blade.php when I use variable I get undefined variable !

again what I want to achieve here is to create posts and in create posts page I want to display sections so the user can select and post posts to specific section.

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)
    {
        //
    }
}

and this is my addSection.blade.php

<form action="" 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>

    

</form>

and in another page ( create.blade.php ) i use this action this page i use to create posts action="



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire