Illegal offset type (ok)
http://blog.com/product
ErrorExceptionIllegal offset typehttp://blog.com/product
True
public function index()
{
$products = DB::table('productstest')->get();
return view('fontend.product.list')->with('products',$products);
}
/**
False :(
public function index()
{
$products = DB::table('productstest')->get();
return view('fontend.product.list')->with($products);
}
/**
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use Input;
use Illuminate\Support\Facades\DB;
class ProductController extends Controller {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = DB::table('productstest')->get();
return view('fontend.product.list')->with($products);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
return view('fontend.product.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
$validator = Validator::make($request->all(), [
'name' => 'required'
]);
if ($validator->fails()) {
return redirect('product/create')->withErrors($validator)->withInput();
} else {
// Lưu thông tin vào database, phần này sẽ giới thiệu ở bài về database
$active = $request->has('active') ? 1 : 0;
$product_id = DB::table('productstest')->insertGetId(
array(
'name' => $request->input('name'),
'price' => $request->input('price'),
'content' => $request->input('content'),
'img_path' => $request->input('image_path'),
'active' => $active,
)
);
return redirect('product/create')->with('message', 'Sản phẩm được tạo thành công với ID: ' . $product_id);
}
}
/**
* 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)
{
$active = $request->has('active')? 1 : 0;
$updated = DB::table('products')
->where('id', '=', $id)
->update([
'name' => $request->input('name'),
'price' => $request->input('price'),
'content' => $request->input('content'),
'image_path' => $request->input('image_path'),
'active' => $active,
'updated_at' => \Carbon\Carbon::now()
]);
return Redirect::back()
->with('message', 'Cập nhật sản phẩm thành công')
->withInput();
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
//
}
}
PreviousThe GET method is not supported for this route. Supported methods: POST. (ok)NextXây dựng một blog đơn giản sử dụng Laravel (videos)
Last updated