我需要知道如何使用 laravel 中的资源控制器将数据从数据库检索到多个页面
I need to know how to retrieve data from database to multiple pages using resource controller in laravel
由于我是 Laravel 的新手,这是我的第一个 Laravel 项目。
这是我的控制器。
ProductsController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use App\Product;
use App\Category;
class ProductsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$arr['products'] = Product::all();
return view('admin.products.index')->with($arr);
$productsAll = Product::get();
return view ('index')->with(compact('productsAll'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.products.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request, Product $product, Category $category)
{
if($request->prod_image_path->getClientOriginalName())
{
$ext =$request->prod_image_path->getClientOriginalName();
$file = date('YmdHis').rand(1,99999).'.'.$ext;
$request->prod_image_path->storeAs('public/admin',$file);
}else{
$file ='';
}
$product-> prod_name = $request-> prod_name;
$product-> prod_meta_title = $request-> prod_meta_title;
$product-> prod_description = $request-> prod_description;
$product-> prod_category = $request-> prod_category;
$product-> prod_price = $request-> prod_price;
$product-> prod_discount = $request-> prod_discount;
$product-> prod_image_path = $file;
$product->save();
return redirect()->route('admin.products.index');
}
/**
* 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(Product $product)
{
$arr['product'] = $product;
return view('admin.products.edit')->with($arr);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Product $product)
{
if($request->prod_image_path->getClientOriginalName())
{
$ext =$request->prod_image_path->getClientOriginalName();
$file = date('YmdHis').rand(1,99999).'.'.$ext;
$request->prod_image_path->storeAs('public/admin',$file);
}else{
$file ='';
}
$product-> prod_name = $request-> prod_name;
$product-> prod_meta_title = $request-> prod_meta_title;
$product-> prod_description = $request-> prod_description;
$product-> prod_category = $request-> prod_category;
$product-> prod_price = $request-> prod_price;
$product-> prod_discount = $request-> prod_discount;
$product-> prod_image_path = $file;
$product->save();
return redirect()->route('admin.products.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Product:: destroy($id);
return redirect()->route('admin.products.index');
}
}
为了在 admin.products 页面中查看,我使用了下面的代码。
public function index()
{
$arr['products'] = Product::all();
return view('admin.products.index')->with($arr);
}
为了在索引页中查看,我使用了下面的代码。
public function index()
{
$productsAll = Product::get();
return view ('index')->with(compact('productsAll'));
}
如何在索引函数中同时使用这两个函数。或者解决这个问题的好做法是什么?
这是我的索引页。
index.blade.php
@foreach($productsAll as $p)
<a href="../shop/product-categories-7-column-full-width.html" class="d-block py-2 text-center">
<img class="img-fluid mb-1 max-width-100-sm" src="{{ asset('assets/img/300X300/img6.jpg')}}" alt="Image Description">
<h6 class="font-size-14 mb-0 atext font-weight-semi-bold">{{ $p -> prod_name }}</h6>
</a>
</li>
@endforeach
我认为有两种可能的方法:
- 使用其他方法扩展您当前的控制器(例如
indexAdmin
)
public function index()
{
$productsAll = Product::get();
return view ('index')->with(compact('productsAll'));
}
public function indexAdmkn()
{
$arr['products'] = Product::all();
return view('admin.products.index')->with($arr);
}
- 遵守 resource controller principle 并为您的管理员创建一个单独的控制器(例如
ProductsAdminController
)
顺便说一下,如果您是 Laravel 的新手,我强烈推荐 Laracasts。他们也提供免费的培训视频。例如。 this one 在控制器上。
由于我是 Laravel 的新手,这是我的第一个 Laravel 项目。
这是我的控制器。
ProductsController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use App\Product;
use App\Category;
class ProductsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$arr['products'] = Product::all();
return view('admin.products.index')->with($arr);
$productsAll = Product::get();
return view ('index')->with(compact('productsAll'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.products.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request, Product $product, Category $category)
{
if($request->prod_image_path->getClientOriginalName())
{
$ext =$request->prod_image_path->getClientOriginalName();
$file = date('YmdHis').rand(1,99999).'.'.$ext;
$request->prod_image_path->storeAs('public/admin',$file);
}else{
$file ='';
}
$product-> prod_name = $request-> prod_name;
$product-> prod_meta_title = $request-> prod_meta_title;
$product-> prod_description = $request-> prod_description;
$product-> prod_category = $request-> prod_category;
$product-> prod_price = $request-> prod_price;
$product-> prod_discount = $request-> prod_discount;
$product-> prod_image_path = $file;
$product->save();
return redirect()->route('admin.products.index');
}
/**
* 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(Product $product)
{
$arr['product'] = $product;
return view('admin.products.edit')->with($arr);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Product $product)
{
if($request->prod_image_path->getClientOriginalName())
{
$ext =$request->prod_image_path->getClientOriginalName();
$file = date('YmdHis').rand(1,99999).'.'.$ext;
$request->prod_image_path->storeAs('public/admin',$file);
}else{
$file ='';
}
$product-> prod_name = $request-> prod_name;
$product-> prod_meta_title = $request-> prod_meta_title;
$product-> prod_description = $request-> prod_description;
$product-> prod_category = $request-> prod_category;
$product-> prod_price = $request-> prod_price;
$product-> prod_discount = $request-> prod_discount;
$product-> prod_image_path = $file;
$product->save();
return redirect()->route('admin.products.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Product:: destroy($id);
return redirect()->route('admin.products.index');
}
}
为了在 admin.products 页面中查看,我使用了下面的代码。
public function index()
{
$arr['products'] = Product::all();
return view('admin.products.index')->with($arr);
}
为了在索引页中查看,我使用了下面的代码。
public function index()
{
$productsAll = Product::get();
return view ('index')->with(compact('productsAll'));
}
如何在索引函数中同时使用这两个函数。或者解决这个问题的好做法是什么?
这是我的索引页。
index.blade.php
@foreach($productsAll as $p)
<a href="../shop/product-categories-7-column-full-width.html" class="d-block py-2 text-center">
<img class="img-fluid mb-1 max-width-100-sm" src="{{ asset('assets/img/300X300/img6.jpg')}}" alt="Image Description">
<h6 class="font-size-14 mb-0 atext font-weight-semi-bold">{{ $p -> prod_name }}</h6>
</a>
</li>
@endforeach
我认为有两种可能的方法:
- 使用其他方法扩展您当前的控制器(例如
indexAdmin
)
public function index()
{
$productsAll = Product::get();
return view ('index')->with(compact('productsAll'));
}
public function indexAdmkn()
{
$arr['products'] = Product::all();
return view('admin.products.index')->with($arr);
}
- 遵守 resource controller principle 并为您的管理员创建一个单独的控制器(例如
ProductsAdminController
)
顺便说一下,如果您是 Laravel 的新手,我强烈推荐 Laracasts。他们也提供免费的培训视频。例如。 this one 在控制器上。