微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在布尔值上调用成员函数 attach() - Laravel

如何解决在布尔值上调用成员函数 attach() - Laravel

我在使用 attach() 函数时遇到问题。我创建了 3 个表。 1 表用于添加电影。表 2 适用于电影类别。 3表是电影ID和类别ID的集合。多对多的组合。

当我想从表单中添加一个带有类别的新视频时,我有错误 -> 调用布尔值上的成员函数 attach()。如果有人知道答案,请帮忙。感谢您的时间!

VideoController.PHP -> 看方法 store();

<?PHP

namespace App\Http\Controllers;

use Request;
use App\Http\Requests\CreateVideoRequest;
use App\Video;
use App\Category;
use Auth;
use Session;

class VideoController extends Controller
{
    public function __construct(){
        $this->middleware('auth',['except' => 'index']);
    }

    public function index(){
        $videos = Video::latest()->get();
        return view('videos.index')->with('videos',$videos);
    }

    public function show($id){
        $video = Video::find($id);  
        return view('videos.show')->with('video',$video);
    }

    public function create(){
        $categories = Category::pluck('name','id');
        return view('videos.create')->with('categories',$categories);
    }

    public function store(CreateVideoRequest $request){
        $video = new Video($request->all());
        //dd($request->all());
        Auth::user()->videos()->save($video);

        $categoryIds = $request->input('CategoryList');
        $video->categories()->attach($categoryIds);

        Session::flash('video_created','Added');

        return redirect('video');
    }

    public function edit($id){
        $video = Video::findOrFail($id);
        return view('videos.edit')->with('video',$video);
    }

    public function update($id,CreateVideoRequest $request){
        $video = Video::findOrFail($id);
        $video->update($request->all());
        return redirect('video');
    }
}

Video.PHP - 模型

<?PHP

namespace App;

use Illuminate\Database\Eloquent\Model;

class Video extends Model
{   

    protected $fillable = [
        'title','url','description'
    ];

    public function user(){
        return $this->belongsTo('App\User');
    }

    public function categories(){
        return $this->belongsToMany('App\Video')->withTimestamps;
    }
}

Category.PHP - 模型

<?PHP

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{

    protected $fillable = [ 'name' ];

    public function videos(){
        return $this->belongsToMany('App/Video')->withTimestamps;
    }
}

User.PHP - 模型(也许你需要一个 User.PHP 模型来解决问题)

<?PHP

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name','email','password',];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password','remember_token',];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',];

    public function videos(){
        return $this->hasMany('App\Video');
    }
}

迁移 - create_category_video_table.PHP

<?PHP

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCategoryVideoTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('category_video',function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('category_id');

            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->unsignedBigInteger('video_id');

            $table->foreign('video_id')->references('id')->on('videos')->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('category_video');
    }
}

迁移 - create_categories_table.PHP

<?PHP

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories',function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categories');
    }
}

迁移 - create_videos_table.PHP

<?PHP

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateVideosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('videos',function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');     
            $table->string('title');
            $table->string('url');
            $table->text('description');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('videos');
    }
}

form.blade.PHP - Laravel Collection 中的简单表单

{!! Form::select('CategoryList',$categories,null,['multiple' => 'multiple']) !!}

解决方法

对于数据透视表的时间戳,您将调用“方法”withTimestamps() 而不是名为 withTimestamps 的“属性”:

// Video
public function categories()
{
    return $this->belongsToMany(Category::class)->withTimestamps();
}

// Category
public function videos()
{
    return $this->belongsToMany(Video::class)->withTimestamps();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。