出什么问题了?我在数据库中有记录。
资源路线为Route::resource('dylan_dog', 'DylanDogController');
我在$dylan_dog
中循环了由控制器(在下面提供)的每个index.blade.php
记录,如下所示:
@foreach ($dylan_dog as $ddog)
<tr>
<td>{{ $ddog -> id }}</td>
<td>{{ $ddog -> name }}</td>
<td>{{ $ddog -> title }}</td>
<td>{{ $ddog -> biography }}</td>
<td><a href="{{ route('dylan_dog.edit', $dylan_dog->id) }}" class="btn btn-primary">Edit</a></td>
<td>
<form action="{{ route('dylan_dog.destroy', $dylan_dog->id) }}" method="post">
@csrf
@method('DELETE')
<button class="btn btn-danger" type="submit" name="delete">Delete</button>
</form>
</td>
</tr>
@endforeach
有什么收获?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Dylan_dog;
class DylanDogController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$dylan_dog = Dylan_dog::all();
return view('dylan_dog.index', compact('dylan_dog'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('dylan_dog.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name'=>'required',
'title'=> 'required',
'biography' => 'required'
]);
$dylan_dog = new Dylan_dog([
'name' => $request->get('name'),
'title'=> $request->get('title'),
'biography'=> $request->get('biography')
]);
$dylan_dog->save();
return redirect('/dylan_dog')->with('success', 'Thanks for your contribution');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//return view('dylan_dog.index', ['name' => Dylan_dog::findOrFail($id)]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$dylan_dog = Dylan_dog::find($id);
return view('dylan_dog.edit', compact('dylan_dog'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$request->validate([
'name'=>'required',
'title'=> 'required',
'biography' => 'required'
]);
$dylan_dog = Share::find($id);
$dylan_dog->name = $request->get('name');
$dylan_dog->title = $request->get('title');
$dylan_dog->biography = $request->get('biography');
$dylan_dog->save();
return redirect('/dylan_dog')->with('success', 'Character has been updated');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$dylan_dog = Share::find($id);
$dylan_dog->delete();
return redirect('/dylan_dog')->with('success', 'Character has been deleted successfuly');
}
}
错误消息“ ErrorException(E_ERROR)属性[id]不存在 在这个集合实例上。 (视图: C:\ xampp \ htdocs \ miljan \ resources \ views \ dylan_dog \ index.blade.php)“
答案 0 :(得分:1)
仅注意到您正在使用$dylan_dog->id
。 $dylan_dog
是Collection
,并且没有id
作为属性。请将其更改为$ddog->id
:
<td><a href="{{ route('dylan_dog.edit', $ddog->id) }}" class="btn btn-primary">Edit</a></td>
<td>
<form action="{{ route('dylan_dog.destroy', $ddog->id) }}" method="post">
@csrf
@method('DELETE')
<button class="btn btn-danger" type="submit" name="delete">Delete</button>
</form>
</td>
您还应该将路线参数更改为$ dogg。