Laravel if statement in form generator to select checkbox - php

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' : '') !!}

Related

PHP variable is not assigned

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.)

Laravel pagination link doesn't work during empty input

I am trying to show all data from my table in my laravel blade view.
Controller
public function search()
{
$search = request('show');
$users = User::query();
foreach ($search as $field=>$value)
{
if($value!=NULL)
{
$users = $users->where($field, 'LIKE', '%'.$value.'%');
}
}
if(request()->has('sort'))
{
$order = explode(',',request('sort'));
$users = $users->orderBy($order[0],$order[1]);
}
$users = $users->SimplePaginate(15);
return view('nabil.homepage',compact('users'));
}
Blade
#foreach($users as $user)
<tr>
#if(session()->has('show'))
#foreach(session()->get('columns') as $column)
#if(in_array($column,session()->get('show')))
<td>{{ $user->$column }}</td>
#endif
#endforeach
#endif
</tr>
#endforeach
</table>
{{ $users->appends(Illuminate\Support\Facades\Input::except('page'))->links() }}
If I search something then it works perfectly. But if I search with blank value. Then it doesn't work. The first page comes without any problem but then it breaks.
Case 1 (problematic case):
I input an empty value in search. So the search url is /search?show%5Bbango%5D= .
However, when I click next then the url becomes /search?page=2 (And returns error)
If I manually input /search?show%5Bbango%5D=&page=2 in url address then it works perfectly.
Case 2:
I input some data like '123' in search. Then my url becomes /search?show%5Bbango%5D=123 .
In that case everything works perfectly.
Case 3:
It may be irrelevant. But if I run query in more than one field then it works ok but somehow all empty inputs get removed from url when I use pagination.
I may try to search with two input fields (example: name and bango). If I keep the name field empty and put '123'in bango then the url becomes like /search?show%5Bbango%5D=123&show%5Bname%5D=.
However, if I try to go to next page. then the url becomes /search?show%5Bbango%5D=123&page=2.
Although, the showed results are good but url suddenly drops the name field.
How can I make sure that the pagination still works for empty input. I checked in some similar questions and tried appending query request. But it still doesn't work for me.
Since you are using foreach, it is expecting an array. Try to set
$search = request('show') ?? [];
on the controller.

Redirect back to form with data in laravel

I have this function in the controller.
public function newsedit($id)
{
$editNews = $this->agro->find($id);
//return $editNews;
return Redirect::back()->with('editNews',$editNews);
//return View::make('agro.show')->with('editNews',$editNews);
}
The return $editNews displays data, so there is data in $editNews.Now i am trying to pass the same data to the view using redirect as shown in the above code.
But apparently the value is not passed. The following code shows the value is not availabel in the view
#if(isset($editNews))
<h1> value availabel</h1>
#else
<h1> No value </h1>
#endif
It displays No value . Please help me pass the data to view.I don't understant where have i gone wrong.
Laravel 4:
#if(Session::has('editNews'))
Laravel 5:
#if(session()->has('editNews'))
If you want to get the data, replace has() with get()
return View::make('agro.show', ['editNews' => $editNews]);
You can simply use
#if( Session::get( 'editNews' ) )
// show something
#endif
in Laravel 4 - it will return false to the #if block if the variable is not set

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.

How to display textbox value dynamically which is get from database using laravel

I got value from database and then i need to set those value to textbox. I have created a controller file with the method name of edit look like below
userdata.blade.php:
public function edit($id)
{
echo "You have clicked edit link".$name;
$editdata = DB::table('newuser')->where('Id','=',$id)->get();
return View::make('editdata',array('list' => $editdata));
}
I have passed array of value as parameter to the view file.now i need to diaplay the value of name to textbox.how to do that in html page using laravel. My html page look like below
editdata.blade.php:
<html>
<head></head>
<body>
<div>
{{Form::open(array('url' => 'login', 'method' => 'post'))}}
{{Form::label('name','Name',array('id'=>'label-name'))}}
{{Form::text('name',{{$list->Name}}}}
{{ Form::close() }}
</div>
</body>
</html>
can anyone tell me that what mistake i did.Thanks in advance
Just remove the curly brackets, you are already "inside" PHP code and don't need them:
{{ Form::text('name',$list->Name) }}
Also you get a collection from your controller you probably want to do:
$editdata = DB::table('newuser')->where('Id','=',$id)->first();
Or even:
$editdata = DB::table('newuser')->find($id);
get() returns a collection (multiple rows) and not the model itself. You can use User::find($id) which gives you direct access to the model with the specified Id.
When not using eloquent just replace get() with first()

Categories