0% found this document useful (0 votes)
17 views14 pages

Laravel Note

This document provides a step-by-step guide for creating a Laravel project, including setting up the environment, creating models, controllers, and implementing CRUD (Create, Read, Update, Delete) functionalities. It details the necessary commands and code snippets for each operation, as well as how to set up routes and views using Blade templates. The document also includes instructions for running the project and generating application keys.

Uploaded by

Shine Lynn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views14 pages

Laravel Note

This document provides a step-by-step guide for creating a Laravel project, including setting up the environment, creating models, controllers, and implementing CRUD (Create, Read, Update, Delete) functionalities. It details the necessary commands and code snippets for each operation, as well as how to set up routes and views using Blade templates. The document also includes instructions for running the project and generating application keys.

Uploaded by

Shine Lynn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Contents

Requirements
Create larvel project
Create model and database
Create Controller
Create (C)
Read (R)
Update (U)
Delete (D)
Route
PHP Laravel framework
Requirement

Xampp
https://www.apachefriends.org/download.html
or
laragon

https://laragon.org/download/
Composer
https://getcomposer.org/download/

Node js
PHP

Run Xampp
Create larvel project

1. composer global require laravel/installer


2. laravel new “name”

Set up .env file


bash
npm install

npm run dev

Create model and database


1. Ctrl+` (open terminal)
2. php artisan make:model "modle name" –m

3. new files are created at database/migrations


and app/Models
4. add require column at database/migratons/filename

e.g.
5. run php artisan migrate at terminal
you will see the posts table in your database

Create Controller (Resource Controller)


1. php artisan make:controller "controllr_name" –r

New files is created at app/HTTP/Controllers


2. In the controller, you need to write functions for creating,
updating, deleting, and viewing.
C Create function

1. Function name is store ()


2. Request $request: A dependency injection of the Request
class, which provides access to the HTTP request data,
such as form inputs, query parameters, and files.
3. $request->validate ([]): This method validates the
incoming request data based on the provided rules.
4. Post::create ([]): This method creates a new record in the
posts database table.

create.blade.php (resources/views/layouts)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Create</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min
.css" rel="stylesheet" integrity="sha384-
QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">
</head>
<body>

<h1 class="text-center">create</h1>
<form action="{{ route('posts.store')}}" class="ms-5" method="POST">
@csrf
<label for="title">Title</label><br>
<input type="text" name="title" class="mt-4 mb-3 @error('title')
is-invalid @enderror"><br>

@error('title')
<div class="invalid-feedback">{{ $message }}</div>
@enderror

<label for="content mt-4">Content</label><br>


<textarea name="content" class="mt-4 @error('content') is-
invalid @enderror" row="4"></textarea>

@error('content')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
<button class="btn btn-primary mt-4">Submit</button>
</form>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundl
e.min.js" integrity="sha384-
YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
</body>
</html>

R Read Function
public function index()
{
$posts = Post::all();
return view('layouts.list', compact('posts'));
}
Function name is index()
Post::all() read all data from posts table
list.blade.php (resources/views/layouts)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>List</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min
.css" rel="stylesheet" integrity="sha384-
QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">

</head>
<body>

<a href="{{route ('posts.create')}}">


<button class="btn btn-primary float-right mb-2">New</button>
</a>
<table class="table">
<thead>
<th>id</th>
<th>title</th>
<th>content</th>
<th>action</th>
</thead>
<tbody>
@foreach($posts as $p)
<tr>
<td>{{$p -> id}}</td>
<td>{{$p -> title}}</td>
<td>{{$p -> content}}</td>
<td>
<form action="{{route ('posts.destroy', $p-> id)}}"
method="POST">
@csrf
@method('DELETE')
<a href="{{route ('posts.edit', $p->id)}}" class="btn btn-
primary">Edit</a>
<button class="btn btn-danger" type="submit">Delete</a>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>

<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundl
e.min.js" integrity="sha384-
YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
</body>
</html>

U update function
public function update(Request $request, $id)
{
$request->validate([
'title' => 'required',
'content' => 'required'
]);

Post::find($id)-> update([
'title' => $request->title,
'content'=> $request->content
]);
return redirect('/posts');
}

Update by id
So you need parameters
$id is post->id

Post::find($id)-> update

Fine the id in the posts table and update

edit.blade.php (resources/views/layouts)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Create</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min
.css" rel="stylesheet" integrity="sha384-
QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">
</head>
<body>

<h1 class="text-center">create</h1>
<form action="{{ route('posts.update, $post->id)}}" class="ms-5"
method="POST">
@csrf
@method('PUT')
<label for="title">Title</label><br>
<input type="text" name="title" class="mt-4 mb-3 @error('title')
is-invalid @enderror" value="{{ $post -> title}}"><br>

@error('title')
<div class="invalid-feedback">{{ $message }}</div>
@enderror

<label for="content mt-4">Content</label><br>


<textarea name="content" class="mt-4 @error('content') is-
invalid @enderror" row="4">{{ $post -> content}}</textarea>

@error('content')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
<button class="btn btn-primary mt-4">Submit</button>
</form>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundl
e.min.js" integrity="sha384-
YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
</body>
</html>

D Delete function
public function destroy($id)
{
Post::find($id)->delete();
return redirect('/posts');
}

Function name id destroy


Post::find($id)->delete();
Find the id in the posts table and delete it
Create route
Route/web.php

Route::resource('posts',PostController::class);

If you want to find the routes, use this command.


php artisan route:list

Run the project with


php artisan serve
Ctrl + click the [http://……]

Run this
php artisan key:generate

You might also like