简体   繁体   English

调用未定义的方法Illuminate \\ Database \\ Query \\ Builder :: attach()在laravel 5.3中

[英]Call to undefined method Illuminate\Database\Query\Builder::attach() In laravel 5.3

I need help with this error 我需要帮助解决这个错误

Call to undefined method Illuminate\\Database\\Query\\Builder::attach() 调用未定义的方法Illuminate \\ Database \\ Query \\ Builder :: attach()

my Model is 我的模型是

public function casts()
{
    return $this->belongsToMany(Cast::class)->withTimestamps();
}

My Controller 我的控制器

public function addCast(Request $request, $id)
{
    $this->validate($request, [
        'casts' => 'required'
    ]);
    $show = $this->shows->findOrFail($id);
    $show->photos()->attach($request->input('casts'));
    dd($show);

}

my Form 我的表格

 <form action="{{ route('shows.addCasts', $show->id) }}" method="POST">
            {{ csrf_field() }}
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title"><img src="{{ Storage::url($show->small_image) }}" height="30" width="30">{{ $show->name }}</h4>
        </div>
        <div class="modal-body">
            <div class="form-group">
                {!! Form::label('casts') !!}
                {{ Form::select('casts[]', $casts, null, ['class' => 'form-control roles']) }}
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            {!! Form::submit('Save Cast', ['class' => 'btn btn-primary']) !!}
            {{--<button type="submit" class="btn btn-primary">Save changes</button>--}}
        </div>
        </form>

can anybody tell my what i did wrong or if you guys have any solution Thank for your help 任何人都可以告诉我我做错了什么或者你们有什么解决方案感谢您的帮助

The other side of the relationship also needs to be a belongsToMany as the attach method is for many to many relationships - see an already answered post on this: 关系的另一面也需要是belongsToMany,因为attach方法适用于多对多的关系 - 请参阅已经回答的帖子:

"Many to many - attach" “多对多 - 附上”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用未定义的方法Illuminate \\ Http \\ RedirectResponse :: validate() - Call to undefined method Illuminate\Http\RedirectResponse::validate() 调用未定义的方法:laravel - Call to undefined method : laravel 如何通过查询生成器方法在不使用模型的情况下使用Ajax将laravel中的数据插入/发布到数据库中 - How to insert/post data in laravel using ajax into Database by query builder method without using model 解析查询“无法调用未定义的方法&#39;equalTo&#39;” - Parse Query “Cannot call method 'equalTo' of undefined” laravel 5.3和Vuejs Ajax在选择框上调用 - laravel 5.3 and Vuejs Ajax call on select box Zurb Foundation 5.3 - 关闭画布不工作“TypeError:此[方法]未定义” - Zurb Foundation 5.3 - Off Canvas not working “TypeError: this[method] is undefined” 传递给 Illuminate\Database\Eloquent\Builder::create() 的参数 1 必须是数组类型,null 给定.. 发布数据时出错 - Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, null given .. Error to post data 重写方法以将事件处理程序附加到每个调用 - Override a method to attach an event handler to every call 无法调用未定义的on方法 - Cannot call method of on of undefined 无法调用未定义的方法 - Can not call method of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM