Why is blade displaying the model original value and not attribute? - laravel-5

In my controller I am mutating the original value of a model like this;
$tags = [
'[firstName]',
'[city]'
];
$values = [
$recipient['firstName'],
$city['name']
];
$module['content'] = str_replace($tags, $values, $module['content']);
Then in blade I am just trying to use the mutated attribute;
{!! $module['content'] !!}
I can see that my mutation is working when I dd the model but once passed to my view, it is presenting the original value and not the attribute.
Why/how is this?
Thanks

Related

store and update an array as json in mysql database laravel 9

I have values that I want to store in the database, I have declared the type as json and cast it as an array:
protected $casts = [
'favourites' => 'array'
];
however, I'm not sure how to add and update and read the values inside, any ideas?
controller function:
public function addtofav()
{
$id = request()->get('id');
$user = auth()->user();
json_decode($user->favourites,true);
array_push($user->favourites,$id);
dump('hello');
json_encode($user->favourites);
$user->save();
return response(['id'=> $id],200);
}
Quoting from the Laravel documenations:
adding the array cast to that attribute will automatically deserialize
the attribute to a PHP array when you access it on your Eloquent
model.
therefore, no need to make any encoding or decoding to your values before any update or create, just keep it as an array and Eloquent will take care of that, since you have the $cast array as below:
protected $casts = [
'options' => 'array',
];
Actually you should use many to many but if you want to use array, you don't encode or decode.
public function addtofav()
{
$id = request()->get('id');
$user = auth()->user();
$favourites = $user->favourites;
array_push($favourites,$id);
$user->favourites = $favourites
$user->save();
return response(['id'=> $id],200);
}

how to bind the model relation value in livewire wire:model?

There is a Product model with hasmany descriptions relationship.
descriptions relation has two columns, count, body.
How I must define the wire:model value to show the selected product descriptions values in Livewire Update component?
I must mention getting data from relation works fine just can't show data in the input tag of wire:model attribute! I think the problem is on the key definition of protected $rules or wire:model value!
the Update Class:
public $product;
protected $listeners = ['selectedProduct'];
public function selectedProduct($id){
$this->product = Product::with('descriptions')->findOrFail($id);
}
protected $rules = [
"description.count" => "required",
"description.body" => "required",
];
the livewire view:
#if($product)
#foreach($product->descriptions as $description)
<input type="text" wire:model="description.count">
<texatarea wire:model="description.body"></texatarea>
#endforeach
#endif
the loop and number of filed is repeated correct but no data is shown!
There are a few things I'd like to mention, first is that your view should always only have one root HTML element - and subsequent elements that is generated in a loop should also have one unique root element inside, that has wire:key on it. The wire:key should be something unique on your page.
Then we need to have a look at the rules - its correct that any models needs a rule to be visible in input-elements, but you have a collection of elements, so the rules need to reflect that. To do that, you specify a wildcard as the key
protected $rules = [
"description.*.count" => "required",
"description.*.body" => "required",
];
The fields then has to be bound towards an index, so Livewire knows its in a collection.
<div>
#if ($product)
#foreach($product->descriptions as $index=>$description)
<div wire:key="product-description-{{ $description->id }}">
<input type="text" wire:model="descriptions.{{ $index }}.count">
<texatarea wire:model="descriptions.{{ $index }}.body"></texatarea>
</div>
#endforeach
#endif
</div>
Finally, you need to declare the descriptions as a public property on the class, and store them there.
public $product;
public $descriptions;
protected $listeners = ['selectedProduct'];
protected $rules = [
"description.*.count" => "required",
"description.*.body" => "required",
];
public function selectedProduct($id){
$this->product = Product::with('descriptions')->findOrFail($id);
$this->descriptions = $this->product->descriptions;
}

How to directly access JSON data as an object

I have a table where one of the columns has the data type "json". The column is called payment_data and inside of it I have a JSON object:
{"success":true,"status":0,"resposta":"OK","referencia":"44304425","entidade":"5555"}
I found an example online that I could call directly in Laravel, using this example:
{{ $order['payment_data->entidade']}}
But it doesn't work so I guess it is not right. Is there a helper available to get the information directly?
You have to override cast variable in your model:
protected $casts = [
'payment_data' => 'object'
];
Then you can use like this:
{{ $order->payment_data->entidade }}
You can use attribute casting on your model which would allow you to access the JSON data as an array.
For example, you can add the following to your your model class:
protected $casts = [
'payment_data' => 'array',
];
Then you should be able to access values inside the JSON value like this: $order->payment_data['entidade'].
try:
$order = new Order::find( 1 );
$json_data = json_decode( $order->payment_data );
echo $json_data->entidade;
If you have a json like this
{"success":true,"status":0,"resposta":"OK","referencia":"44304425","entidade":"5555"}
you can do like this to access the values
$json = {"success":true,"status":0,"resposta":"OK","referencia":"44304425","entidade":"5555"}
the you can access the values like this
$json = json_decode($json, true);
echo $json['entidade'];
I hope it helps some one,this how i solved my problem.

QueryException in Connection.php line 647:

I'm trying to save checkboxes. I should be able to select one or mutliple.
At the moment I'm getting this error
QueryException in Connection.php line 647: Array to string conversion (SQL: insert into seo (url, meta_title, meta_description, keywords, robot, updated_at, created_at) values (ff, ff, ff, ff, noindex, 2017-03-27 08:59:54, 2017-03-27 08:59:54))
Here is my create.blade.php
<div class="robots">
{!! Form::label('noindex', 'noindex') !!}
{!! Form::checkbox('robot[]', 'noindex') !!}
{!! Form::label('follow', 'follow') !!}
{!! Form::checkbox('robot[]', 'follow') !!}
</div>
my method in my seo controller
public function create()
{
//Gets all the seo that are in the database
$seos = Seo::with('menu')->get();
//Lists the title of the seo
$menu_options = Menu::pluck('title', 'id');
return view('seo::admin.create', compact('seos'))->with('menu_options', $menu_options);
}
public function store()
{
//Gets all the input in the fields in the form
$input = Input::all();
//Checks the input fields against the validation rules in the Seo model
$validation = Validator::make($input, Seo::$rules);
//If the validation fails the a message will pop up saying that there was validation errors
if($validation->fails()){
return redirect()->route('seo.create')
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors');
}
//If the validation passes then it gets saved to the database
if($validation->passes()){
$seos = new Seo();
//Gets the menu_id
$menuId = (array) array_get($input, 'menu_id');
//Saves the input to the database
$seos->fill($input)->save();
//Syncs the menu function in the Seo model to save the menu ID in menu_seo
$seos->menu()->sync($menuId);
$seos = Seo::all();
return view('seo::admin.index', compact('seos'));
}
}
You've trying to save robot array as string. You need to serialize it first. You can do it manually:
$robot = json_encode($input['robot']);
Or you can use array casting feature:
protected $casts = [
'robot' => 'array',
];
The array cast type is particularly useful when working with columns that are stored as serialized JSON. For example, if your database has a JSON or TEXT field type that contains serialized JSON, adding the array cast to that attribute will automatically deserialize the attribute to a PHP array when you access it on your Eloquent model:

How do I pass a selectbox variable to the View from the Controller in CodeIgniter?

I want to pass a variable (selectbox id, which comes from database) to the view from the controller, but do not know how to do it.
Your original question wasn't that clear, but after reading the comments you could do this.
Controller
//whatever function you're using to populate your original array below.
$data['all'] = $this->model_name->getData();
//then run that data through a foreach loop to populate the dropdown variable.
foreach($data['all'])
{
$data['idDropDown'][$data['all']['id']]=$data['all']['ad'];
}
This will pass an array like: [455]=>Aliağa, [456]=>Balçova to the view as $idDropDown along with your original data as $all
Then in the view just used CI's form dropdown.
echo form_dropdown('id',$idDropDown);
Let's say you have a controller called article and a method index then in your view you will have :
<?php
echo form_open('article/index');
echo form_input('text');
echo form_close;
?>
And in your controller something like:
public function index()
{
if($this->input->post()) {
$this_is_catched_text = $_POST['text'];
}
}
This is without validation and other stuff. Just you get the idea how it works.
In your case it will be like this
$ilce = array(array("id" => 455, "il_id" => 35,"ad" => "Aliağa"),
array("id" => 456, "il_id" => 35, "ad" => "Balçova"));
$options = array();
foreach($ilce as $x) {
$options[$x['id']] = $x['ad'];
}
echo form_dropdown('names', $options);

Resources