image upload code
controller code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
// studentview is the model name
use App\Studentview;
use Validator;
class StudentViewController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
// studentshow is function
public function studentform()
{
return view('studentinsert');
}
public function studentshow()
{
// Studentview is the model name
$students= Studentview::all();
return view('studentview',compact('students'));
}
public function index()
{
//
}
/**
* 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 studentinsert(Request $request)
{
$this->validate($request,[
'name'=>'required|max:30|unique:student',
'price'=>'required',
'imge'=>'required'
]);
$photoName = time().'.'.$request->image->getClientOriginalExtension();
$student = new Studentview();
$student->name = $request->name;
$student->price= $request->price;
$student->image= $photoName;
// imageupload is the foldername
$request->image->move(public_path('imageupload'), $photoName);
$student->save();
return redirect()->route('admin.show')->with('status','data insert successful');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show()
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function studentedit($id)
{
// $data is sent to the studentedit form in value
$data = Studentview::find($id);
return view('studentedit',compact('data'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $idate(format)
* @return \Illuminate\Http\Response
*/
public function studentupdate(Request $request)
{
//
$student=Studentview::find($request->id);
$student->name = $request->student_name;
$student->price =$request->price;
$student->save();
return redirect()->route('admin.show')->with('status','data update successful');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function studentdelete($id)
{
$Student =Studentview::find($id);
$Student->delete();
return redirect()->route('admin.show')->with('status','data delete successful');
}
}
html code
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<h1 style="text-align:center; ">Add New Student</h1>
<div class="container-fluid" style="margin-left: 150px; margin-right: 50px;">
@if (count($errors) > 0)
<div class = "alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="post" action="{{route('admin.save')}}" enctype="multipart/form-data">
{{@csrf_field()}}
<div class="form-group" class="col-xs-4">
<label for="exampleInputEmail1">Enter the name </label>
<input type="text" name="name" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" style="" value="{{old('name')}}">
<small id="emailHelp" class="form-text text-muted">We'll never share your Name with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">enter the price</label>
<input type="number" class="form-control" id="exampleInputPassword1" name="price" value="{{old('price')}}">
</div>
<div class="form-group">
<label for="image">Choose Image</label>
<input id="image" type="file" name="image">
</div>
<!-- <div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div> -->
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</body>
</html>
route
Route::post('/studentinsert','StudentViewController@studentinsert')->name('admin.save');
0 Comments