我的 Laravel 5.1 有问题,当我单击删除时,类 Illuminate\Database\Eloquent\Relations\HasMany 的对象无法转换为字符串
画廊控制器.php
public function deleteGallery($id)
{
//load the gallery
$currentGallery = Gallery::findOrfail($id);
// check ownership
if ($currentGallery->created_by != Auth::user()->id) {
abort('403','You are not allowed to delete this gallery');
}
// get the images
$images = $currentGallery->images();
// delete the images
foreach ($currentGallery->$images as $image) {
unlink(public_path($image->file_path));
}
// delete the DB records
$currentGallery->images()->delete();
$currentGallery->delete();
return redirect()->back();
}
Run Code Online (Sandbox Code Playgroud)
路由.php
Route::get('gallery/delete/{id}','GalleryController@deleteGallery');
Run Code Online (Sandbox Code Playgroud)
模型库.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Gallery extends Model
{
protected $table = 'gallery';
public function …Run Code Online (Sandbox Code Playgroud)