请我需要帮助,我正在Laravel上的一个项目中,当T尝试以多对多方式使用数据透视表时遇到了问题
在我的category model
Category.php
中,我具有以下功能
public function users()
{
return $this->belongsToMany(user::class);
}
在我的user model
User.php
中,我具有以下功能
public function authors()
{
return $this->belongsToMany(author::class);
}
在web.php
中,我有这个
Route::post('/onboarding', 'OnboardsController@categoryUser');
在我的onboarding view
中,我有一个表格:
<form action="{{ url('/onboarding') }}" method="POST">
@csrf
@foreach($categories as $category)
<div class="radio-item">
<input class="form control" name="category_id" type="checkbox" id="category_id" required>
<label for="name">{{ $category -> title }}</label>
</div>
@endforeach
<div class="form-row">
<div class="form-group col-md-12 text-center">
<button type="submit">Proceed to Authors</button>
</div>
</div>
</form>
在我的OnboardsController
我有这个
public function categoryUser (Request $request)
{
$user = User::findOrFail(Auth::user()->id);
$user = categories()->attach($request->input('category_id'));
return redirect ( route ('onboarding_author'));
}
这是返回的错误:
调用未定义函数App \ Http \ Controllers \ categories()
当我将控制器中的代码更改为
$category = Category::find($request->input('category_id'));
$user = User::where('user_id', Auth::id());
$category->users()->attach($user->id);
这是返回的错误:
调用未定义函数App \ Http \ Controllers \ users()
这是我完整的OnboardsController.php
代码
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Author;
use App\User;
use App\Category;
class OnboardsController extends Controller
{
public function index()
{
//
return view('onboarding')
->with('categories', Category::all());
}
public function author()
{
//
return view('onboarding_author')
->with('authors', Author::all());
}
public function categoryUser (Request $request)
{
dd(request('category_id'));
Category::findOrFail(request('category_id'))
->users()
->attach(auth()->user()->id);
return redirect ( route ('onboarding_author'));
}
请问我是Laravel的新手,我已经做了3天了,我不知道该如何解决。我需要帮助。
答案 0 :(得分:4)
您的问题就在这里
$user = User::findOrFail( Auth::user()->id );
$user = categories()->attach($request->input('category_id'));
// ^^^^^^^^^^^^
您在...上调用方法category()?什么都没有。我假设您想在categories()
模型中调用User.php
方法。因此,尝试像这样更新代码:
$user = User::findOrFail( Auth::user()->id );
$user->categories()->attach($request->input('category_id'));
// ^^^^^^^^^^^^
这样,您将关联两个对象。
正如我所提到的,我假设您在categories
模型中拥有User
方法:
# User.php
public function categories()
{
return $this->belongsToMany(Category::class);
}
请记住,模型应该大写。
此外,您可以改进代码,直接从Auth外观获取用户
此:
$user = User::findOrFail(Auth::user()->id);
等同于:
$user = Auth::user();
答案 1 :(得分:0)
您可以通过以下方法实现此目标:
$counter_connected = true;
$link = mysqli_connect($counter_host, $counter_user, $counter_password, $counter_database);
if (!$link) {
$counter_connected = false;
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
exit;
}
if ($counter_connected == true) {
$db = mysqli_connect($counter_host, $counter_user, $counter_password, $counter_database);
$query = "SELECT * FROM CanadaPostal";
$result = $db->query($query) or die(mysql_error());
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$latitude = $row['latitude'];
$longitude = $row['longitude'];
$id = $row['id'];
$googlemapKey = "[MY-KEY]";
$json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($latitude).','.trim($longitude).'&sensor=true&key='.$googlemapKey.'');
$data = json_decode($json, true);
$counter = 0;
foreach ($data['results'] as $item) {
if( $counter == 0 ) {
if( !empty( $item['formatted_address'] ) ){
$formatted_address = $item['formatted_address'];
$query.$counter = "UPDATE CanadaPostal SET fulladd='".$item['formatted_address']."' WHERE id=".trim($id)."";
$result = $db->query($query.$counter) or die(mysql_error());
}
}
$counter = $counter + 1;
}
}
}
}
在这里,您通过请求的public function categoryUser()
{
Category::findOrFail(request('category_id'))
->users()
->attach(auth()->user()->id);
return redirect(route('onboarding_author'));
}
得到了Category
,并附加了登录的用户(使用category_id
助手来获得用户的auth()
)。