4

Data are stored as ["item_1", "item_2"] in database like shown below.

I want to display those data in view blade properly.

Items Model

Product Model

protected $fillable = ['name', 'prod_id'];
public function models() {
    return $this->hasMany(Model::class, 'prod_id');
}

Model Model

protected $fillable = ['model', 'prod_id'];
protected $cat = ['model'=>'array'];
public function model()
{
 return $this->belongsTo(Product::class, 'prod_id');
}

Controller - store method

public function create (Request $request, Product $product){
$models = new Model;
    {
     $model = json_encode(request('models'));
     $items->models = $model;
     $product->models()->save($models);
    }
}

Controller show method

public function show(request $id){
 $product = Product::findorfail($id);
 $models = Model::with(['model'])->where('prod_id', $product->id)->get();
 return  view ('show',  compact('product', 'models'));

Create View

<input type="checkbox" name="model[]" value="Samsung">
<input type="checkbox" name="model[]" value="Nokia">
<input type="checkbox" name="model[]" value="Apple">
<button>Add Model</button>

I tried show view:

@foreach($models as $model)
  {{ json_decode($model->models) }}
@endforeach

It throws

htmlspecialchars() expects parameter 1 to be string, array given

What am I missing.

PS: MySQL does not support json column, so I saved as text column.

4
  • What is your output of $models? Commented Mar 26, 2018 at 15:04
  • The controller code you've provided does not make any sense. It appears to be a copy/paste error. Please correct the question. Commented Mar 26, 2018 at 15:04
  • @Nawin Its ``{"id":18,"prod_id":22,"models":{"id":22,"user_id":1,``` Commented Mar 26, 2018 at 15:14
  • @patricus Updated with show method. Commented Mar 26, 2018 at 15:16

3 Answers 3

2

you need to do someting like this.

Model Model

protected $fillable = ['models', 'prod_id']; // screenshot says that the field name is "models"

protected $cast = ['models' => 'array']; // the property is $cast no $cat  

public function product()
{
 return $this->belongsTo(Product::class, 'prod_id');
}

ModelController - store method

public function store (Request $request){  

  $product = Model::create([
    'models' => json_encode($request->models),
    'prod_id' => $request->prod_id
  ]);  

  return redirect()->back()->with('success', 'created!');
}

public function show(Request $id){

  $model = Model::findOrFail($id)->with('product');

  return  view ('model.show',  compact('model'));
}

ProductController show method

public function show(request $id){
 $product = Product::findOrFail($id)->with('models'); // the method name is findOrFail() no findorfail 
 // $models = Model::with(['model'])->where('prod_id', $product->id)->get();
 return  view ('show',  compact('product'));
}

Into the show View

@foreach($product->models as $models)
  @foreach(json_decode($models->models) as $model)
    {{ $model }}
  @endforeach
@endforeach
Sign up to request clarification or add additional context in comments.

3 Comments

What should I do to achieve the same for multiple fields? Like prod_id, model_name to pass through json/array in a single column as done for size above?
Look my answer in this link. I think is the answer for your question. stackoverflow.com/questions/49269528/…. If not make a new question and i'll try to help you again
I have 3 fields name, address, profile_photo. Here is new Question: https://stackoverflow.com/questions...
2

Your Models Model confuses me a little bit. You seem to have a field name model that's the same as a relationship method name. That means whenever you include that relation, it'd functionally override that property with data from the related table. (I say 'functionally' because you're using dynamic properties, whereas it is actually possible to explicitly tell Eloquent whether you want an attribute or relation without making it guess.)

That said, your $model->models property could be coming back as an array for one of two reasons. The first is that it may be accidentally referring to a relational data-set and not the JSON string you were expecting. The second is you've corrected the protected $cat = ['model'=>'array']; to read protected $cast = ['models'=>'array'];, and it's stepping on your toes now. By casting it to an array, it may be getting automatically get interpreted back into one before you call json_decode on it.

Either way, I'd dd($model->models) to see what it is first.

4 Comments

dd($models->models) show an array of models Collection {#209 ▼ #items: array:1 [▼ 0 => "["Samsung","Nokia","Apple"]" ] } . Nested ``foreach` loops solved.
The collection result of $models = Model::with(['model'])->where('prod_id', $product->id)->get();. That's a many-to-one relationship though, right? Replace get with first.
Property [models] does not exist on this collection instance.
My bad, I think you had it right the first time with nested loops.
1

You need to change your foreach like this:

@foreach($models->models as $model)
  {{ json_decode($model) }}
@endforeach

because Your array is like this

{"id":18,"prod_id":22,"models":{"id":22,"user_id":1}}

In here the $models is getting only id and prod_id models is still array so your foreach should be @foreach($models->models as $model)

Sample Code is here:

$arr = '{"id":18,"prod_id":22,"models":{"id":22,"user_id":1}}';
echo '<pre>';
foreach (json_decode($arr->models) as $str){
    echo $str;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.