candyy
7 months ago

Laravel 5.0.13 not passing trough update function

Posted 7 months ago by candyy

Hello! I've been handed some legacy code in laravel... The idea is simple - the app has products, categories, and articles

The issue - we did not have a controller to add/edit categories. I tried to replicate the code from ProductsController but i'm stuck at updating some fields.

My code:

/* CategoriesController.php */

public function update(Requests\UpdatePageRequest $request, $id)
    { dd('update');
        $input = $request->all();

        $category = Category::find($id);
        $category->name_ro = $input['name_ro'];
        
        $category->name_en = $input['name_en'];
        $category->save();

        return redirect('appmin/categories');
    }
/* edit.blade.php */

 {!! Form::model($category, array('route' => array('appmin.categories.update', $category->id), 'method' => 'PUT')) !!}
  <div class="form-group">
                  {!! Form::label('name_ro', 'Name RO') !!}
                  {!! Form::text('name_ro', Input::old('name_ro'), array('class' => 'form-control')) !!}
</div>

            {!! Form::submit('Savge', array('class' => 'btn btn-primary')) !!}

          {!! Form::close() !!}
/*Category.php*/
<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model {
    
    protected $fillable = [
                'name_ro',
                'name_en',
    ]; 

    public function products() {

        return $this->belongsToMany('App\Product')->orderBy('sort_by','asc')->withTimestamps();

    }

    public function getNameAttribute()
    {
            $locale = \Session::get('locale');

            if ( $locale != null )  {
                $column = "name_" . $locale;
            }   else {
                $column = "name_ro";
            }
            return $this->{$column};
    }


}

/*Product.php*/
<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model {


    protected $fillable = [
                'cat_id',
                'title_ro',
                'title_en',
                'content_ro',
                'content_en',
                'slug_ro',
                'slug_en',
                'image',
    ];
...
/*ProductsController.php*/
public function update(Requests\UpdatePageRequest $request, $id)
    {


        $input = $request->all();
    //  dd($input['categories']);
        $product = Product::find($id);
        $product->title_ro = null;
        $product->title_ro = $input['title_ro'];
        $product->content_ro = $input['content_ro'];
        $product->title_en = $input['title_en'];
        $product->content_en = $input['content_en'];
        $product->slug_ro = Str::slug($input['title_ro']);
        $product->slug_en = Str::slug($input['title_en']);
        $product->action_ro = $input['action_ro'];
        $product->action_en = $input['action_en'];
        $product->use_ro = $input['use_ro'];
        $product->use_en = $input['use_en'];
        $product->package_ro = $input['package_ro'];
        $product->package_en = $input['package_en'];
        $product->price = $input['price'];
        if(array_key_exists('image', $input)) {

        $image = $input['image'];
        $filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
        $path = (public_path().'/products/' . $filename);
        Image::make($image->getRealPath())->save($path);

        $product->image = '/products/'.$filename;
        }

        $product->save();

        $product->categories()->sync($input['cat_list']);

        return redirect('appmin/products');
    }

I receive errors for not filling fields regarding products ( 'content_ro', 'title_ro', etc)

I'm simply stuck.

I have no idea what I should do/check/test ...

Please sign in or create an account to participate in this conversation.