universal
universal

Reputation: 467

Laravel - Display Array/String Object in view from database

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.

Upvotes: 4

Views: 7251

Answers (3)

kmuenkel
kmuenkel

Reputation: 2789

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.

Upvotes: 2

Maru Amallo
Maru Amallo

Reputation: 430

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

Upvotes: 2

Nawin
Nawin

Reputation: 1692

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;
}

Upvotes: 1

Related Questions