Pass a variable to an other view - php

I try to load a view(project_template) in an other view.
My problem ist, how i can give my $project-variable to the 'project_template'-view which i create in my main-view with "View:make".
Actually i use
->with($project)
But when I try to use it in my template_view, i get an non-object-error
I also used "compact" what also not worked for me.
In my main-view i can use the $project-variable without problems
View:
#foreach($projects as $project)
<tr class="gradeA">
<td>
<td>{!! $project->description !!}</td>
<td>
{!! View::make('shared.tables.project_template')->with($project)->render() !!}
</td>
</tr>
#endforeach
Project_Template-View:
{!! $project->project_name !!}
Error-Message
ErrorException in 0f9dcdc70fd0be3b50d7a39ad7bc90d7 line 4:
Trying to get property of non-object
Thanks for the help!

Change to this:
#foreach($projects as $project)
<tr class="gradeA">
<td>
<td>{!! $project->description !!}</td>
<td>
#include('shared.tables.project_template')
</td>
</tr>
#endforeach

Simply use the #include(viewName, [data]).
That means, in your case:
#include('shared.tables.project_template', ['project' => $project])

Related

Cannot use object of type stdClass as array in laravel 5.4

This is my controller function
public function plist(){
$plists = DB::table('panchayaths')->get();
return view('webapp.panchayath list', ['plists' => $plists]);;
}
This is my view
#foreach ($plists as $plist)
<td> {{$plist['id']}}</td>
<td> {{$plist['pname']}}</td>
<td> {{$plist['total_ward']}}</td>
#endforeach
did you try $plist->id. The get response is an object not an associative array.. if you want to use it as array in you blade add ->toArray() after the get().
in controller:
public function plist()
{
$plists = DB::table('panchayaths')->get()->toArray();
return view('webapp.panchayath list', ['plists' => $plists]);
}
OR if use Eloquent ORM:
public function plist()
{
$plists = Panchayaths::all();
return view('webapp.panchayath list', ['plists' => $plists]);
}
in view:
#foreach ($plists as $plist)
<tr>
<td> {{$plist['id']}}</td>
<td> {{$plist['pname']}}</td>
<td> {{$plist['total_ward']}}</td>
</tr>
#endforeach
OR:
#foreach ($plists as $plist)
<tr>
<td>{!! $plist->id !!}</td>
<td>{!! $plist->pname !!}</td>
<td>{!! $plist->total_ward !!}}</td>
<tr>
#endforeach

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

Blade template and radio buttons - select first in foreach loop

I have the following Blade template entry, which creates (as part of a table row) a column of radio buttons.
I want to have only the first radio genereated selected, and I want to do this via the PHP, no js post page load.
How do I check if this is the "first" entry in my collection and thus place the string checked as an attribute in the HTML? My latest code permutation is below, but it does not work.
#foreach ($organisationsOwned as $organisationOwned)
<tr class="organisations-table">
<td><input type="radio" name="organisations" id="{!! $organisationOwned->id !!}" #if ($organisationsOwned{0}) {!! "checked" !!} #endif></td>
<td>{!! $organisationOwned->org_title !!}</td>
<td>{!! $organisationOwned->plan_title !!}</td>
<td style="text-align: center;">{!! currency_format($organisationOwned->gst_amount) !!}</td>
<td style="text-align: right;">{!! $organisationOwned->subscription_ends_at !!}</td>
</tr>
#endforeach
More precisely, here's the line for the input:
<input type="radio" name="organisations" id="{!! $organisationOwned->id !!}" #if ($organisationsOwned{0}) {!! "checked" !!} #endif>
I have tried variations such as:
{{ head($organisationsOwned) ? checked : '' }}>
and
{{ $organisationsOwned{0} ? checked : '' }}>
and even this suggestion shown as a working model of my question:
#if ($organisationOwned == reset($organisationsOwned)) checked #endif
Thanks and happy new year!
If you did not specify keys for the elements of $organisationsOwned you can use the element's index to determine if it's the first one:
#foreach($organisationsOwned as $index => $organisationOwned)
...
<td><input type="radio" name="organisations" id="{!! $organisationOwned->id !!}" #if (!$index) {!! "checked" !!} #endif></td>
...
#endforeach

Server side pagination of laravel 5

