La एकल पृष्ठ अनुप्रयोग हामीलाई DB मा आधारभूत कार्यहरूको चक्र पूरा गर्न अनुमति दिनेछ: सिर्जना गर्नुहोस्, पढ्नुहोस्, अद्यावधिक गर्नुहोस् र मेटाउनुहोस् Vue.js , Vue राउटर र लारावेल फ्रेमवर्क .
आजकल, सबैभन्दा लोकप्रिय JS फ्रेमवर्कहरू Angular JS र Vue JS हुन्। Angular JS र Vue JS धेरै प्रयोगकर्ता-अनुकूल र सबैभन्दा लोकप्रिय JS फ्रेमवर्कहरू हुन्। यसले पृष्ठ र शक्तिशाली jquery प्रमाणीकरण बिना नै सम्पूर्ण परियोजना वा अनुप्रयोगको व्यवस्थापन प्रदान गर्दछ।
Vue Laravel (सँगै लारावेल मिक्स , आधारित उत्कृष्ट संलेखन उपकरण वेबप्याक ) र विकासकर्ताहरूलाई ट्रान्सपाइलरहरू, कोड प्याकेजहरू, स्रोत नक्साहरू वा आधुनिक फ्रन्टएन्ड विकासको अन्य "फोहोर" पक्षहरूको चिन्ता नगरी जटिल एकल पृष्ठ अनुप्रयोगहरू निर्माण गर्न सुरु गर्न अनुमति दिन्छ।
Php १7.4,०००
Laravel 8.x
MySQL
पहिले, टर्मिनल खोल्नुहोस् र नयाँ laravel परियोजना सिर्जना गर्न निम्न आदेश चलाउनुहोस्:
composer create-project --prefer-dist laravel/laravel crud-spa
वा, यदि तपाईंले कम्पोजर विश्वव्यापी निर्भरताको रूपमा Laravel Installer स्थापना गर्नुभयो भने:
laravel new crud-spa
स्थापना पछि आफ्नो प्रोजेक्ट रूट डाइरेक्टरीमा जानुहोस्, .env फाइल खोल्नुहोस् र निम्न अनुसार डाटाबेस विवरणहरू सेट गर्नुहोस्:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=<DATABASE NAME>
DB_USERNAME=<DATABASE USERNAME>
DB_PASSWORD=<DATABASE PASSWORD>
अगाडि-अन्त निर्भरताहरू स्थापना गर्न निम्न आदेश चलाउनुहोस्:
npm install
त्यस पछि, स्थापना गर्नुहोस् दृश्य , vue-राउटर e vue-axios । Vue-axios Laravel ब्याकएन्ड API कल गर्न प्रयोग गरिनेछ। टर्मिनलमा निम्न आदेश चलाउनुहोस्:
npm install vue vue-router vue-axios --save
कोटी टेम्प्लेट, माइग्रेसन र नियन्त्रक सिर्जना गर्नुहोस्। यसको लागि निम्न आदेश चलाउनुहोस्:
php artisan make:model Category -mcr
-mcr यो विषयले एकल आदेश संश्लेषण प्रयोग गरेर मोडेल, माइग्रेसन र नियन्त्रक सिर्जना गर्नेछ।
अब, बाट श्रेणी माइग्रेसन फाइल खोल्नुहोस् डाटाबेस / माइग्रेसन र प्रकार्यमा कोड बदल्नुहोस् माथि() :
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('description');
$table->timestamps();
});
}
निम्न आदेश प्रयोग गरेर डाटाबेस माइग्रेट गर्नुहोस्:
php artisan migrate
अब, Category.php बाट टेम्प्लेट खोल्नुहोस्app/Models
र फाइलमा कोड सम्पादन गर्नुहोस् model
Category.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model {
use HasFactory;
protected $fillable = ['title','description'];
}
?>
त्यस पछि, खोल्नुहोस् CategoryController.php र अनुक्रमणिकामा कोड थप्नुहोस्, भण्डार गर्नुहोस्, अद्यावधिक गर्नुहोस् र निम्न तरिकाहरू मेटाउनुहोस्:
<?php
namespace App\Http\Controllers;
use App\Models\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$categories = Category::all(['id','title','description']);
return response()->json($categories);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$category = Category::create($request->post());
return response()->json([
'message'=>'Category Created Successfully!!',
'category'=>$category
]);
}
/**
* Display the specified resource.
*
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function show(Category $category)
{
return response()->json($category);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Category $category)
{
$category->fill($request->post())->save();
return response()->json([
'message'=>'Category Updated Successfully!!',
'category'=>$category
]);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function destroy(Category $category)
{
$category->delete();
return response()->json([
'message'=>'Category Deleted Successfully!!'
]);
}
}
Ora defiतिनीहरूलाई समाप्त गर्नुहोस् routes
फाइलहरूमा web.php e api.php । फोल्डरमा जानुहोस् routes
र web.php र api.php फाइल खोल्नुहोस् र निम्न अपडेट गर्नुहोस् routes
:
routes/web.php
<?php
Route::get('{any}', function () {
return view('app');
})->where('any', '.*');
routes/api.php
<?php
Route::resource('category',App\Http\Controllers\CategoryController::class)->only(['index','store','show','update','destroy']);
यस चरणमा, डाइरेक्टरीमा जानुहोस् स्रोत/दृश्य, फाइल सिर्जना गर्नुहोस् app.blade.php र निम्न कोड थप्नुहोस् app.blade.php:
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" value="{{ csrf_token() }}"/>
<title>Laravel Vue CRUD App</title>
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet" type="text/css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<link href="{{ mix('css/app.css') }}" type="text/css" rel="stylesheet"/>
</head>
<body>
<div id="app">
</div>
<script src="{{ mix('js/app.js') }}" type="text/javascript"></script>
</body>
</html>
यस चरणमा, फोल्डरमा जानुहोस् resource/js
, फोल्डर सिर्जना गर्नुहोस् components
र निम्न रूपमा फाइलहरू सिर्जना गर्नुहोस्:
View app
Welcome.vue
Category/List.vue
Category/Add.vue
Category/Edit.vue
एप। vue यो हाम्रो Vue एपको मुख्य फाइल हो। Defiहामी समाप्त गर्नेछौं राउटर-दृश्य त्यो फाइलमा। सबै पथहरू फाइलमा देखा पर्नेछ एप। vue .
फाइल खोल्नुहोस् Welcome.vue
र त्यो फाइलमा निम्न कोड अपडेट गर्नुहोस्:
<template>
<div class="container mt-5">
<div class="col-12 text-center">
<h1>Laravel Vuejs CRUD</h1>
</div>
</div>
</template>
App.vue फाइल खोल्नुहोस् र निम्नानुसार कोड अपडेट गर्नुहोस्:
<template>
<main>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<router-link to="/" class="navbar-brand" href="#">Laravel Vue Crud App</router-link>
<div class="collapse navbar-collapse">
<div class="navbar-nav">
<router-link exact-active-class="active" to="/" class="nav-item nav-link">Home</router-link>
<router-link exact-active-class="active" to="/category" class="nav-item nav-link">Category</router-link>
</div>
</div>
</div>
</nav>
<div class="container mt-5">
<router-view></router-view>
</div>
</main>
</template>
<script>
export default {}
</script>
त्यसपछि, खोल्नुहोस् resource / js / components / category / List.vue
र फाइलमा निम्न कोड थप्नुहोस्:
<template>
<div class="row">
<div class="col-12 mb-2 text-end">
<router-link :to='{name:"categoryAdd"}' class="btn btn-primary">Create</router-link>
</div>
<div class="col-12">
<div class="card">
<div class="card-header">
<h4>Category</h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody v-if="categories.length > 0">
<tr v-for="(category,key) in categories" :key="key">
<td>{{ category.id }}</td>
<td>{{ category.title }}</td>
<td>{{ category.description }}</td>
<td>
<router-link :to='{name:"categoryEdit",params:{id:category.id}}' class="btn btn-success">Edit</router-link>
<button type="button" @click="deleteCategory(category.id)" class="btn btn-danger">Delete</button>
</td>
</tr>
</tbody>
<tbody v-else>
<tr>
<td colspan="4" align="center">No Categories Found.</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name:"categories",
data(){
return {
categories:[]
}
},
mounted(){
this.getCategories()
},
methods:{
async getCategories(){
await this.axios.get('/api/category').then(response=>{
this.categories = response.data
}).catch(error=>{
console.log(error)
this.categories = []
})
},
deleteCategory(id){
if(confirm("Are you sure to delete this category ?")){
this.axios.delete(`/api/category/${id}`).then(response=>{
this.getCategories()
}).catch(error=>{
console.log(error)
})
}
}
}
}
</script>
त्यस पछि, खोल्नुहोस् resource /js/components/category/Add.vue
र फाइलमा निम्न कोड अपडेट गर्नुहोस्:
<template>
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h4>Add Category</h4>
</div>
<div class="card-body">
<form @submit.prevent="create">
<div class="row">
<div class="col-12 mb-2">
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" v-model="category.title">
</div>
</div>
<div class="col-12 mb-2">
<div class="form-group">
<label>Description</label>
<input type="text" class="form-control" v-model="category.description">
</div>
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name:"add-category",
data(){
return {
category:{
title:"",
description:""
}
}
},
methods:{
async create(){
await this.axios.post('/api/category',this.category).then(response=>{
this.$router.push({name:"categoryList"})
}).catch(error=>{
console.log(error)
})
}
}
}
</script>
त्यस पछि, खोल्नुहोस् resource /js/components/category/Edit.vue
र फाइलमा निम्न कोड अपडेट गर्नुहोस्:
<template>
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h4>Update Category</h4>
</div>
<div class="card-body">
<form @submit.prevent="update">
<div class="row">
<div class="col-12 mb-2">
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" v-model="category.title">
</div>
</div>
<div class="col-12 mb-2">
<div class="form-group">
<label>Description</label>
<input type="text" class="form-control" v-model="category.description">
</div>
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name:"update-category",
data(){
return {
category:{
title:"",
description:"",
_method:"patch"
}
}
},
mounted(){
this.showCategory()
},
methods:{
async showCategory(){
await this.axios.get(`/api/category/${this.$route.params.id}`).then(response=>{
const { title, description } = response.data
this.category.title = title
this.category.description = description
}).catch(error=>{
console.log(error)
})
},
async update(){
await this.axios.post(`/api/category/${this.$route.params.id}`,this.category).then(response=>{
this.$router.push({name:"categoryList"})
}).catch(error=>{
console.log(error)
})
}
}
}
</script>
अब, तपाईंले गर्नुपर्छ defiसमाप्त गर्नुहोस् Vue routes
, र यो गर्न फोल्डरमा जानुहोस् resource / js
, फाइल सिर्जना गर्नुहोस् route.js
र फाइलमा निम्न कोड अपडेट गर्नुहोस्:
const Welcome = () => import('./components/Welcome.vue' /* webpackChunkName: "resource/js/components/welcome" */)
const CategoryList = () => import('./components/category/List.vue' /* webpackChunkName: "resource/js/components/category/list" */)
const CategoryCreate = () => import('./components/category/Add.vue' /* webpackChunkName: "resource/js/components/category/add" */)
const CategoryEdit = () => import('./components/category/Edit.vue' /* webpackChunkName: "resource/js/components/category/edit" */)
export const routes = [
{
name: 'home',
path: '/',
component: Welcome
},
{
name: 'categoryList',
path: '/category',
component: CategoryList
},
{
name: 'categoryEdit',
path: '/category/:id/edit',
component: CategoryEdit
},
{
name: 'categoryAdd',
path: '/category/add',
component: CategoryCreate
}
]
यहाँ हामीले कम्पोनेन्टहरू प्रयोग गरेका छौं ढिलो लोडिङ. भ्यू जेएस कम्पोनेन्टको लोडिङलाई एक तरिकाले व्यवस्थापन गर्छ lazy
पथहरूसँग, त्यसैले DOM मा तपाईंले कम्पोनेन्टहरू लोड गर्न सक्नुहुन्छ जब तिनीहरू पथहरू मार्फत आवश्यक हुन्छ।
अब तपाईंले सबै मार्गहरू, vue-axios र अन्य निर्भरताहरू थप्न आवश्यक छ।
resource / js / app.js
require('./bootstrap');
import vue from 'vue'
window.Vue = vue;
import App from './components/App.vue';
import VueRouter from 'vue-router';
import VueAxios from 'vue-axios';
import axios from 'axios';
import {routes} from './routes';
Vue.use(VueRouter);
Vue.use(VueAxios, axios);
const router = new VueRouter({
mode: 'history',
routes: routes
});
const app = new Vue({
el: '#app',
router: router,
render: h => h(App),
});
webpack.mix.js
const mix = require('laravel-mix');
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel applications. By default, we are compiling the CSS
| file for the application as well as bundling up all the JS files.
|
*/
mix.js('resources/js/app.js', 'public/js')
.postCss('resources/css/app.css', 'public/css', [
//
]).vue();
टर्मिनल खोल्नुहोस् र यो आदेश चलाउनुहोस्:
npm run watch
php artisan serve
अप्रिल स्थानीय होस्ट: 8000 XNUMX। ० ब्राउजरमा।
BlogInnovazione.it
गत सोमबार, फाइनान्सियल टाइम्सले OpenAI सँग सम्झौताको घोषणा गर्यो। FT ले आफ्नो विश्व स्तरीय पत्रकारिता लाई लाइसेन्स...
लाखौं मानिसहरूले स्ट्रिमिङ सेवाहरूको लागि भुक्तानी गर्छन्, मासिक सदस्यता शुल्क तिर्छन्। यो आम धारणा छ कि तपाईं…
Veeam द्वारा Coveware ले साइबर जबरजस्ती घटना प्रतिक्रिया सेवाहरू प्रदान गर्न जारी राख्नेछ। Coveware ले फोरेन्सिक र उपचार क्षमताहरू प्रदान गर्दछ ...
अनुमानित मर्मतसम्भारले तेल र ग्यास क्षेत्रमा क्रान्तिकारी परिवर्तन गर्दैछ, बिरुवा व्यवस्थापनको लागि एक नवीन र सक्रिय दृष्टिकोणको साथ।…