Skip to content
Advertisement

Pulling data into a select option in laravel

I am trying to pull data from a different table into a select tag where the user can choose which program a certain course belongs to. I am getting an error of syntax error, unexpected '}', expecting ')' (View: C:xampphtdocsIRMSresourcesviewscoursescreate.blade.php). How can I achieve this so that when I click on the Add Course button, it will give me the form iI can select the program code from.?

My code below:

index.blade

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <p><a href="." class="btn btn-outline-dark">Go Back</a></p>
            <div class="card">
                <div class="card-header">Courses</div>

                <div class="card-body">
                    @if (session('status'))
                        <div class="alert alert-success" role="alert">
                            {{ session('status') }}
                        </div>
                    @endif

                    <p><a href="/home/courses/create" class="btn btn-success" role="button">Add Course</a></p>
                    @if (count($courses)> 0 )
                        <table class="table table-striped">
                            <tr>
                                <th></th>
                                <th>Program Code</th>
                                <th>Course Code</th>
                                <th>Course Name</th>
                                <th></th>
                                <th></th>
                            </tr>
                            @foreach ($courses as $course)
                                <tr>
                                    <td></td>
                                    <td>{{$course->program_code}}</td>
                                    <td>{{$course->course_code}}</td>
                                    <td>{{$course->course_name}}</td>
                                    <td>Edit</td>
                                    <td>Delete</td>
                                </tr>
                            @endforeach
                        </table>
                    @else
                        <p>No Courses found!</p>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@include('inc.sadmin-navbar')
@endsection

create blade

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <p><a href="." class="btn btn-outline-dark">Go Back</a></p>
            <div class="card">
                <div class="card-header">Add New Course</div>

                <div class="card-body">
                    @if (session('status'))
                        <div class="alert alert-success" role="alert">
                            {{ session('status') }}
                        </div>
                    @endif

                    {!! Form::open(['action'=>'CoursesController@store', 'method'=>'POST']) !!}
                        <div class="form-group row justify-content-center">
                            {{Form::label('program_name', 'Program Name')}}
                            <div class="col-md-4">
                                @foreach ($programs as $program)
                                    {{Form::select('{{$program->id}}', ['{{$program->program_code}}'], null, ['placeholder' => 'Pick Program'])}}
                                @endforeach
                            </div>
                        </div>
                        <div class="form-group row justify-content-center">
                            {{Form::submit('Add Program', ['class'=>'btn btn-success'])}}
                        </div>
                    {!! Form::close() !!}
                </div>
            </div>
        </div>
    </div>
</div>
@include('inc.sadmin-navbar')
@endsection

courses controller

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppCourse;
use AppProgram;

class CoursesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function index()
    {
        $courses = Course::all();
        return view('courses.index')->with('courses', $courses);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return IlluminateHttpResponse
     */
    public function create()
    {
        $programs = Program::all();
        return view('courses.create')->with('programs', $programs);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */
    public function store(Request $request)
    {
        //
    }

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

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

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

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

programs controller

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppProgram;
// use AppCSE;

class ProgramsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function index()
    {
        $programs = Program::all();
        return view('programs.index')->with('programs', $programs);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return IlluminateHttpResponse
     */
    public function create()
    {
        return view('programs.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'program_name' => 'required',
            'program_code' => 'required'
        ]);

        $program = new Program;
        $program->program_name = $request->input('program_name');
        $program->program_code = $request->input('program_code');
        $program->save();

        return redirect('home/programs');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function show($id)
    {
        $program = Program::find($id);
        // $courses = CSE::all();
        return view('programs.software.index')->with('program', $program);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function edit($id)
    {
        $program = Program::find($id);
        return view('programs.edit')->with('program', $program);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'program_name' => 'required',
            'program_code' => 'required'
        ]);

        $program = Program::find($id);
        $program->program_name = $request->input('program_name');
        $program->program_code = $request->input('program_code');
        $program->save();

        return redirect('home/programs');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function destroy($id)
    {
        $program = Program::find($id);
        $program->delete();

        return redirect('home/programs');
    }
}

Advertisement

Answer

You can try this also

$programs = Program::all();
$select = [];
foreach($programs as $program){
    $select[$program->id] = $program->program_code;
}
return view('programs.index', compact(['programs','select'));
 {!! Form::select('program', $select, null, ['class'=>'form-control']) !!}

or

@foreach ($programs as $program)
     {{Form::select($program->id, [$program->program_code], null, ['placeholder'=>'Pick Program'])}}
@endforeach
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement