错误SQLSTATE [42S22] Laravel 5.4

时间:2017-11-09 05:03:01

标签: php laravel laravel-5 eloquent laravel-5.4

我是laravel及其代码的新手,并获得了基本的PHP技能...... 我收到了这个错误:

  

" SQLSTATE [42S22]:未找到列:1054未知列' id'在'哪里   条款' (SQL:从reseps删除id为空)"。

当我想编辑/删除我的数据时。

控制器

class ResepController extends Controller
{

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $resep = Resep::all();
return view('resep.index', compact('resep'));
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
     return view('resep.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function tambahResep(ResepFormRequest $request)
{
    $slug= uniqid();
    $resep= new Resep(array(
        'resep_nama' => $request-> get('resep_nama'),
        'resep_unit' => $request-> get('resep_unit'),
        'resep_satuan' => $request-> get('resep_satuan'),
        'resep_harga' => $request-> get('resep_harga'),
        'resep_hargaUnit' => $request-> get('resep_hargaUnit'),
        'slug' => $slug
    ));
    $resep -> save();
    return redirect('/resep/create')-> with('status','Resep berhasil ditambah!');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($slug)
{
    $resep = Resep::whereSlug($slug)->firstOrFail();
return view('resep.show', compact('resep'));
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($slug)
{
     $resep = Resep::whereSlug($slug)->firstOrFail();
return view('resep.edit', compact('resep'));
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(ResepFormRequest $request, $slug)
{
    $resep = Resep::whereSlug($slug)->firstOrFail();
    $resep->resep_nama = $request->get('resep_nama');
    $resep->resep_unit = $request->get('resep_unit');
    $resep->resep_satuan = $request->get('resep_satuan');
    $resep->resep_harga = $request->get('resep_harga');
    $resep->resep_hargaUnit = $request->get('resep_hargaUnit'); 
    $resep->save();
    return redirect(action('ResepController@edit', $resep->slug))-> with('status', 'Resep berhasil dirubah!');
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($slug)
{
    $resep = Resep::whereSlug($slug)->firstOrFail();
    $resep->delete();
    return redirect('/resep')->with('status', 'Resep berhasil dihapus!');
}

模型

    class Resep extends Model{
    protected $fillable = ['resep_nama', 'resep_unit', 'resep_satuan', 'resep_harga', 'resep_hargaUnit','slug'
];
protected $guarded = ['resep_id'];

public function user()
{
    return $this->belongsTo('App\User');
}

public function getresepNama()
{
    return $this->resep_nama;
}

1 个答案:

答案 0 :(得分:0)

您似乎正在使用其他列来存储Resep模型的主键。你需要告诉Eloquent它是什么:

class Resep extends Model {
   protected $primaryKey = 'resep_id';
}