Library Management system in Laravel PHP

Library management system is a project which aims in developing a computerized system to maintain all the daily work of a library. The LMS helps both students and library Officer to keep proper track of all the books available in the College library or School Library. It allows both the Librarian and the student to search for the desired book. The main feature of this LMS is that all the books available in the library can be displayed in a list so that students and Teachers need not roam through the entire library to find a book. Additionally, the application effectively maintains the details of users/students to whom books have been issued; it also records the issued date and returns date.

We have used PHP technology for building this LMS Project. We have used the LARAVEL Framework of PHP.As we know that the LARAVEL is the most popular framework among all the frameworks.

Database : Mysql, Server side Technology: PHP ( Laravel ) ,

For Fetching records from the database we are using the Eloquent and Query builder feature of Laravel. We have also used Middleware for login. There are more than 15 tables in MYSQL Database.

This is Adding Book page .

Download the Source code

Now below the sample code of the project StudentController.php

// Code starts here 
<?php
namespace App\Http\Controllers;
use Exception;
use App\Models\Logs;
use App\Models\Books;
use App\Models\Issue;
use App\Models\Branch;
use App\Models\Student;
use Illuminate\Http\Request;
use App\Models\StudentCategories;
use App\Http\Controllers\HomeController;
use Illuminate\Support\Facades\Redirect;
class StudentController extends Controller
{
    public function __construct(){
		$this->filter_params = array('branch','year','category');
	}
	public function index()
	{
		$conditions = array(
			'approved'	=> 0,
			'rejected'	=> 0
		);
		$students = Student::join('branches', 'branches.id', '=', 'students.branch')
		->join('student_categories', 'student_categories.cat_id', '=', 'students.category')
		->select('student_id', 'first_name', 'last_name', 'student_categories.category', 'roll_num', 'branches.branch', 'year')
			->where($conditions)
			->orderBy('student_id');
		// $this->filterQuery($students);
		$students = $students->get();
		// dd($students);
        return $students;
	}
	public function StudentByAttribute(Request $request)
	{
		// dd($request->branch );
		$conditions = array(
			'approved'	=> 0,
			'rejected'	=> 0
		);
		if ($request->branch != 0) {
			
			$students = Student::join('branches', 'branches.id', '=', 'students.branch')
			->join('student_categories', 'student_categories.cat_id', '=', 'students.category')
			->select('student_id', 'first_name', 'last_name', 'student_categories.category', 'roll_num', 'branches.branch', 'year')
				->where($conditions)
				->where('students.branch', $request->branch)
				->orderBy('student_id');
			$students = $students->get();
			return $students;
		
		}
		elseif ($request->category != 0) {
			
			$students = Student::join('branches', 'branches.id', '=', 'students.branch')
			->join('student_categories', 'student_categories.cat_id', '=', 'students.category')
			->select('student_id', 'first_name', 'last_name', 'student_categories.category', 'roll_num', 'branches.branch', 'year')
				->where($conditions)
				->where('students.category', $request->category)
				->orderBy('student_id');
			$students = $students->get();
			return $students;
		
		}
		elseif ($request->year != 0) {
			// dd($request->year );
			$students = Student::join('branches', 'branches.id', '=', 'students.branch')
			->join('student_categories', 'student_categories.cat_id', '=', 'students.category')
			->select('student_id', 'first_name', 'last_name', 'student_categories.category', 'roll_num', 'branches.branch', 'year')
				->where($conditions)
				->where('students.year', $request->year)
				->orderBy('student_id');
			$students = $students->get();
			return $students;
		
		}
		return "No Result Found";
		
	}
	public function create()
	{
		$conditions = array(
			'approved'	=> 1,
			'rejected'	=> 0
		);
		$students = Student::join('branches', 'branches.id', '=', 'students.branch')
		->join('student_categories', 'student_categories.cat_id', '=', 'students.category')
		->select('student_id', 'first_name', 'last_name', 'student_categories.category', 'roll_num', 'branches.branch', 'year', 'email_id', 'books_issued')
			->where($conditions)
			->orderBy('student_id');
		// $this->filterQuery($students);
		$students = $students->get();
        return $students;
	}
	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}
	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$student = Student::find($id);
		if($student == NULL){
			throw new Exception('Invalid Student ID');
		}
		$student->year = (int)substr($student->year, 2, 4);
		$student_category = StudentCategories::find($student->category);
		$student->category = $student_category->category;
		$student_branch = Branch::find($student->branch);
		$student->branch = $student_branch->branch;
		if($student->rejected == 1){
			unset($student->approved);
			unset($student->books_issued);
			$student->rejected = (bool)$student->rejected;
			return $student;
		}
		if($student->approved == 0){
			unset($student->rejected);
			unset($student->books_issued);
			$student->approved = (bool)$student->approved;
			return $student;
		}
		unset($student->rejected);
		unset($student->approved);
		$student_issued_books = Logs::select('book_issue_id', 'issued_at')
			->where('student_id', '=', $id)
			->orderBy('created_at', 'desc')
			->take($student->books_issued)
			->get();
		foreach($student_issued_books as $issued_book){
			$issue = Issue::find($issued_book->book_issue_id);
			$book = Books::find($issue->book_id);
			$issued_book->name = $book->title;
			$issued_book->issued_at = date('d-M', strtotime( $issued_book->issued_at));
		}
		$student->issued_books = $student_issued_books;
		return $student;
	}
	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}
	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update(Request $request, $id){
        $flag = (bool)$request->get('flag');
        $student = Student::findOrFail($id);
		if($flag){
			// if student is approved
	        $student->approved = 1;
		} else {
			// if student is rejected for some reason
			$student->rejected = 1;
		}
        $student->save();
        return "Student's approval/rejection status successfully changed.";
	}
	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy(Request $request, $id)
	{
		// dd($request->all());
		if ($request->category) {
			
			$student = StudentCategories::find($id);
			$student->delete();
			if (!$student) {
				return "Student Category Fail to Delete!.";
			}else {
				return redirect(route('settings'));
			}
		}elseif ($request->branch) {
			
			$branch = Branch::find($id);
			$branch->delete();
			if (!$branch) {
				return "School Branch Fail to Delete!.";
			}else {
				return redirect(route('settings'));
			}
		}
	}
	public function renderStudents(){
		$db_control = new HomeController;
		return view('panel.students')
			->with('branch_list', $db_control->branch_list)
			->with('student_categories_list', $db_control->student_categories_list);
	}
	public function renderApprovalStudents(){
		$db_control = new HomeController;
		return view('panel.approval')
			->with('branch_list', $db_control->branch_list)
			->with('student_categories_list', $db_control->student_categories_list);
	}
	public function getRegistration(){
		$db_control = new HomeController;
		return view('public.registration')
			->with('branch_list', $db_control->branch_list)
			->with('student_categories_list', $db_control->student_categories_list);
	}
	public function postRegistration(Request $request){
		$validator = $request->validate([
				'first'			=> 'required|alpha',
				'last'			=> 'required|alpha',
				'rollnumber'	=> 'required|integer',
				'branch'		=> 'required|between:0,10',
				'year'			=> 'required|integer',
				'email'			=> 'required|email',
				'category'		=> 'required|between:0,5'
		]);
		if(!$validator) {
			return Redirect::route('student-registration')
				->withErrors($validator)
				->withInput();   // fills the field with the old inputs what were correct
		} else {
			$student = Student::create(array(
				'first_name'	=> $request->get('first'),
				'last_name'		=> $request->get('last'),
				'category'		=> $request->get('category'),
				'roll_num'		=> $request->get('rollnumber'),
				'branch'		=> $request->get('branch'),
				'year'			=> $request->get('year'),
				'email_id'		=> $request->get('email'),
			));
			if($student){
				return Redirect::route('student-registration')
					->with('global', 'Your request has been raised, you will be soon approved!');
			}
		}
	}
	public function Setting()
	{
		$branches = Branch::all();
		$student_category = StudentCategories::all();
		return view('panel.addsettings')
		->with('branches', $branches)
		->with('student_category', $student_category);
	}
	public function StoreSetting(Request $request)
	{
		// dd($request->all());
		if ($request->category) {
			
			$student = StudentCategories::create($request->all());
			if (!$student) {
				return "Student Category Fail to Save!.";
			}else {
				return "Student Category Save Succesfully!.";
				// return back();
			}
		}elseif ($request->branch) {
			
			$branch = Branch::create($request->all());
			if (!$branch) {
				return "School Branch Fail to Save!.";
			}else {
				return "School Branch Save Succesfully!.";
				// return back();
			}
		}
		
	}
}

