<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\VueJSProductController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/vue-product', [VueJSProductController::class,'showVueProduct']);
Route::resource('vueproduct',VueJSProductController::class);
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
class VueJSProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::latest()->paginate(5);
$response = [
'pagination' => [
'total' => $products->total(),
'per_page' => $products->perPage(),
'current_page' => $products->currentPage(),
'last_page' => $products->lastPage(),
'from' => $products->firstItem(),
'to' => $products->lastItem()
],
'data' => $products
];
return response()->json($response);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'price' => 'required',
'content' => 'required',
]);
$create = Product::create($request->all());
return response()->json($create);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'price' => 'required',
'content' => 'required',
]);
$edit = Product::find($id)->update($request->all());
return response()->json($edit);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Product::find($id)->delete();
return response()->json(['done']);
}
public function showVueProduct(){
return view('frontend.vue-product');
}
}
C:\xampp\htdocs\songkhoe\app\Models\Product.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $fillable = [
"name",
"price",
"content"
];
}