La સિંગલ પેજ એપ્લિકેશન અમને DB પર મૂળભૂત કામગીરીના ચક્રને પૂર્ણ કરવાની મંજૂરી આપશે: બનાવો, વાંચો, અપડેટ કરો અને કાઢી નાખો Vue.js , Vue રાઉટર્સ અને લારેવેલ ફ્રેમવર્ક .
આજકાલ, સૌથી વધુ લોકપ્રિય JS ફ્રેમવર્ક કોણીય JS અને Vue JS છે. કોણીય JS અને Vue JS ખૂબ જ વપરાશકર્તા મૈત્રીપૂર્ણ અને સૌથી વધુ લોકપ્રિય JS ફ્રેમવર્ક છે. તે પૃષ્ઠને તાજું કર્યા વિના અને શક્તિશાળી jquery માન્યતા વિના સમગ્ર પ્રોજેક્ટ અથવા એપ્લિકેશનનું સંચાલન પ્રદાન કરે છે.
Vue Laravel સાથે પ્રી-પેકેજ આવે છે (સાથે લારાવેલ મિક્સ , પર આધારિત ઉત્તમ ઓથરિંગ ટૂલ વેબપેક ) અને વિકાસકર્તાઓને ટ્રાન્સપિલર્સ, કોડ પેકેજો, સ્ત્રોત નકશા અથવા આધુનિક ફ્રન્ટ એન્ડ ડેવલપમેન્ટના અન્ય "ગંદા" પાસાઓ વિશે ચિંતા કર્યા વિના જટિલ સિંગલ પેજ એપ્લિકેશન્સ બનાવવાનું શરૂ કરવાની મંજૂરી આપે છે.
પીએચપી 7.4
લારાવેલ 8.x
MySQL
પ્રથમ, ટર્મિનલ ખોલો અને નવો laravel પ્રોજેક્ટ બનાવવા માટે નીચેનો આદેશ ચલાવો:
composer create-project --prefer-dist laravel/laravel crud-spa
અથવા, જો તમે કંપોઝર ગ્લોબલ ડિપેન્ડન્સી તરીકે લારાવેલ ઇન્સ્ટોલર ઇન્સ્ટોલ કર્યું છે:
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
-એમસીઆર આ વિષય સિંગલ કમાન્ડ સિન્થેસિસનો ઉપયોગ કરીને મોડલ, માઈગ્રેશન અને કંટ્રોલર બનાવશે.
હવે, માંથી કેટેગરી સ્થળાંતર ફાઈલ ખોલો ડેટાબેઝ / સ્થળાંતર અને કોડને ફંક્શનમાં બદલો ઉપર() :
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!!'
]);
}
}
ઓરા 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 બ્રાઉઝરમાં.
BlogInnovazione.it
કોઈપણ વ્યવસાય કામગીરી વિવિધ સ્વરૂપોમાં પણ ઘણો ડેટા ઉત્પન્ન કરે છે. એક્સેલ શીટમાંથી આ ડેટાને મેન્યુઅલી દાખલ કરો...
2024ના પ્રથમ ત્રણ મહિનામાં કંપનીના ઈમેઈલનું સમાધાન છેલ્લા ત્રિમાસિક ગાળાની સરખામણીમાં બમણાથી વધુ વધ્યું છે.
ઇન્ટરફેસ સેગ્રિગેશનનો સિદ્ધાંત ઑબ્જેક્ટ-ઓરિએન્ટેડ ડિઝાઇનના પાંચ સોલિડ સિદ્ધાંતોમાંથી એક છે. વર્ગમાં હોવું જોઈએ...
માઈક્રોસોફ્ટ એક્સેલ એ ડેટા એનાલિસિસ માટેનું રેફરન્સ ટૂલ છે, કારણ કે તે ડેટા સેટ્સનું આયોજન કરવા માટે ઘણી સુવિધાઓ પ્રદાન કરે છે,…
2017 થી રિયલ એસ્ટેટ ક્રાઉડફંડિંગના ક્ષેત્રમાં યુરોપના નેતાઓમાં Walliance, SIM અને પ્લેટફોર્મ, પૂર્ણ થવાની જાહેરાત કરે છે…
ફિલામેન્ટ એ "એક્સિલરેટેડ" લારેવેલ ડેવલપમેન્ટ ફ્રેમવર્ક છે, જે ઘણા ફુલ-સ્ટેક ઘટકો પ્રદાન કરે છે. તે પ્રક્રિયાને સરળ બનાવવા માટે રચાયેલ છે…
"મારે મારી ઉત્ક્રાંતિ પૂર્ણ કરવા માટે પાછા ફરવું પડશે: હું મારી જાતને કમ્પ્યુટરની અંદર રજૂ કરીશ અને શુદ્ધ ઊર્જા બનીશ. એકવાર સ્થાયી થયા પછી…
Google DeepMind તેના આર્ટિફિશિયલ ઇન્ટેલિજન્સ મોડલનું સુધારેલું સંસ્કરણ રજૂ કરી રહ્યું છે. નવું સુધારેલું મોડલ માત્ર…