How to use json decoded data in blade laravel - php

part of this data in store method decoded and i want to use this encoded data in blade template:
How is this possible?
public function store(VehiclesRequest $request)
{
$data = Vehicle::create([
'container_type' => $request->container_type,
'type' => $request->type,
'delivery_capacity' => $request->delivery_capacity,
'plate_number' => json_encode($request->plate_number),
'chasis_number' => $request->chasis_number,
'capacity_dimensions' => json_encode($request->capacity_dimensions),
'fuel_consumption_rate' => $request->fuel_consumption_rate,
'capacity_weight' => $request->capacity_weight,
'insurance_expire_date' => json_encode($request->insurance_expire_date),
'insurance_type' => $request->insurance_type,
'is_available' => $request->is_available,
]);
return redirect()->action('VehicleController#index');
}
VehicleController:
public function index()
{
$vehicles = Vehicle::all();
return view('vehicle.index', compact('vehicles'));
}
view:
<tbody>
#foreach(vehicles as $vehicle)
<tr>
<td>{{ $vehicles->plate_number }}</td>
<td>{{ $vehicles->delivery_capacity }}</td>
</tr>
#endforeach
</tbody>
i used from {{ json_decode($vehicles->plate_number) }} in blade template but an error occurred.

You can Decode json data in view.blade.php like this :
<tbody>
#foreach($vehicles as $vehicle)
<tr>
<td>
#foreach(json_decode($vehicles->plate_number) as $plate)
{{ $plate['variable_name'] }}
#endforeach
</td>
<td>{{ $vehicles->delivery_capacity }}</td>
</tr>
#endforeach
</tbody>

Related

How to pass a Static value with search results in a Laravel Controller

