Laravel 5.1 类 Illuminate\Database\Eloquent\Relations\HasMany 的对象无法转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32940769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.1 Object of class Illuminate\Database\Eloquent\Relations\HasMany could not be converted to string
提问by Tony keane
I have problem with my laravel 5.1 error Object of class Illuminate\Database\Eloquent\Relations\HasMany could not be converted to string when i click delete
我的 Laravel 5.1 有问题,当我单击删除时,类 Illuminate\Database\Eloquent\Relations\HasMany 的对象无法转换为字符串
GalleryController.php
画廊控制器.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();
}
Route.php
路由.php
Route::get('gallery/delete/{id}','GalleryController@deleteGallery');
Model Gallery.php
模型库.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Gallery extends Model
{
protected $table = 'gallery';
public function images()
{
return $this->hasMany('App\Image');
}
}
Model Image.php
模型图像.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class image extends Model
{
protected $fillable = ['gallery_id', 'file_name', 'file_size', 'file_mime', 'file_path',
'created_by'];
public function gallery()
{
return $this->belongsTo('App\Gallery');
}
}
Views gallery.blade.php
查看 gallery.blade.php
@extends('master')
@section('content')
<div class="row">
<div class="col-md-12">
<h1>My Gallery</h1>
</div>
</div>
<div class="row">
<div class="col-md-8">
@if ($galleries->count() > 0)
<table class="table table-striped table-bordered table-responsive">
<thead>
<tr class="info">
<th>Name of the gallery</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach($galleries as $gallery)
<tr>
<td>{{$gallery->name}}
<span class="pull-right">
{{ $gallery->images()->count() }}
</span>
</td>
<td><a href="{{url('gallery/view/'.$gallery->id)}}">View</a> /
<a href="{{url('gallery/delete/'.$gallery->id)}}">Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
@endif
</div>
<div class="col-md-4">
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form class="form" method="POST" action="{{url('gallery/save')}}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<input type="text" name="gallery_name"
id="gallery_name" placeholder="Name of the gallery"
class="form-control"
value="{{ old('gallery_name')}}" />
</div>
<button class="btn btn-primary">Save</button>
</form>
</div>
</div>
@endsection
回答by Mwaa Joseph
In the gallery controller you don't need to get the images since your not using the variable any where else.Hence remove the lines
在画廊控制器中,您不需要获取图像,因为您没有在其他任何地方使用变量。因此删除线条
// get the images
$images = $currentGallery->images();
Just have the foreach be as
只需将 foreach 作为
// delete the images
foreach ($currentGallery->images as $image) {
unlink(public_path($image->file_path));
}
please note use $currentGallery->images
请注意使用 $currentGallery->images