0

I'm trying to edit a column of datatable to show informations in my index view table.

This is my controller's method that I call throught Ajax in the view:

public function getField(){

       // Current User
        $user_id = \Auth::user()->id;
        $users = User::find($user_id)->users();
        return Datatables::of($users )
          ->editColumn('id', '<a href="'.url('users').'/{{$id}}/edit">Edit</a>')
          ->editColumn('fields_id', function($users) {
             $mystring = '';
             $fields_id_array = json_decode($users->fields_id);
             foreach($fields_id_array as $f_id){         

                /* this works */  
                // $mystring  .=  Fields::where('id', '=', $f_id) -> pluck('field_name');

                /* this doesn't work */  
                $mystring  .=  Fields::find($f_id) -> pluck('field_name');
             }
             return $mystring;
          })
          ->rawColumns(['id'])
          ->make(true);
      }

The field 'fields_id' in users table is a JSON field like this: ['1','2','5'] and these refers to the id's of another table: 'Fields'.

I have already read this questions but I think my db table is it ok because increments are primary-key equivalent in Laravel migrations.

This is my migration for the table 'fields':

public function up()
    {
        Schema::defaultStringLength(191);
        Schema::create('fields', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users');
            $table->string('code')->unique();
            $table->string('param1');
            $table->string('param3')->nullable();
            $table->timestamps();
        });
    }

Thanks

Roberto Remondini
  • 242
  • 1
  • 6
  • 19
  • are you using soft deletes? The query builder where also returns (finds) "soft deleted" entries, where as find does not include those. Also, what type is your $az_id variable (and what is it's value)? – flynorc Jan 03 '18 at 14:48
  • No I'm not using soft deletes. I forgot to add that find($az_id) return to me all the records of 'Fields' table. – Roberto Remondini Jan 03 '18 at 15:25
  • I'm sorry. $az_id is wrong. The correct name is $f_id and it is an integer. – Roberto Remondini Jan 03 '18 at 15:32
  • hmm... what version of laravel are you using? did you try to check what you get back if you try to run the two statements in "tinker" mode? – flynorc Jan 03 '18 at 16:53
  • I'm using Laravel 5.4 version. I don't know what is it 'tinker' mode but if I try this, outside of the loop: Fields::find(1) -> pluck('field_name'); I get back this [''Name1","Name2", ... "NameN"] – Roberto Remondini Jan 03 '18 at 17:35

1 Answers1

1

a wild guess here, based on your last comment...

try this inside you loop

$mystring  .=  Fields::find(intval($f_id))->pluck('field_name');

I've added the intval() function arround your $f_id since you said you said they are in this format ['1','2','5']. that is an array of strings (that should be cast to integers if you plan to use them with find() function.

flynorc
  • 829
  • 6
  • 13
  • I have tried this: $mystring .= Fields::find(intval($f_id))->pluck('field_name'); but doesn't work. I can do it also with this: $fields_id_array = json_decode($user->fields_id); $fields_array = Fields::whereIn('id',$fields_id_array)->pluck('field_name')->toArray(); and it works but I can't understand why the find does not work – Roberto Remondini Jan 04 '18 at 07:53
  • and if you dd($fields_id_array); just before the loop? because like you said... if you call Fields::find(1) -> pluck('field_name'); you do get back the array of what you want, that is why i'm thinking the problem is with the value you pass as the argument – flynorc Jan 04 '18 at 09:29
  • I can't call dd() but print_r($fields_id_array) returns this: Array ( [0] => 1 [1] => 3 ) In this case my loop get out twice all the records : ["Name1","Name2","Name3"] ["Name1","Name2","Name3"] – Roberto Remondini Jan 04 '18 at 14:30
  • I add another detail to question. If I call the method getKeyName(); in this way: (new Fields)->getKeyName(); it get out the correct key 'id'. – Roberto Remondini Jan 04 '18 at 14:31
  • sorry, i ran out of ideas to try... just to check i understand the problem correctly... when you run the $mystring .= Fields::where('id', '=', $f_id) -> pluck('field_name'); inside the loop, you get the results back correctly and if you run $mystring .= Fields::find($f_id) -> pluck('field_name'); then you get nothing? Also as i wrote this, have you tried printing (var_dump) the result you are trying to append to $mystring (in case that is an array that could be causing some issues) – flynorc Jan 04 '18 at 18:17
  • In the first case I get out the correct results instead with $mystring .= Fields::find($f_id) -> pluck('field_name'); I get out all the records so many times how many are the loops. Find() doesn't filter results and return the same data as Fields::all() -> pluck('field_name') – Roberto Remondini Jan 05 '18 at 09:00