Skip to content

Instantly share code, notes, and snippets.

@nikhilj1
Created July 22, 2020 09:48
Show Gist options
  • Save nikhilj1/a0cc3560b444d0d1c49e18700c05b519 to your computer and use it in GitHub Desktop.
Save nikhilj1/a0cc3560b444d0d1c49e18700c05b519 to your computer and use it in GitHub Desktop.
Error While Testing with Faker Code.
<?php
true
array:4 [
0 => 1
1 => 2
2 => 3
3 => 4
]
[]
App\Post]8;;file://F:\Code\myGist\app\Post.php#L35\^]8;;\ {#4456
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: array:1 [
"category_id" => 1
]
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
* App\Post
*/
class Post extends Model
{
/**
* Set the tags
*
* @param array $value
* @return string
*/
public function setTagsAttribute($value)
{
dump(!empty($value));
//if (!empty($value))
return (implode(',', $value));
}
}
<?php
namespace App\Http\Controllers;
use App\Category;
use App\Post;
use App\Tag;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$post->category_id = $request->get('category_id');
//$post->tags = $request->get('tags');
if(!empty($request->get('tags')) ) {
$post->tags = $request->get('tags'); // Triggers Mutator Post.php@setTagsAttribute output true
dump($request->get('tags')); // output: array:4 [ 0 => 1 , 1 => 2 , 2 => 3 , 3 => 4 ]
dump($post->tags); // output: []
}
else
$post->tags = '';
dd($post); // Here there is not even a tags in attribute
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment