1
votes

I'm new with laravel and its code and got basic PHP skills... I got this error:

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: delete from reseps where id is null)".

When i want to edit/delete my data.

Controller

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!');
}

Model

    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
where is the delete query? - madalinivascu
Sorry forgot to add Delete... here it is - a0605
what is whereSlug returning? - madalinivascu
you need to return the id as well in whereSlug - madalinivascu
An unique id..$slug= uniqid(); - a0605

1 Answers

0
votes

It seems you're using a different column to store the primary key of the Resep model. You need to tell Eloquent what it is with:

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