The below file is LogController.php . This controller is for login page

// Code start her e
<?php
namespace App\Http\Controllers;
use App\Models\Logs;
use App\Models\Books;
use App\Models\Issue;
use App\Models\Student;
use Illuminate\Http\Request;
use App\Models\StudentCategories;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Exception;
class LogController extends Controller
{
    public function index()
	{
		$logs = Logs::select('id','book_issue_id','student_id','issued_at')
			->where('return_time', '=', 0)
			->orderBy('issued_at', 'DESC');
			// dd($logs);
		
		$logs = $logs->get();
		for($i=0; $i<count($logs); $i++){
	        
	        $issue_id = $logs[$i]['book_issue_id'];
	        $student_id = $logs[$i]['student_id'];
	        
	        // to get the name of the book from book issue id
	        $issue = Issue::find($issue_id);
	        $book_id = $issue->book_id;
	        $book = Books::find($book_id);
			$logs[$i]['book_name'] = $book->title;
			// to get the name of the student from student id
			$student = Student::find($student_id);
			$logs[$i]['student_name'] = $student->first_name . ' ' . $student->last_name;
			// change issue date and return date in human readable format
			$logs[$i]['issued_at'] = date('d-M', strtotime($logs[$i]['issued_at']));
			if ($issue->return_time == 0) {
				$logs[$i]['return_time'] =  '<p class="color:red">Pending</p>';
			}else {
				$logs[$i]['return_time'] = date('d-M', strtotime($logs[$i]['return_time']));
			}
		}
        return $logs;
	}
	public function create()
	{
		//
	}
	public function store(Request $request)
	{
		$data = $request->all()['data'];
		$bookID = $data['bookID'];
		$studentID = $data['studentID'];
		
		$student = Student::find($studentID);
		
		if($student == NULL){
			return "Invalid Student ID";
		} else {
			$approved = $student->approved;
			
			if($approved == 0){
				return "Student still not approved by Admin Librarian";
				// throw new Exception('');
			} else {
				$books_issued = $student->books_issued;
				$category = $student->category;
				
				$max_allowed = StudentCategories::where('cat_id', '=', $category)->firstOrFail()->max_allowed;
				
				if($books_issued >= $max_allowed){
					return 'Student cannot issue any more books';
				} else {
					$book = Issue::where('book_id', $bookID)->where('available_status', '!=', 0)->first();
					if($book == NULL){
						return 'Invalid Book Issue ID';
					} else {
						$book_availability = $book->available_status;
						// dd($book);
						if($book_availability != 1){
							return 'Book not available for issue';
						} else {
							// book is to be issued
							DB::transaction( function() use($bookID, $studentID) {
								$log = new Logs;
								$log->book_issue_id = $bookID;
								$log->student_id	= $studentID;
								$log->issue_by		= Auth::id();
								$log->issued_at		= date('Y-m-d H:i');
								$log->return_time	= 0;
								$log->save();
								$book = Issue::where('book_id', $bookID)->where('available_status', '!=', 0)->first();
								// changing the availability status
								$book_issue_update = Issue::where('book_id', $bookID)->where('issue_id', $book->issue_id)->first();
								$book_issue_update->available_status = 0;
								$book_issue_update->save();
								// increasing number of books issed by student
								$student = Student::find($studentID);
								$student->books_issued = $student->books_issued + 1;
								$student->save();
							});
							return 'Book Issued Successfully!';
						}
					}
				}
			}
		}
	}
	public function show($id)
	{
		//
	}
	public function edit($id)
	{
		$issueID = $id;
		$conditions = array(
			'book_issue_id'	=> $issueID,
			'return_time'	=> 0
		);
		$log = Logs::where($conditions);
		if(!$log->count()){
			return 'Invalid Book ID entered or book already returned';
		} else {
		
			$log = Logs::where($conditions)
				->firstOrFail();
			$log_id = $log['id'];
			$student_id = $log['student_id'];
			$issue_id = $log['book_issue_id'];
			DB::transaction( function() use($log_id, $student_id, $issue_id) {
				// change log status by changing return time
				$log_change = Logs::find($log_id);
				$log_change->return_time = date('Y-m-d H:i');
				$log_change->save();
				// decrease student book issue counter
				$student = Student::find($student_id);
				$student->books_issued = $student->books_issued - 1;
				$student->save();
				// change issue availability status
				$issue = Issue::find($issue_id);
				$issue->available_status = 1;
				$issue->save();
				
			});
			return 'Successfully returned';
			
		}
	}
	public function update($id)
	{
		//
	}
	public function destroy($id)
	{
		//
	}
    public function renderLogs() {
        return view('panel.logs');
    }
    public function renderIssueReturn() {
        return view('panel.issue-return');
    }
}

Download the Source Code : Library Management System

Read our More Articles

Crop and Upload Image in Laravel

Signup using REST API in Laravel

Laravel Redirect to URL

Netflix clone app using Laravel

Comments 2

Leave a Reply

Your email address will not be published. Required fields are marked *