jet
196
4
Laravel

Trying to get property 'id' of non-object

Posted 6 months ago by jet

i need help

university controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\models\University;
use Auth;
use Session;

class UniversityController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $universities = University::all();

        return view('university.index', ['universities' => $universities]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $university = new University();
        return view('university.form', ['university' => $university, 'action' => 'create', 'countries' => $university->getCountries()]);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $data = $request->input();
        unset($data['_token']);
        $data['user_id'] = Auth::user()->id;
        if ($request->hasFile('logo')) {
            $data['logo'] = $request->file('logo')->store('public');
        }
        if(University::create($data)) {
            Session::flash('success', 'sucessfully save!');
            return redirect('/universities');
        } else {
            return 'there was an error';
        }

             $this->validate($request, array(
                'name' => 'required|max:255',          
                'about' => 'required|max:255',          
                'country' => 'required|max:255',          
                'address' => 'required|max:255',          
        ));

        $university = new University;

        $university->name = $request->name;
        $university->about = $request->about;
        $university->country = $request->country;
        $university->address = $request->address;

        $university->save();

        Session::flash('success', 'sucessfully save!');
    }


    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $university = University::findOrFail($id);
        return view('university.show', ['university' => $university]);

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $university = University::findOrFail($id);
        return view('university.form', ['university' => $university, 'action' => 'edit', 'countries' => $university->getCountries()]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->input();
        unset($data['_token']);
        unset($data['_method']);
        $data['user_id'] = Auth::user()->id;
        if ($request->hasFile('logo')) {
            $data['logo'] = $request->file('logo')->store('public');
        }
        if(University::where('id', $id)->update($data)) {

            return redirect('/universities');
        } else {
            return 'there was an error';
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        if (University::findOrFail($id)->delete()) {
            return response()->json('');
        } else {
            return 'there was an error';
        }
    }
}


index form

@extends('layouts.admin')


@section('content')

    <h3>All Universities</h3>

    <p>
        <a href="/admissions" class="btn btn-warning btn-sm">Programs</a>
      <a href="/universities/create" class="btn btn-success btn-sm">Add University</a>
    </p>
    @if ($universities->count() > 0)
    <div class="table-responsive">
      <table class="table table-striped">
        <tr>
          <th>
             Logo
          </th>
          <th>
            Name
          </th>
          <th>
            Address
          </th>

          <th>
            Actions
          </th>
        </tr>
        @foreach($universities as $university)
          <tr>
            <td>
               <img class="" src="{{asset($university->logo)}}" width="50" />
            </td>
            <td>
              <a href="/universities/{{$university->id}}" >{{$university->name}}</a>
            </td>
            <td>
              {{$university->address}}
            </td>

            <td>
              <form  onsubmit="deleteItem(event,'/universities/{{$university->id}}', 'University' )" style="display: inline-block;">
                {{ csrf_field() }}
                <input type="hidden" name="_method" value="DELETE">
                <button type="submit" class="btn btn-danger btn-sm">Delete</button>
              </form>
              <a href="/universities/{{$university->id}}/edit" class="btn btn-primary btn-sm">Edit</a>
            </td>
          </tr>
        @endforeach
      </table>
    </div>
   @else
       <p>
         You have no universities at the moment.
       </p>
   @endif




@endsection

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.