I have the following method defined in my controller. I want to pass the value of $title along with the search results so that it can be displayed at the top of the blade page, but I am unsure how to do it.
public function index_sog(Request $request)
{
$title = 'Standard Operating Guideline';
return view('knowledgebase.index', [
'kbase' => Knowledgebase::orderBy('category', 'asc')
->filter(request(['tags', 'search']))
->where('type','SOG')
->paginate(20),
'search' => $request->input('search')
]);
}
My output...
<h4>{{//TITLE SHOULD GO HERE//}}</h4>
<div class="panel-container show">
<div class="panel-content">
#foreach ($kbase->groupBy('category') as $category => $group)
<table class="table">
<tr>
<th colspan="3" class="text-center bg-fusion-50"><strong>{{ $category }} <strong></th>
</tr>
#foreach ($group as $kb)
<tr>
<td>{{ $kb->title }}</td>
<td></td>
<td></td>
</tr>
#endforeach
</table>
#endforeach
</div>
</div>
ADD on return variables. And you can use on blade like {{ $title }}
> return view('knowledgebase.index', [
> 'kbase' => Knowledgebase::orderBy('category', 'asc')
> ->filter(request(['tags', 'search']))
> ->where('type','SOG')
> ->paginate(20),
> 'search' => $request->input('search'),
> 'title' => $title
> ]);
For example you can do this way:
return view('knowledgebase.index', [
'kbase' => Knowledgebase::orderBy('category', 'asc')
->filter(request(['tags', 'search']))
->where('type','SOG')
->paginate(20),
'search' => $request->input('search')
])->with('title', $title);
By adding the ->with() method to the return.
You can also put it inside the array of variables that you already have in return.
And then in your view:
<h4>{{ $title }}</h4>

Having problem with retriving json data in laravel

I'm working on a laravel project and i'm getting JSON error. I've try few options to solve the issue but did not come up with any solutions.
In this project I've to send multiple data into database onder one "ID". In that ID there are at list 10 data need to insert. So I used JSON format. It saves ok but getting error when I try to pull data.
Here is my code
Controller :
public function newExpenses(Request $request){
$Jexpenses = new Jexpenses;
$json = array(
'des' => $request->description,
'valu' => $request->expenses
);
$Jexpenses->voucher = $request->voucher_no;
$Jexpenses->date = $request->date;
$Jexpenses->description = json_encode($json);
$Jexpenses->expenses = $request->total;
$Jexpenses->remarks = $request->remark;
if($Jexpenses->save()){
$notification = array(
'message' => 'Expenses Saved',
'alert-type' => 'success'
);
}
return redirect()->back()->with($notification);
}
result :
{"des":["Jon","Sam","Pitter"],"valu":["20","30","15"]}
Return Controller:
public function expenses(){
$data = array(
'title' => 'Expenses',
'expenses' => Jexpenses::all()
);
return view('expenses.expenses')->with($data);
}
html :
#foreach ($expenses as $key => $getData)
{{ $array = #json( $getData->description ) }}
<tr>
<td>{{ ++$key }} </td>
<td>{{ $getData->voucher }} </td>
<td>{{ $getData->date }} </td>
#foreach ($array->description as $k => $v)
<td>{{ $array->des['$k'] }}</td>
<td>{{ $array->valu['$k'] }}</td>
#endforeach
<td>{{ $getData->expenses }}</td>
<td>{{ $getData->remarks }}</td>
</tr>
#endforeach
it should be work fine but getting error
error:
syntax error, unexpected '<' (View: C:\xampp\htdocs\acSoft\resources\views\expenses\expenses.blade.php
Need some help. Thanks.
1) Use json_decode($getData->description) instead #json. The #json is shortly form of json_encode
2) $array->description not exists property. Should be $array->des
3) You use variable in ' quotes. You should use " quote or nothing round variables.
4) It is bad idea using reserved words as variable names. I am about variable $array
#foreach ($expenses as $key => $getData)
{{ $descriptionData = json_decode( $getData->description ) }}
<tr>
<td>{{ $key + 1 }} </td>
<td>{{ $getData->voucher }} </td>
<td>{{ $getData->date }} </td>
#foreach ($descriptionData->des as $k => $v)
<td>{{ $descriptionData->des[$k] }}</td>
<td>{{ $descriptionData->valu[$k] }}</td>
#endforeach
<td>{{ $getData->expenses }}</td>
<td>{{ $getData->remarks }}</td>
</tr>
#endforeach

Data doesn't display

Need some help here, I'm using Laravel app, my problem is my data wont display on the table. I tried some ways to display my data but it doesn't work.
Here's my code.
<tbody>
#if (count($expenses) > 0)
#foreach ($expenses as $expense)
<tr data-entry-id="{{ $expense->id }}">
<td field-key='expense_category'>{{ $expense->expense_category->name or '' }}</td>
<td field-key='entry_date'>{{ $expense->entry_date }}</td>
<td field-key='amount'>{{ $expense->amount }}</td>
<td field-key='created_by'>{{ $expense->created_by->name or '' }}</td>
<td>
#can('view')
#lang('quickadmin.qa_view')
#endcan
#can('edit')
#lang('quickadmin.qa_edit')
#endcan
#can('delete')
{!! Form::open(array(
'style' => 'display: inline-block;',
'method' => 'DELETE',
'onsubmit' => "return confirm('".trans("quickadmin.qa_are_you_sure")."');",
'route' => ['expenses.destroy', $expense->id])) !!}
{!! Form::submit(trans('quickadmin.qa_delete'), array('class' => 'btn btn-xs btn-danger')) !!}
{!! Form::close() !!}
#endcan
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="9">#lang('quickadmin.qa_no_entries_in_table')</td>
</tr>
#endif
</tbody>
here is my expenseController.
but i have never touched this code, i always work on the table form.
class ExpensesController extends Controller
{
/**
* Display a listing of Expense.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
if (! Gate::allows('expense_access')) {
return abort(401);
}
if ($filterBy = Input::get('filter')) {
if ($filterBy == 'all') {
Session::put('Expense.filter', 'all');
} elseif ($filterBy == 'my') {
Session::put('Expense.filter', 'my');
}
}
$expenses = Expense::all();
return view('admin.expenses.index', compact('expenses'));
}
}
try to find are you getting data from DB or not ....
$expenses = Expense::all();
dd($expenses);
It will show your data collection.
As I Think you are not getting any data from here.

Laravel Parameter passing error from controller to view

I want to pass the parameter $questions to view, but it gives the following error:
ErrorException (E_ERROR)
Undefined variable: questions (View: C:\Users\Krishan\Documents\GitHub\GroupProject\lcurve\resources\views\quizz\questions\index.blade.php)
This is my controller index function part:
public function index()
{
$questions = Question::all();
return view('quizz/questions.index', compact('questions'));
}
This is a part Of my view:
<tbody>
#if (count($questions_options) > 0)
#foreach ($questions_options as $questions_option)
<tr data-entry-id="{{ $questions_option->id }}">
<td></td>
<td>{{ $questions_option->question->question_text or '' }}</td>
<td>{{ $questions_option->option }}</td>
<td>{{ $questions_option->correct == 1 ? 'Yes' : 'No' }}</td>
<td>
View-->
<!--Edit-->
{!! Form::open(array(
'style' => 'display: inline-block;',
'method' => 'DELETE',
'onsubmit' => "return confirm('".trans("quickadmin.are_you_sure")."');",
'route' => ['questions_options.destroy', $questions_option->id])) !!}
{!! Form::submit(trans('quickadmin.delete'), array('class' => 'btn btn-xs btn-danger')) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="5">no_entries_in_table</td>
</tr>
#endif
</tbody>
enter image description here
where is questions_options coming from? You are passing questions. So your for loop should be
#if (count($questions) > 0)
#foreach ($questions as $question)
//rest of your code
#endforeach
#endif
and your return view part can be return view(quizz.questions.index, compact('questions'))
Firstly, the error message you have mention should be shown. The error message should be:
Undefined variable: questions_options (View:C:\Users\Krishan\Do........
Because you are passing questions to view but you are accessing question_options in view. So, it should say question_options in undefined in view.
Besides, do you know you can avoid this count check? You can use laravel's forelse tag here a below:
#forelse($questions as $question)
//Your table goes here
#empty
<tr>
<td colspan="5">no_entries_in_table</td>
</tr>
#endforelse

Laravel 3: with_input() not saving input

I have a Laravel form that gets passed through some validation. Obviously, if the validation fails, I'd like the user to be redirected to form and alerted of their mistake. Also, because the form is fairly large, I don't want the user to have to re-input any data. Here is the form (I've cut off a large part to reduce the size of this message)
:#layout('templates.main')
#section('content')
#if(Session::has('validation_errors'))
<ul class="form_errors">
#foreach($errors as $error)
{{$error}}
#endforeach
</ul>
#endif
{{ Form::open('account/create', 'POST') }}
<table>
<tr>
<td>{{ Form::label('dealer', 'Dealer') }}</td>
<td>{{ Form::select('dealer', array('1' => 'Dealer #1', '2' => 'Dealer #2')) }}</td>
<td>{{ Form::label('amount', 'Amount') }}</td>
<td>{{ Form::text('amount', Input::old('amount')) }}</td><!--Here is where I'm testing for old input-->
</tr>
<tr>
<td>{{ Form::label('date', 'Date') }}</td>
<td>{{ Form::date('date', NULL, array('class' => 'date_picker')) }}</td>
<td>{{ Form::label('installation', 'Installation #') }}</td>
<td>{{ Form::input('text', 'installation') }}</td>
</tr>
<tr>
<td colspan="4">{{ Form::textarea('notebox', NULL, array('id' => 'notebox')) }}</td>
</tr>
<tr>
<td colspan="4">{{ Form::submit('Submit') }} {{ Form::reset('Reset') }}</td>
</tr>
</table>
{{ Form::close() }}
#endsection
When the form is submitted, here is the controller that handles it:
public function post_create() {
//Validate it in the model
$validation = Account::validate(Input::all());
if($validation->fails()){
return Redirect::to('account/create')
->with_input()
->with('validation_errors', true)
->with('errors', $validation->errors->all('<li>:message</li>'));
}
else {
return "passed";//for debugging purposes
}
}
And lastly, here is the Account model:
class Account extends Eloquent {
public static function validate($input) {
//Validation rules
$rules = array(
'firstname' => 'required',
'lastname' => 'required',
//etc...
);
//Custom validation messages
$messages = array(
'firstname_required' => 'A first name is required.',
'lastname_required' => 'A last name is required.',
//etc...
);
//Pass it through the validator and spit it out
return Validator::make($input, $rules, $messages);
}
}
So when I go to test this by intentionally making the form invalid, I am redirected to the account/create form, however, no validation errors show, and the old input is not saved (I only attached Input::old() to the amount textbox).
It turns out that if the total number of characters from Input::old() exceeds ~1400 characters, things get a little buggy. I solved this by changing from 'driver' => 'cookie' to 'driver' => 'file' in my application/config/session.php and it began to work as expected.
https://github.com/laravel/laravel/issues/1475

Categories