PHP variable is not assigned - php

I'm working on a laravel project.
now I want to access one of my column which is data
The data colmn contains an array of stuff like message, klant_address, reactionPlacer and more.
foreach(Auth::user()->unreadNotifications as $notfiy) {
{{ $notify->data['klant_address'] }} //This gives an error "variable is not assigned"
}
but if i store it first to a variable it does'nt gives an error like this
foreach(Auth::user()->unreadNotifications as $notfiy) {
$klantAddress = $notify->data['klant_address']
echo $klantAddress
}
Now comes the weirdest part of all
if i do
dd($notify->data['klant_address']) <-- this does gives me the signle string return
and
foreach(Auth::user()->unreadNotifications as $notfiy) {
{{ $notfiy->data['messages'] }} <--- This does output
}
does work.
I hope someone can help me or at least explain why this is happened. Because I'm so confused for it.
thanks in advance.
Error Page
Database Table

Looks more like JSON to me. (Look at the last image)
Try this:
{{ $notify->data->klant_address ?? '' }}
If that doesn't work, try {{ json_encode($notify->data)->klant_address ?? '' }}
(Thrown in ternaries to help with nulls etc.)

Related

Impossible to access an attribute on a integer variable

I am trying to access the value in twig from:
image of data dump
But keep getting an error:
Impossible to access an attribute ("startDate") on a integer variable ("2").
And in my twig it's:
{% for item in data %}
...
{{ item.decoded.startDate }}
...
In Controller:
foreach ($data as $d) {
$d->decoded = json_decode($d->getValue());
}
Can somebody help skipping the error when the value is integer or null?
Try to dump item.decoded to see if startDate is available

Laravel if statement in form generator to select checkbox

I want to automatically select a checkbox when the field in the database is filled in.
I am trying to use #if statement in the Form generator but it is not checking the checkbox.
Here is the code I am using:
{!! Form::checkbox('offer_made', 'offer_made', #if(empty($phase_2->offer_made)) 1 #endif) !!}
Im sending this over to the view in my Controller:
public function show(Order $order)
{
$order = Order::where('id', $order->id)->first();
$current_phase = $order->current_phase;
if($current_phase == 1) {
$phase_2 = Order_Phase_2::where('order_id', $order->id)->first();
return view('orders.phase-2', compact('order', 'phase_2'));
}
}
When I echo $phase_2->offer_made in the view it shows 1 so the value is coming through but the if statement is not working inside the Form builder.
Anyone knows how to fix this?
Thanks already!
You might be checking for the value incorrectly:
#if(empty($phase_2->offer_made)) 1 #endif)
This outputs a 1 if the value is empty. You should be checking for !empty() if I understand the field correctly.
So you might have success with this:
#if(!empty($phase_2->offer_made)) 1 #else 0 #endif)
Is the value 1/0 itself? Use $phase_2->offer_made directly as the third parameter.
use ternary operator as you cannot use blade syntax in php code. Try something like this:
{!! Form::checkbox('offer_made', 'offer_made',(!empty($phase_2->offer_made)) ? 'checked' : '') !!}

Blade foreach cast output to an object

I am trying to figure out how I can give my data output in the blade file, the Laravel look;
like $data->name
But I can't get the output to be casted as an object. I think I have to make an array of the data before I can loop it proper in a foreach but this doesn't feel like the right way.
I am relatively new to Laravel and I want to do this the nice way, can someone point me in the right direction? Thanks in advance
Controller:
$data = collect($this->api->organization->index())->toArray();
return View::make('pages.organization.index', array('data' => $data[0]));
View:
#foreach($data as ((object)$organization))
{{ $organization->name }}
#endforeach
I know this will not work, but I think it illustrates my question a little bit.
EDIT
What I didn't realize is that $data = collect($this->api->organization->index()); is returning an array with all the data arrays inside because I didn't name it in my return like this:
return (object)['all' => $data];
After adding all I could reference the code inside my view like I wanted to. I know this is not a very detailed answer, if you run into the same problem message me I'll edit the answer.
Object:
$data = collect($this->api->organization->index());
#foreach($data as $organization))
{{ $organization->name }}
#endforeach
Array:
$data = collect($this->api->organization->index())->toArray();
#foreach($data as $organization))
{{ $organization['name'] }}
#endforeach

Changing the {{ URL::to('home'); }} in laravel

Here is my urlto in laravel
{{ URL::to('forgot'); }}
It will point as mysite.com/forgot
How can i insert the value here
i.e.,
$value = '1';
{{ URL::to('forgot'); }}
And it should point mysite.com/1/forgot
How can i insert the value of $value before the forgot
You should concatenate your $value before your url
i.e.,
$value = '1';
{{ URL::to($value.'/forgot/'); }}
You can use named routes with parameters.
Example route:
Route::get('{value}/forgot', array('as' => 'forgot', function($value = null)
{
return "My value: " . $value;
}));
Now building your URL:
{{ route('forgot', 1); }}
You need to use like this check the documentation Documentation Here
$value = '1';
{{ URL::to($value.'/forgot'); }}
one way is to add a variable to your route
an example here my route is
Route::get('/user/{id}'
then at the destination if i call $id, i get the value that was entered in id.
so if i put /user/1 , $id =1.
this can be a issue however if there is nothing. you need to include code to handle if the $id has no value. another way to do it is
return View::make('searchresults', compact('challenges', 'ideas','users'));
here i have sent arrays with search results, and on the page searchresults if i call $ideas i get that array, this will also work with strings i believe.

Laravel form sort of submitting in debug mode but doesn't work in normal mode

so I have a selection box that gives a dropdown menu to give messages a manager from the dropdown. It takes the input and then changes to a column in the database called manager for it's respective column. When I try to submit the selection menu it gives me the regular error for Laravel. But then when I put ?debug=1 at the end it submits but gives the row's manager column a value of just blank.
Here is what I have in the routes.php
Route::get('foo/{id}', 'fooController#bar');
Route::post('foo/{id}', 'fooController#bar');
This is the form.
{{ Form::open(array('url' => '/admin/foo' . $message->id)) }}
{{ Form::select('handler[]', array('unassigned', 'foo', 'bar'), null, array('style' => 'width: 127px')); }}
{{ Form::submit('Change manager') }}
{{ Form::close() }}
{{ $message->manager }}
and here is what is in the fooController
public function bar($id = null)
{
$message = Message::find($id);
$handler = Input::get('handler[]');
$message->manager = $handler;
$message->save();
return Redirect::action('AdminController#foo_bar');
}
I had a problem like this the other day, I have zero recollection of what I did. I really appreciate any help, thanks! The database is postgresql if that's any help
Try a dd(Input::all()) at the beginning of your controller and make sure you're seeing what you expect.
Also since you're sending an array perhaps you have to do Input::get('handler.0') -- see here right below the Input::only() and Input::except() code block.
It would seem as though because you are naming your select handler[], PHP is grabbing it as part of an array.
When setting up your message model, try this...
public function bar($id = null)
{
$message = Message::find($id);
$handler = Input::get('handler[]');
$message->manager = $handler[0];
$message->save();
return Redirect::action('AdminController#foo_bar');
}
Usually, you'd only use names in your forms post-fixed with [] when you are accepting multiple values like checkboxes/multi-selects etc... Otherwise, it's probably best to stick with not using it because it may cause confusion.
I managed to fix it in a almost frustratingly simple way by just changing the method to PUT.
like this
Form::open(array('url' => 'foo/bar', 'method' => 'put'))

Categories