Passing data from controller to laravel nested view - php

I have a page system in Laravel - where I pass data from controller to view.
$this->data['title'] = $row->title;
$this->data['breadcrumb'] = $row->bc;
Now I passed it as follows:
return View::make('Themes.Page', $this->data);
In the view file, I access the data as follows:
{{$breadcrumb}}
What I am trying to do now is to pass this data in nested views:
$this->layout->nest('content',$page, $this->data);
(Content is the {{content}} in the view which will be replaced with $page contents. I want to pass the $this->data just as before but now I get an error:
Variable breadcrumb not defined.
Note: Laravel Version 4.2 $this->layout is set in constructor to a
template file (Themes.Page)

Actually you don't need to pass any separate data to your partial page(breadcrumb)
controller page
$this->data['title'] = $row->title;
$this->data['breadcrumb'] = $row->bc;
return View::make('idea.show',array("data"=>$this->data));
main view page
<div>
<h1>here you can print data passed from controller {{$data['title']}}</h1>
#include('partials.breadcrumb')
</div>
your partial file
<div>
<h1>here also you can print data passed from controller {{$data['title']}}</h1>
<ul>
<li>....<li>
<li>....<li>
</ul>
</div>
for more information on this you can check following links http://laravel-recipes.com/recipes/90/including-a-blade-template-within-another-template or watch this video https://laracasts.com/series/laravel-5-fundamentals/episodes/13

You should pass data as follows
return View::make('Themes.Page')->with(array(
'data'=>$this->data));
or (since you're passing only 1 variable)
return View::make('Themes.Page')->with('data', $this->data);
and further you can pass it on to nested views as by referencing $data

$dataForNestedView = ['breadcrumb' => $row->bc];
return View::make('Themes.Page', $this->data)->nest('content', 'page.content', $dataForNestedView);
In the Themes.Page view render nested view:
<div>
{{ $content }} <!-- There will be nested view -->
</div>
And in the nested page.content view you can call:
<div>
{{ $breadcrumb }}
</div>
*div tag is only for better understanding.

Okay, after intense search, I figured out that there was a bug in the Laravel version 4.2 I was using.
Laravel 5 works.
For laravel 4.2, a better option would be to pass array of data objects using View::share('data',$objectarray) while passing the data from controller.
Thanks everyone for help

Related

Load data in view in Laravel

I have a simple controller function that fetch all records from db. but when i am trying to show all these records it show nothing. In fact it shows me hard coded foreach loop like this.
#foreach ($compactData as $value) {{ $value->Name }} #endforeach
this is my contoller function.
public function showallProducts()
{
$productstock = Product::all()->stocks;
$productoldprice = Product::all()->OldPrices;
$productcurrentprice = Product::all()->CurrentPrice;
$compactData=array('productstock', 'productoldprice', 'productcurrentprice');
return view('welcome', compact($compactData));
}
this is my view
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
Laravel
</div>
<div class="title m-b-md">
All products
</div>
<table>
<tbody>
#foreach ($compactData as $value)
{{ $value->Name }}
#endforeach
</tbody>
</table>
</div>
</div>
</body>
why it is behaving like this. any solution?? I am using phpstorm version 17. Is their any setting issue to run project because what ever project I ran it gives me the only page which i ran with only html?
My route is.
Route::get('/', function () {
$action = 'showallProducts';
return App::make('ProductController')->$action();
});
Have you checked your $compactData variable? Please dd($compactData) to see what it contains.
Problem 1
You are accessing a relational property as a property of Eloquent collection, like this:
Product::all()->stocks
which is not correct. Because the Collection object doesn't have the property stocks but yes the Product object might have a stocks property. Please read the Laravel documentation about Collection.
Problem 2
$compactData = array('productstock', 'productoldprice', 'productcurrentprice');
This line creating an array of 4 string, plain string not variable. So, your $compactData is containing an array of 4 string. If you want to have a variable with associative array then you need to do the following:
$compactData = compact('productstock', 'productoldprice', 'productcurrentprice');
Problem 3
return view('welcome', compact($compactData));
Here you are trying to pass the $compactDate to the welcome view but unfortunately compact() function doesn't accept variable but the string name of that variable as I have written in Problem 2. So, it should be:
return view('welcome', compact('compactData'));
Problem 4
Finally, in the blade you are accessing each element of the $compactData data variable and print them as string which might be an object.
You most likely have a problem with your web server.
Try to use Laravel Valet as development environnement.
Edit : I found this : Valet for Windows
I think you didn't mention the blade in the name of the view file by which it is saved. So change the name of the file by which it is save to something like:
filename.blade.php
and try again.
Explanation:
#foreach ($compactData as $value) this is the syntax of blade template engine, and to parse and excute it, you have to mention the blade extension in the name.

Laravel get div without ajax

Hi I am trying to get the content within a div element that also happens to be within a form into my controller. I dont want to use ajax. How may I get that done ?
<div id="editorcontents" name="editorcontents">
</div>
Then in controller
Use Input;
$content = Input::get('editorcontents');
In your controller, do something like this. Look up the correct way in the docs (https://laravel.com/docs/5.4/eloquent#retrieving-models). For example, if you want ALL input, you would do Input::all();, instead of Input::where('editorcontents')->get();
public function index() {
$content = Input::where('editorcontents')->get();
return view('your_view.blade.file', compact('content'));
}
Then in your view your would now have $content, that you passed from your controller.
start of by looking at it, add this at top of your view: {{ dd($content) }}. This will die dump $content.
Go ahead and remove that line and do something like (docs here https://laravel.com/docs/5.4/blade#loops):
<div id="editorcontents" name="editorcontents">
#forelse ($content as $value)
<li>{{ $value->body }}</li>
#empty
<p>No content</p>
#endforelse
</div>

How to pass data or data array while rendering view in codeigniter?

My URL is:
http://localhost/CodeIgniterCms/admin/dashboard/otherpages/123
where,
Codeigniter Cms
- admin
-- dashboard (the controller)
--- otherpages (Method)
Controller code
public function otherpages($somedata) {
$this->render('admin/second_view',$somedata);
}
Code in second_view.php
<pre>
<div class="container">
<?php echo $somedata;?>
</div>
</pre>
But it is throwing error
Unable to load the requested file: admin/123.php
Try this within your otherpages function:
$data['somedata'] = $somedata; <br/>
$this->load->view('admin/second_view',$data);

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

Blade helper, get variable from view without parameter

maybe someone know, how can I get variable with name "foo" from blade template get to static function which is used in this template.
For example I have:
<div class="collumns large-8">
{{ Helpers::setLetters('variable_name') }}
</div>
<div class="collumns large-8">
{{ $variable_name }}
</div>
Both divs should set same string because in setLetters function will be
return ${$name};
But, that should be instead $name, because in case about, will be error.
I use laravel 4.1
This doesn't go in your View, it should be in your controller.
Controller
$name = Helpers::setLetters('variable_name');
return \View::make('yourview', array( 'variable_name' => $name);

Categories