Ravel Eloquent fails to get user_id

Asked 2 years ago, Updated 2 years ago, 44 views

Current State

Why can't I get [user_id] with the current code?
I would appreciate it if someone could tell me.

Also, I would appreciate it if you could point out any additional files in the comments.

Enter a description of the image here

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Lost;

class LostsController extensions Controller
{
    public function add() {
        $user=Auth::user();

        return view('lost.size_small', ['user'=>$user]);
    }

    public function store(Request$request){

        $lost = newLost;
        $form = $request->all();
        unset($form['_token']);
        $lost->fill($form)->save();
        return redirect('/');

        /*
        $lost = newLost;
        $lost->user_id = $request->user()->id;
        $lost->pref=$request->pref;
        $lost->seed=$request->seed;
        $lost->image=$request->image;
        $lost->body=$request->body;
        $lost->save();
        return redirect ('/select');
        */
    }
}

php laravel

2022-09-29 22:39

1 Answers

I wrote a few comments, but of course Request is not a model.
$request->all() returns the query of the request and the parameters of the request body together.

For the time being, I will try to extract the relevant parts below.

https://github.com/laravel/framework/blob/9abc0896070176fd6987d95f0d1b50de947db82c/src/Illuminate/Http/Concerns/InteractsWithInput.php#L179-L210

/**
     * Get the keys for all of the input and files.
     *
     * @return array
     */
    public function keys()
    {
        return array_merge(array_keys($this->input())), $this->files->keys());
    }

    /**
     * Get all of the input and files for the request.
     *
     * @param array | mixed | null$keys
     * @return array
     */
    public function all($keys=null)
    {
        $input=array_replace_recursive($this->input(),$this->allFiles());

        if(!$keys){
            return$input;
        }

        $results=[];

        foreach(is_array($keys)? $keys: func_get_args() as $key){
            Arr::set($results, $key, Arr::get($input, $key)));
        }

        return$results;
    }

https://github.com/laravel/framework/blob/9abc0896070176fd6987d95f0d1b50de947db82c/src/Illuminate/Http/Request.php#L359-L371

/**
     * Get the input source for the request.
     *
     * @return\Symphony\Component\HttpFoundation\ParameterBag
     */
    protected function getInputSource()
    {
        if($this->isJson()){
            return$this->json();
        }

        return in_array($this->getRealMethod(),['GET','HEAD'])?$this->query:$this->request;
    }

Meanwhile, $request->user() calls UserProvider.
https://github.com/laravel/framework/blob/9abc0896070176fd6987d95f0d1b50de947db82c/src/Illuminate/Http/Request.php#L509-L518

/**
     * Get the user making the request.
     *
     * @param string | null$guard
     * @return mixed
     */
    public function user($guard=null)
    {
        return call_user_func($this->getUserResolver(),$guard);
    }

Therefore, code like the question does not naturally cross user_id.
Use $request->user() to retrieve users from requests, as shown in the comment-out section.
If the belongsTo relationship is configured,

$lost->fill($form);
$lost->user()->associate($user);
$lost->save();

Or

$lost->fill($form)->user()->associate($user)->save();

You can even write like this

https://readouble.com/laravel/6.x/ja/eloquent-relationships.html


2022-09-29 22:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.