在布尔值上调用成员函数 attach() - Laravel
Posted
技术标签:
【中文标题】在布尔值上调用成员函数 attach() - Laravel【英文标题】:Call to a member function attach() on boolean - Laravel 【发布时间】:2021-05-23 07:42:45 【问题描述】:我对函数 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']) !!
【问题讨论】:
【参考方案1】:对于枢轴的时间戳,您将调用“方法”withTimestamps()
而不是名为 withTimestamps
的“属性”:
// Video
public function categories()
return $this->belongsToMany(Category::class)->withTimestamps();
// Category
public function videos()
return $this->belongsToMany(Video::class)->withTimestamps();
【讨论】:
以上是关于在布尔值上调用成员函数 attach() - Laravel的主要内容,如果未能解决你的问题,请参考以下文章
PHP:致命错误:未捕获的错误:在布尔值上调用成员函数 execute() [重复]
Codeigniter - 在布尔值上调用成员函数 result_array() - 本地设置安装错误