将多个项从循环插入数据库(尝试获取非对象的属性)

时间:2017-06-13 12:51:53

标签: php laravel-5

请求我快速帮助解决错误(尝试获取非对象的属性(查看:C:\ xampp \ htdocs \ Laravel_Apps \ edu_hub \ admin \ resources \ views \ admin \ coursePreset.blade.php))当我尝试将多个复选框项目提交到数据库中时,我得到它。问题似乎来自刀片,因此数据还没有到达控制器。

请在下面找到我的代码:

coursePreset.blade.php

@extends('layouts.admin_layout')

@section('page-name')
    Course Preset
@stop

@section('title')
    Admin | Course Preset
@stop

@section('crumbs')
    <li><a href="{{ url('/admin-index') }}">Dashboard</a></li>
    <li class="active">Course Preset</li>
@stop

@section('content')
    <div class="col-md-12 col-xs-6">
        <div class="white-box">
            <h3 class="box-title text-center">Course Preset List</h3>
            <form class="form-horizontal form-material" action="{{url('/preset')}}" method="POST">
                {{csrf_field()}}

                <div class="row">

                    <div class="form-group">
                        <div class="row">
                            <div class="col-md-2 col-md-offset-3">
                                <select class="form-control"  name="level">

                                    <option>Select</option>
                                    @foreach($level as $item)
                                        <option value="{{$item->level}}">{{$item->level}}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-md-2">
                                <select class="form-control"  name="semester">

                                    <option>Select</option>
                                    @foreach($semester as $item)
                                        <option value="{{$item->semester}}">{{$item->semester}}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-md-3">
                                <button type="submit" class="btn btn-success ">Submit Query</button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
                    <div class="row text-center">
                        <div class="col-md-5 col-sm-6"><h3 class="box-title text-blue">LEVEL: {{$details->level}}</h3></div>
                        <div class="col-md-5 col-sm-6"><h3 class="box-title text-success">SEMESTER: {{$details->semester}}</h3></div>
                    </div>
            <form class="form-horizontal form-material" action="{{url('/preset')}}" method="POST">
                {{csrf_field()}}
                    <div class="row">
                        <div class="col-md-4 col-md-offset-3">
                            {{--@if($count == 0){
                                <p>No Courses Available at the moment</p>
                            }@else--}}
                            @foreach($course as $element)
                                <div class="checkbox checkbox-success">
                                    <input id="checkbox4" type="checkbox" value="{{$element->code}}" name="checkbox" multiple>
                                    <label for="checkbox4">{{$element->name.'-'.$element->code.'-'.$element->credit_hrs}}</label>
                                </div>
                            @endforeach
                           {{-- @endif--}}
                        </div>
                    </div>
                <div class="row">
                    <div class="col-md-5 col-md-offset-4">
                        <button type="submit" class="btn btn-success">Activate Preset</button>
                    </div>
                </div>
            </form>
        </div>
    </div>


@stop

这是来自coursePresetController

<?php

namespace App\Http\Controllers;

use App\coursePreset;
use App\Subject;
use Illuminate\Http\Request;

class coursePresetController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $preset = Subject::select('code')->get();


        $details = Subject::select('level', 'semester')
            ->get()
            ->first()
        ;

        $level = Subject::select('level')
            ->groupBy('level')
            ->get();

        $semester = Subject::select('semester')
            ->groupBy('semester')
            ->get();

        $mylevel = ['level'];
        $mysemester = ['semester'];

        $course = Subject::select('subjects.name', 'subjects.code', 'subjects.credit_hrs')
            ->where('subjects.level', '=', $mylevel)
            ->where('subjects.semester', '=', $mysemester)
            ->get();

        //dd($course);

        return view('admin.coursePreset')
            ->with('level', $level)
            ->with('semester', $semester)
            ->with('course', $course)
            ->with('details', $details)
            ->with('preset', $preset);
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        dd($request->all());
        $preset = new coursePreset();

        $preset->name = $request['name'];
        $preset->code = $request['code'];
        $preset->credit_hrs = $request['credit_hrs'];
        $preset->semester = $request['semester'];
        $preset->level = $request['level'];

        if ($preset->save()){
            //flash($request['name'].' successfully saved.')->success();
            echo 'saved';
        }else{
            //  flash($request['name'].' not saved.')->error();
            echo 'Not saved';
        }

        return redirect()->back();
    }

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

    public function view(Request $request)
    {
        $level = Subject::select('level')
            ->groupBy('level')
            ->get();

        $semester = Subject::select('semester')
            ->groupBy('semester')
            ->get();

        $mylevel = $request['level'];
        $mysemester = $request['semester'];

        //dd($level, $semester);

        $course = Subject::select('subjects.name', 'subjects.code', 'subjects.credit_hrs')
            ->where('subjects.level', '=', $mylevel)
            ->where('subjects.semester', '=', $mysemester)
            ->get();

        //dd($course);
        $details = Subject::select('level', 'semester')
            ->where('semester', '=', $mysemester)
            ->where('level', '=', $mylevel)
            ->get()
            ->first()
        ;

        //$count = ($course);

        return view('admin.coursePreset')
            ->with('course', $course)
            ->with('level', $level)
            ->with('semester', $semester)
            ->with('details', $details);
            //;->with('count', $count);
    }
}

非常感谢快速帮助。 感谢

1 个答案:

答案 0 :(得分:0)

你在控制器索引方法上犯了错误:

CheckBox

这应该是

 $details = Subject::select('level', 'semester')
            ->get()
            ->first()
        ;

希望这有帮助。