laravel 间接修改重载属性没有效果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47707474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:04:10  来源:igfitidea点击:

Indirect modification of overloaded property has no effect

phplaravel

提问by

When I run the below API I got this error

当我运行以下 API 时出现此错误

"Indirect modification of overloaded property App\User::$attendance has no effect".

“间接修改重载属性 App\User::$attendance 无效”。

I still cannot figure it out for so long. Could you please someone help me on this.

这么久我还是想不通。你能请人帮助我吗?

public function apiPresent( $id, Request $request )
{
    $tokenuser = User::with('attendance')
                    ->where('token', $request->input('token'))->first();
    $present   = $request->input('present');
    $user      = User::with('attendance')->find($id);

    if( $user && $tokenuser == $user )
    {
        $user->attendance->present = $present;
        $user->attendance->date    = Carbon::now()->format('d-m-Y');
        $user->attendance->time    = Carbon::now()->format('H-i');
        $user->attendance->save();

        return response()->json([
                    'status'  => 'Ok',
                    'message' => 'Present Changed',
                    'Present' => $user->attendance->present
        ]);
    }
    else
    {
        return response()->json([
                    'status'  => 'Fail',
                    'message' => 'Your state does not changed'
        ]);
    }
}

采纳答案by kunal

If i understand your problem. You are saving the attendance.

如果我理解你的问题。您正在节省出勤率。

Try this:

尝试这个:

use App\Attendance; // Add before controller declaration

public function apiPresent( $id, Request $request )
{
    $tokenuser = User::where('token', $request->input('token'))->first();
    $present   = $request->input('present');
    $user      = User::find($id);

    if( $user && $tokenuser->id == $user->id ){
        $attendance = new Attendance;
        $attendance->present = $present;
        $attendance->date    = Carbon::now()->format('d-m-Y');
        $attendance->time    = Carbon::now()->format('H-i');
        $attendance->save();

        return response()->json([
                'status'  => 'Ok',
                'message' => 'Present Changed',
                'Present' => $present
       ]);
    }
    else {
        return response()->json([
                'status'  => 'Fail',
                'message' => 'Your state does not changed'
       ]);
    }
}

Hope it helps!

希望能帮助到你!

回答by ashutosh

I think the problem is that you're trying to directly assign to a relation of App::User. Your code segment highlights this:

我认为问题在于您试图直接分配给App::User. 您的代码段突出显示了这一点:

if( $user && $tokenuser->id == $user->id ){
    $user->attendance->present = $present;
    $user->attendance->date    = Carbon::now()->format('d-m-Y');
    $user->attendance->time    = Carbon::now()->format('H-i');
    $user->attendance->save();
    ...

In Eloquent, you access a relation as a property using the magic __get()method, which returns a copyof the object.

在 Eloquent 中,您可以使用魔术__get()方法将关系作为属性访问,该方法返回对象的副本

When you're trying to assign any value to any of the relation's property (in this case $user->attendance), it will not have any effect on the original object at all, hence the warning.

当您尝试为任何关系的属性(在本例中为 $user->attendance)分配任何值时,它根本不会对原始对象产生任何影响,因此会发出警告。

You can fix this by creating new object instance and add it to createmethod, like this:

您可以通过创建新对象实例并将其添加到create方法来解决此问题,如下所示:

$attendance = new Attendance();
$attendance->present = $present;
$attendance->date    = Carbon::now()->format('d-m-Y');
$attendance->time    = Carbon::now()->format('H-i');
$user->attendance->create($attendance);

This will save the new record with the foreign key.

这将使用外键保存新记录。

回答by Anil Prasad

In my case

就我而言

Laravel php 5.7 & Mysql 5.7 The Error which i got for the following code is Indirect modification of overloaded property Illuminate\Http\Request::${

Laravel php 5.7 和 Mysql 5.7 我为以下代码得到的错误是对重载属性 Illuminate\Http\Request::${ 的间接修改

Error code

错误代码

 $config->name = $request->title;
        $config->lead_by_id = $request->owner;
        $config->tech_stack = $request->stack;
        $config->is_POC = $request->
        $config->updated_by = $request->
        $config->start_date = $request->start_date;
        $config->end_date = $request->end_date;

Then i found that i have not set the request variable properly

然后我发现我没有正确设置请求变量

Fix

使固定

 $config->name = $request->title;
        $config->lead_by_id = $request->owner;
        $config->tech_stack = $request->stack;
        $config->is_POC = $request->poc;
        $config->updated_by = $request->$user->id;
        $config->start_date = $request->start_date;
        $config->end_date = $request->end_date;