jet

how can i add a validation rules in the controller and form

Posted 1 month ago by jet

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::id();
        if ($request->hasFile('logo')) {
            $data['logo'] = $request->file('logo')->store('public');
        }
        if(University::create($data)) {
             $data = $request->all();    
            Session::flash('success', 'sucessfully save!');
            return redirect('/universities');
        } else {
            return 'there was an error';
        }

          $data->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)) {
             $data = $request->all();
             Session::flash('success', 'sucessfully save!');
            return redirect('/universities');
        } else {
            return 'there was an error';
        }

        $data->save();
    }

    /**
     * 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';
        }
    }
}

form

 <div class="row">
    @if($university->id)
      <form method="POST" action="/universities/{{$university->id}}" enctype="multipart/form-data" class="col-md-12">
        <input type="hidden" name="_method" value="PUT" />
    @else
      <form method="POST" action="/universities" enctype="multipart/form-data" class="col-md-12">
    @endif
      {{ csrf_field() }}

        <input type="hidden" name="id" value="{{$university->id}}" />

      <div class="row">
      <div class="col-md-6 form-group">
        <label>Name</label>
        <input type="text" name="name" placeholder="Name" value="{{$university->name}}" class="form-control" />
      </div>
      <div class="col-md-6 form-group">
        <label>Country</label>
        <select type="text" name="country" placeholder="Name" class="form-control"  >
          <option>
            Select Country
          </option>
          @foreach ($countries as $cat)
            <option
              value="{{$cat}}"
              @if($cat === $university->country)
                selected = "selected"
              @endif
              >
              {{$cat}}
            </option>
          @endforeach

        </select>
      </div>
      </div>
      <div class="form-group">
        <label>About</label>
        <textarea name="about" placeholder="About" class="form-control ckeditor">{!! $university->about !!}</textarea>
      </div>
        <div class="row">
          <div class="col-md-6 form-group">
            <label>Address</label>
            <input type="text" name="address" placeholder="Address" value="{{$university->address}}" class="form-control" />
          </div>
          <div class="col-md-6 form-group">
            <label>Logo</label><br />
            <input type="file" name="logo" placeholder="Photo" value="{{$university->logo}}"  />
          </div>
        </div>

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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.