【发布时间】:2018-04-14 17:51:02
【问题描述】:
错误:
SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列 'products.id'(SQL:select * from products where products.id = 1 限制 1)
我确实有带有“product.id”字段的表“产品”。不知道为什么我会收到这个错误。我收到此错误时我
- 访问/product/{{product_id}}
- 访问/编辑
家庭控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('home')->with('products',$user->products);
}
}
产品控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\product;
class productController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth',['except' => ['index','show']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = product::orderBy('created_at','desc')->paginate(10);
//$products = product::where('type','major')->get();
return view('products.index')->with('products',$products);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('products.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,[
'title' => 'required',
]);
//create product
$product = new product;
$product->title = $request->input('title');
$product->venue = $request->input('venue');
$product->city = $request->input('city');
$product->country = $request->input('country');
$product->description = $request->input('description');
$product->date = $request->input('date');
$product->user_id = auth()->user()->id;
$product->save();
return redirect('/products')->with('success','product Created');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$product = product::find($id);
return view('products.show');
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$product = product::find($id);
return view('products.edit')->with('product',$product);
}
/**
* 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, [
'title' => 'required'
]);
$product = product::find($id);
$product->title = $request->input('title');
$product->save();
return redirect('/products')->with('success','product updated');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$product = product::find($id);
$product->delete();
return redirect('/products')->with('success','product deleted');
}
}
Show.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<h1 class="centre">{{$product->title}}</h1>
<div class="well-xs">
@if(!Auth::guest())
@if(Auth::user()->id == $product->user_id)
<a href="../products/{{$product->id}}/edit" class="btn btn-default">Edit</a>
{!!Form::open(['action' => ['productcontroller@destroy', $product->product_id], 'method' => 'POST'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
@endif
@endif
</div>
<div>
<div>
<h4>product Date: {{$product->date}}</h4>
<h4>product Venue: {{$product->venue}}</h4>
<h4>product Location:{{$product->city}}</h4>
<h4>product Description: </h4>
<p>{{$product->description}} </p>
</div>
</div>
<hr>
Written on
</hr>
</div>
@endsection
Products 表架构:我在 sql 中手动添加了 user_id。当我尝试单独迁移到 add_user_id_to_table 时,迁移不起作用
{
Schema::create('products', function (Blueprint $table) {
$table->increments('product_id');
$table->string('title',200);
$table->string('venue',200);
$table->text('city',200);
$table->text('country',200);
$table->string('description');
$table->date('date',200);
$table->timestamps();
});
}
user.php 模型:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function products(){
return $this->hasMany('App\Product');
}
}
Product.php 模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
//Table NAME
protected $table = 'products';
//PRIMARY KEY
public $primaryKey = 'id';
//Timestamps
public $timestamps =true;
public function user(){
return $this->belongsTo('App\User');
}
}
【问题讨论】:
-
你的数据库表架构是什么?
-
错误发生在哪一行?你能展示你的模型类吗?
-
错误提示您的
products表中没有user_id列 -
错误提示
products.user_id不存在,并且您共享了您的events表架构! -
在您的产品表中,您有
public $primaryKey = 'id';,但您的迁移使用product_id作为键。您应该使用id作为主要的自动递增键。这就是约定。