我有 2 个模型,一个用户和一个地址。我仍在尝试 Associate() ,因为我在https://laravel.com/docs/5.6/eloquent-relationships#updating-belongs-to-relationships中读到它,但我在实现它时遇到了一些麻烦,遗憾的是我的视频教程不涉及 Associate() 函数。
class User extends Authenticatable
public function address() {
return $this->hasOne('App\Address');
}
class Address extends Model
public function user() {
return $this->belongsTo('App\User');
}
// --web.php--
Route::get('/sample',function(){
$user = User::create(['name'=>'user 1','email'=>'user@laravel.com',password='']);
$address = Address::create(['name'=>'sample address of user 1']);
$address->user()->associate($user);
});
Run Code Online (Sandbox Code Playgroud)
一切都已保存,但我预计该地址的 user_id 将为 1。相反,该地址的 user_id 被设置为 NULL。Associate() 不是应该通过自动分配等于父级 ID 的外键来将特定的“belongsTo”模型链接到其父级吗?顺便说一句,我对 PHP 和 Laravel 很陌生,所以是的..
Prettier 扩展不起作用我默认设置它并在保存时格式化他给我一个错误:“需要配置设置为 true 并且不存在配置。正在跳过文件。”
我正在我的 laravel crud 项目上创建一个编辑部分。当我按下编辑键时,它将重定向到edit.blade.php将调用 slug 而不是它们的 id 的 。我已经研究过了,它显示了错误Too few arguments to function App\Http\Controllers\ProductController::edit(), 1 passed and exactly 2 expected
我的路线
Route::get('edit/{slug}', $url. '\productController@update');
Route::get('edit', $url. '\productController@edit');
Run Code Online (Sandbox Code Playgroud)
我的 ProductController::edit()
public function edit(Product $product, $slug)
{
return view('edit', compact('product'));
}
Run Code Online (Sandbox Code Playgroud)
我的 ProductController::update()
public function update(Request $request, Product $product, $slug)
{
Product::where('product_slug',$request->$slug)->update([
'product_title' => $request->title,
'product_slug' => $request->slug,
'product_image' => $request->image
]);
// redirect
return redirect('edit');
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,提前致谢
关闭分行有什么好处?如果我不这样做会发生什么。
我在一个团队中工作,我们在 Bitbucket 中使用 Git。有同事总说关闭分行非常重要。但为什么?
我想向我的 GitHub Action 工作流程添加一个条件。每当完成git push -forgit push --force而不是正常的git push. 这可能吗?
避免出现错误消息。当我运行命令时,我收到以下错误消息npm run dev:
npm notice
npm notice New major version of npm available! 7.6.1 -> 8.1.4
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.1.4
npm notice Run npm install -g npm@8.1.4 to update!
npm notice
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! Found: acorn@7.4.1
npm ERR! node_modules/acorn
npm ERR! acorn@"^7.0.0" from acorn-node@1.8.2
npm ERR! node_modules/acorn-node
npm ERR! acorn-node@"^1.6.1" from detective@5.2.0
npm ERR! node_modules/detective
npm ERR! detective@"^5.2.0" from tailwindcss@2.2.19
npm ERR! node_modules/tailwindcss
npm …Run Code Online (Sandbox Code Playgroud)