Model.php第2794行出现错误异常:Laravel 5.2中的偏移量类型非法*
我在创建产品时遇到了一个问题,类似这样的错误消息会出现“ErrorException in Model.php第2794行:非法偏移类型” Model Product.phpModel.php第2794行出现错误异常:Laravel 5.2中的偏移量类型非法*,php,laravel,Php,Laravel,我在创建产品时遇到了一个问题,类似这样的错误消息会出现“ErrorException in Model.php第2794行:非法偏移类型” Model Product.php <?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { protected $fillable = ['name','description','image','size','
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['name','description','image','size','price','category_id'];
protected $primaryKey = ['product_id'];
public function category()
{
$this->belongsTo(Category::class);
}
}
我想是因为默认名称更改了。做这个改变并尝试一下
public function category()
{
$this->belongsTo(Category::class, 'category_id', 'category_id');
}
或者图像未设置或为空,请尝试将其添加为:
if ($request->hasFile('image')) {
$data['image'] = $this->saveimage($request->file('image'));
}else{
$data['image'] = '';
}
我明白了:
array:7 [▼
"_token" => "dkbgGdfB1wP5rTRlGmKmo0rItrnV5T1pvnj65Uc3"
"name" => "Bukan Hekel"
"description" => "Bukan Hekel"
"price" => "120000"
"size" => "12"
"category_lists" => array:1 [▼
0 => "1"
]
"image" => UploadedFile {#215 ▼
-test: false
-originalName: "bukanhekel.jpeg"
-mimeType: "image/jpeg"
-size: 54465
-error: 0
path: "/tmp"
filename: "phpnjDRJA"
basename: "phpnjDRJA"
pathname: "/tmp/phpnjDRJA"
extension: ""
realPath: false
writable: false
readable: false
executable: false
file: false
dir: false
link: false
}
]
在产品模型类中,更改此行protected$primaryKey=['Product_id']代码>到受保护的$primaryKey='product_id'代码>应该可以解决此错误。所以,图像可能返回空值。我编辑我的答案并尝试一下,你也可以使用dd($request->all())进行检查。那么在具有复合主键的架构上使用Faker/Factories是否存在已知问题?Faker可以很好地为我生成所有内容,除了具有$primaryKey=['key1'、'key2'、'key3']
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
// Front End
Route::get('/', 'CatalogsController@index')->name('index');
// Auth
Route::auth();
Route::get('/home', 'HomeController@index');
// Back End
Route::group(['prefix' => 'backend', 'middleware' => ['auth','admin']], function() {
Route::get('/', function() {
return view('admin.index');
})->name('backend.index');
Route::resource('categories', 'CategoryController');
Route::resource('products', 'ProductController');
});
public function category()
{
$this->belongsTo(Category::class, 'category_id', 'category_id');
}
if ($request->hasFile('image')) {
$data['image'] = $this->saveimage($request->file('image'));
}else{
$data['image'] = '';
}
array:7 [▼
"_token" => "dkbgGdfB1wP5rTRlGmKmo0rItrnV5T1pvnj65Uc3"
"name" => "Bukan Hekel"
"description" => "Bukan Hekel"
"price" => "120000"
"size" => "12"
"category_lists" => array:1 [▼
0 => "1"
]
"image" => UploadedFile {#215 ▼
-test: false
-originalName: "bukanhekel.jpeg"
-mimeType: "image/jpeg"
-size: 54465
-error: 0
path: "/tmp"
filename: "phpnjDRJA"
basename: "phpnjDRJA"
pathname: "/tmp/phpnjDRJA"
extension: ""
realPath: false
writable: false
readable: false
executable: false
file: false
dir: false
link: false
}
]