laravel-i need help passing user\u id to tasks\u table in sql database error:call to undefined method app\\user::id()

anauzrmj  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(402)

我还是一个学生,还没有弄清楚所有这些东西。我一直在尝试让我的任务在store方法中将用户id传递给我的sql数据库。我试过1000种不同的方法,但都不走运。
当前错误:调用未定义的方法app\user::id()。我注意到其他帖子的错误中似乎没有2\ after应用程序。我不明白它为什么这么做。
我已经发布了所有的代码,以防有人从中得到任何帮助。任何帮助都将不胜感激,如果您需要更多的帮助,请告诉我。
这是我的任务控制器

<?php

namespace App\Http\Controllers;

use App\Task;
use Illuminate\Http\Request;
use Auth;
use App\User;

class TaskController extends Controller
{
    public function __construct(){
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function index()
    {
        return response(Task::latest()->get(), 200);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->validate([
            'text' => 'required',
            'finished' => 'required|boolean',
        ]);

        $user = Auth::user()->id();
        $task = Task::create($data, $user);

        return response($task, 201);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Task  $task
     * @return \Illuminate\Http\Response
     */
    public function show(Task $task)
    {
        return response($task, 200);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Task  $task
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Task $task)
    {
        $data = $request->validate([
            'text' => 'required',
            'finished' => 'required|boolean',
        ]);

        $task->update($data);

        return response($task, 200);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Task $task
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function destroy(Task $task)
    {
        $task->delete();

        return response('Deleted Succesfully', 200);
    }
}

这是我的任务模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    protected $fillable = [
        'text',
        'finished',
        'user_id'

    ];

    protected $casts = [
        'finished' => 'boolean',
    ];

    public function tasks(){
        return $this->hasMany(Task::class);
    }

    public function user(){
        return $this->belongsTo(User::class);
    }

}

这是我的用户模型

namespace App;

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

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',
    ];

    public function tasks(){
        return $this->hasMany(Task::class);
    }
}

任务表架构

public function up()
{
    Schema::create('tasks', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id');
        $table->longText('text');
        $table->boolean('finished')->default(false);
        $table->timestamps();
    });
}

用户表架构

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
r55awzrz

r55awzrz1#

只是稍微修改了一下代码:
任务模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    protected $fillable = [
        'text',
        'finished',
        'user_id'
    ];

    protected $casts = [
        'finished' => 'boolean',
    ];

    public function user(){
        return $this->belongsTo(User::class);
    }

}

任务控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Task;
use Auth;
use DB;

class TaskController extends Controller
{
    public function __construct(){
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function index()
    {
        return response(Task::latest()->get(), 200);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'text' => 'required',
            'finished' => 'required|boolean'
        ]);

        $task = Task::create([
            'text' => $request->text,
            'finished' => $request->finished,
            'user_id' => Auth::user()->id
        ]);

        return response($task, 201);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Task  $task
     * @return \Illuminate\Http\Response
     */
    public function show(Task $task)
    {
        return response($task, 200);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Task  $task
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Task $task)
    {
        $request->validate([
            'text' => 'required',
            'finished' => 'required|boolean',
        ]);

        $task->update([
            'text' => $request->text,
            'finished' => $request->finished,
        ]);

        return response($task, 200);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Task $task
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function destroy(Task $task)
    {
        $task->delete();

        return response('Deleted Successfully', 200);
    }
}

相关问题