I am having some trouble implementing server side pagination in Laravel 5.
Do you have any idea please tell
try with this one
//in controller
$users = \App\User::paginate(15)
return view('your desired view file name', compact('users'));
// in view
<div class="text-center text-muted" role="status" aria-live="polite">Showing {{$users->firstItem()}} to {{$users->lastItem()}} of {{$users->total()}} entries</div>
<div style="display:flex;justify-content:center;align-items:center;" >
<p></p>
{!! with(new Illuminate\Pagination\BootstrapThreePresenter($users))->render()!!}
</div>
In controller you do somrthing like following
$users = \App\User::paginate(15)
return view('template.file', compact('users'));
and in View file you put following
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
#foreach ($users as $user)
<tr>
<td> {{ $user->name }} </td>
</tr>
#endforeach
</tbody>
</table>
{!! $users->appends(\Input::except('page'))->render() !!}
Last line renders pagination links.

Laravel 5 How to Pass 2 Models to a Form

I have form and i am using form model binding. My form is
{!! Form::model($vehicle,['url' => '/pages/store']) !!}
<table style="width:650px; margin-left: 4px;" >
<tbody>
<tr>
<td>ID</td>
<td>Model</td>
<td>Brand</td>
<td>License Plate</td>
</tr>
<tr>
<td>{!! Form::text('id' ,null , ['readonly'], ['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td>
<td>{!! Form::text('model' ,null ,['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td>
<td>
{!! Form::select('brand_id', $brands, null, ['id'=>'brandBox', 'style' => 'width:150px;']) !!}
</td>
<td>{!! Form::text('licenseplate' ,null ,['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td>
</tr>
<tr>
<td colspan="2">Client</td>
</tr>
<tr>
<td colspan="2">{!! Form::select('representive_client_id', $clients, null, ['id'=>'clientSelectBox', 'class' => 'selectbox']) !!}</td>
</tr>
<tr>
<td colspan="2">Telephone Number</td>
</tr>
<tr>
<td colspan="2">{!! Form::text('tel_number' ,null ,['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td>
</tr>
<tr>
<td colspan="2">Adress</td>
<td colspan="2">{!! Form::textarea('address' ,null ,['class' =>'textboxlong form-control','style'=>'height:60px;']) !!}</td>
</tr>
</tbody>
</table>
<div id="buttoncontainer">
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $first -> id ) }}"><<</a>
#if($previous)
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $previous ) }}">PREVIOUS</a>
#endif
#if($next)
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $next ) }}">NEXT</a>
#endif
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $last -> id ) }}">>></a>
<a class="btn btn-default" id="add">EKLE</a>
{!! Form::submit('EDIT', array('class'=>'btn btn-primary')) !!}
{!! Form::submit('NEW RECORD', array('class'=>'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
I am passing the $vehicle as
$vehicle = Vehicle::where('vehicles.id',$id)->join('clients', 'vehicles.representive_client_id', '=', 'clients.id')->first();
Store Function
$client = new Client;
$client -> full_name = $client_id;
$client -> tel_number = $tel_number;
$client -> mobile_number = $mobile_number;
$client -> save();
$last_client_id = $client -> id;
$input = Request::except('client_id');
$vehicle = Vehicle::create($input);
$u_vehicle = Vehicle::orderBy('created_at', 'desc')->first();
$u_vehicle -> update(array('client_id' => $last_client_id));
I am able to see all values of these fields in my view but when it comes to store a new record to my database i am getting this error
Column not found Unknown column 'tel_number'
Guess i need to pass 2 models (Vehicle and Client) to the form but not sure how to make it. Any help would be appreciated.
In short you need to pass your view a instance of Vehicle with Client - I know you're trying to do that with the join but I'm guessing that isn't the right way to do it. I set up a clean install with Laravel and it appears to work when you set up the relationship on the model.
I'm unsure what your relationships are so let's assume a Client has one Vehicle so the Vehicle belongs to the Client. So we can put your relationship in your Vehicle.php
public function client() {
return $this->belongsTo('App\Client'); // or whatever the namespace to your class is
}
See more here on defining relationships: http://laravel.com/docs/5.1/eloquent-relationships#defining-relationships
Then when you load the model in your view you can do this:
$vehicle = \App\Vehicle::with('client')->where('id', $id)->first();
The important part being: with('client')
Then any text fields with Client attributes should be populated with the existing data.
I hope that helps, if you need more help please let me know. I've got sample code of it working in a blank laravel project so I can throw that up on github if you need to see it.

Categories