Thursday, March 2, 2017

Laravel 5: Having trouble updating related model values by using save/push methods, they just don't work

I'm trying to update some values of a related model but after assigning the new values and using save() or push() the values are not updated in database. More than that, execution just stops at those methods and all I can see is a blank page. No errors, no nothing, it just doesn't even reach the return statement. If I use the try-catch, it just skips the save() or push().

Here is the Product model (just without the fields and methods that are not related to what I'm currently trying to do):

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = "products";

    public $timestamps = false;

    public $fillable = [
        ...
    ];

    public function userProduct()
    {
        return $this->belongsTo("\\App\\Models\\UserProduct", "id", "product_id");
    }
}

The UserProduct model with fields which I'm trying to update:

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserProduct extends Model
{

    protected $primaryKey = null;
    public $incrementing = false;
    protected $table = "user_product";

    public $fillable = [
        ...
        "is_featured",
        "is_hidden_from_latest"
        ...
    ];

    public function product()
    {
        return $this->belongsTo("\\App\\Models\\Product", "product_id", "id");
    }

    public function pendingProduct()
    {
        return $this->belongsTo("\\App\\Models\\PendingProduct", "target_product_id", "target_product");
    }

}

Code from the controller:

$replaced_product_sku = Input::get("replaced_sku");
$new_product_sku = Input::get("new_sku");

$products = Product::with([
    "userProduct" => function($q) {
        $q->orderBy("updated_at", "asc");
    }
])->where("product_status", "live")->get();

if (!$found_replaced = $products->where("item_sku", $replaced_product_sku)->first()) {
    return redirect("admin/content")
        ->with("danger", "Replaced product was not found.");
}

if (!$found_new = $products->where("item_sku", $new_product_sku)->first()) {
    return redirect("admin/content")
        ->with("danger", "The new featured product was not found.");
}

$found_replaced->userProduct->is_featured = 0;
$found_replaced->userProduct->is_hidden_from_latest = 1;
$found_new->userProduct->is_featured = 1;
$found_new->userProduct->is_hidden_from_latest = 0;

$found_replaced->userProduct->save();
$found_new->userProduct->save();

return redirect("admin/content")
    ->with("...", "...");

Tried using push() method instead of save() but the only thing that happens is that execution stops at $found_replaced->userProduct->save(); and a blank page is displayed. Also tried something like this:

$found_replaced->update([
    "userProduct.is_featured" => 0,
    "userProduct.is_hidden_from_latest" => 1
]);

$found_new->update([
    "userProduct.is_featured" => 1,
    "userProduct.is_hidden_from_latest" => 0
]);

but still without success.




via Eseth

Advertisement