Laravel 5.8 CRUD Tutorial with examples

Create Route for View:

Syntax: Route::get('uri', function(){return view('view_name')});

To write it in a simpler way:

Syntax:- Route::view method

Example:- Route::view(‘about’,’aboutme’)

-We should create view in structured way(inside folder) that means all the views should not be in the view folder and we can make it in structured way by making folder in views.

-To create route for view which is inside folder:

Syntax:- Route::get(‘uri’, Function(){
return view(‘folder_name.view_name’)});

Example:- Route::get(‘aboutprofile’, Function(){return view(‘admin.profile’)});

-Passing data from Route to view:

*Data should be in array and in key value/pair.

*Inside the view, we can then access each value using its corresponding key.

Example 1:-

Route::get(‘contact’, function(){
Return view(‘contactme’,[‘name’=>’Sonam’]);
});

 *We can also use the with method to add individual pieces of date to the view.

Example:-

Route::get(‘contact’, function(){
return view(‘contactme’)->with(‘name’,’Sonam’)
});

-Accessing the data which is passed from Route to view:

<h1>Hello{{$name}}</h1>

-Returning view from web.php using parameter:

Route::get(‘user/(u_id}’, function($id){
Return view(‘myuser’,[‘id’=>$id]);
});

(In the User View)

<body>
<h1>User page </h1>
<h1>{{$id123}}</h1>
</body>

-For multiple parameter:

(In web.php)

Route::get(‘post/{post_id}/comment/{comment_id}’, function($post_id, $comment_id){
Return view(‘mypost’,[‘postid’=>$post_id, ‘commentid=>’$comment_id’]);
});

(In the Post View)

<body>
<h1>User page </h1>
<h1>Post Id:{{$postid}}</h1>
<h1>Comment Id:{{$commentid}}</h1>
</body>

-For making optional parameter:

(In web.php)

Route::get(‘user/(name?}’, function($name=null){
Return view(‘myuser’,[‘name’=>$name]);

(In User view)

<body>
<h1>User page </h1>
</body>

For calling this in the server we can type 127.0.0.1.8000 or 127.0.0.1.8000/name and it will display the content in both scenario without giving error. Though it will not return any name if we do not enter /name.

-For regular expression

(In web.php)

Route::get(‘product/{p_name}’, function($name){
Return view(‘myproduct’,[name’=>$name]);
})->where(‘p_name’=>‘[A-za-z]+’);

(In myproduct view file)

<body>
<h1>Product page </h1>
<h1>{{$name}}</h1>
</body>

To redirect from one view to another view:

(In web.php)

Route::view(‘login’, ‘mylogin’);
Route::view(‘register’,’myregister’)
Route::redirect(‘login’, ‘register’);

(In View file)

<body>
<h1>Login</h1>
</body>

To fallback:

(In web.php)

Route::fallback(function(){
Return view(‘default;);
});

In Default View:

<body>
<h1>Default</h1>
</body>

Controllers

 Controllers can group related request handling logic into a single class. Instead of defining all of your request handling logic as closures in route files, you may wish to organize this behaviour using controller classes.

– Controllers are stored in the app/Http/Controllers

-Controller extends the base controller class included with Laravel.

Defining Controller Class

Run command: php artisan make:controller controller_name

Example: php artisan make:controller AboutController

Creating Route for Controller Class

(In AboutController.php)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
	Function show(){
		Return ‘Hello Controller’;}
}

(In Web.php)

Use App\Http\Controllers\AboutControllers;

Route::get(‘about’,[AboutController::class,’show’]);

Syntax:

Route::get(‘URI’,[ControllerName::class,’method_name’]);

Getting parameter in Controller

In AboutController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
	Function show(){
		Return ‘Hello Controller’,$name;}
}

In web.php

Use App\Http\Controllers\AboutController;

Route::get(‘about/{name}’),[AboutController::class,’show’]);

To import controller in web.php

Use App\Http\Controllers\AboutController;

Controller returning string with parameter

In AboutController

function show($name){
return “Hello Controller”.$name;

In web.php

Route::get(‘about/{name}’,[Aboutcontroller::class,’show’]};

Returning View from Controller Class

In AboutControllers

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
	function show(){
		return view(‘Hello Controller’);}
}

In web.php

Use App\Http\Controllers\AboutController;

Route::get(‘about’,[AboutController::class,’show’]);

In Views:

<body>
<h1>About my controller</h1>
</body>

Joydeep M
Latest posts by Joydeep M (see all)
Tagged : / / / /
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x