If the form doesn't fill all fields on submission, it will not execute further.
For Laravel, we just have to call the paginate method by passing number of records per page. it will automatically do everything for you:-
$query=Cms::select('*');
$query->where('status','=','1');
if($keyword!=''){
$query->where('title', 'LIKE', "%{$keyword}%");
$query->orWhere('page_title','LIKE', "%{$keyword}%");
}
$records=$query->paginate(10);
Here am Just explaining a scenario case, suppose you want to paginate a list of pages, which will be having keywords search also. we will do like this.We have to pass number of records per page into paginate function.
In View you just have to put:-
<?php echo $pages->links();?>
So a pagination, which is styled in boostrap will appear.
11) LOADING VIEWS IN THE CONTROLLER FUNCTIONS
You can load views by calling:-
return view('cms.list', $data);
Here views are stored in resources/app/views folder
So by calling this laravel will pick list.blade.php file stored in cms folder inside views.
12) BLADE TEMPLATING IN LARAVEL
In Laravel view files will be saved as extensions blade.php. This is laravels own templating language.
i) For Loading css and js
All Css, js, images are stored in public folder. So inorder to access from it put like:-
<link rel="stylesheet" href="{{asset('theme')}}/assets/css/bootstrap.min.css" />
Here boostrap.min.css is stored in public/theme/assets/css folder.
You can use same to access all the css, js, image files stored in public folder.
2)For accessing Images stored in Uploads folder
<img src="{{ URL::to('/') }}/uploads/image.jpg" width="150px" height="150px" >
3) Extending a main layout in your view File
@extends('layouts.main')
Our Theme will be having a normal layout for some pages and its content part will be changing for most pages. so in this case we can define a main layout file in views folder and extend it in our view file. here the view is extending main.blade.php stored in layouts folder in the view folder.
4)Defining Sections in the layout Files
You can define sections in layout file and include it in the place you want it to appear. This is used when u want to include a common sidebar. but it have to appear between the dynamic view pages.
so define sections like:-
// Design code here
@endsection
then call yield to display where you want:-
@yield('content')
This you can put in the main layout file, so the section content will be replaced in it while including the same.
5) Including template file
You can also include a theme file into the template using the syntax:-
@include('layouts.messagebar')
Here the messagebar.blade.php defined in the layouts folder will be included.
13) IMAGE UPLOADING IN LARAVEL
There are many functions inbuilt in laravel for managing the image uploads.
if ($request->hasFile('txtFile') && $request->file('txtFile')->isValid()) {
//$request->hasFile('txtFile') -this function checks, whether the //file has been submitted from the form.
//$request->file('txtFile')->isValid()- this function checks, whether //the file submitted is valid.
//receiving file
$file = $request->file('txtFile');
//getting file values
$fileorgname = $file->getClientOriginalName();
$fileextension = $file->getClientOriginalExtension();
$filerealpath = $file->getRealPath();
$filesize = $file->getSize();
$filememetype = $file->getMimeType();
// defining allowed extensions
$allowedextensions=array('jpg','jpeg','png','bmp','gif');
//Checking whether uploaded file extension is matching the allowed extensions
if (in_array($fileextension,$allowedextensions))
{
// Getting the stored name and defining the path uploads in public folder to upload the file
$imagename = $request->txtFile->store('uploads');
$imagename=str_replace('uploads/','',$imagename);
$destinationPath = 'uploads';
//Storing the file to uploads folder
$file->move($destinationPath, $imagename);
}
}
14)CRUD OPERATIONS IN LARAVEL
INSERT
$insertdata = array('title'=>$name,'content'=>$content',banner'=>$imagename)
Cms::create($insertdata);
Here already CMs model has been loaded and by initiating Cms model and passing the array to create function, the data will be inserted.
UPDATE
ORM
$insertdata = array('title'=>$name,'content'=>$content',banner'=>$imagename)
Cms::where('page_id',$id)->update($insertdata);
Query Builder
DB::table('cms_pages')->where('page_id', $id)->update(['published' => 0]);
Here calling cms model and passing id and update array we can update records.
RETRIEVING
We can retrieve a single record or a group of records like this:-
ORM
$record= Cms::where('page_id',$id)->first();
QUERY Builder
For getting all:-
$rec= DB::table('cms_pages')->get();
For retrieving with condition:-
$rec=DB::table('cms_pages')->where('level', 1)->get();
DELETING
QUERY Builder Function
DB::table('cms_pages')->where('page_id', '=',$list[$i])->delete();
15) SESSIONS and FLASH Messages in Laravel
After saving data, if you want to show success or failure messages, the flash method in session will be handy.
we can set a flash variable in session like:-
$request->session()->flash('msg', 'Selected Items Deleted!!');
We can check, whether that msg variable is set by calling:-
if ($request->session()->has('msg')) {
}
For accessing the msg variable we can call like:-
$data['errormsg']=$request->session()->get('msg');