i have a problem passing variable during the rendering of only a section
All works good but array of data passed to the section('sidebar') view create an error ($data doesn't exist)
My blade files are
Default.blade.php
..other html code before..
<body>
#include('includes.header')
<div class="container-fluid">
<div class="row">
#yield('sidebar')
<!-- main content -->
#include('includes.main')
</div>
<footer class="row">
#include('includes.footer')
</footer>
</div>
</body>
..other code after..
home.blade.php
#extends('layouts.default')
#section('sidebar')
#include('includes.sidebar')
#stop
sidebar.blade.php
..other html code before..
<h2>The current UNIX timestamp is {{ time() }}.</h2>
<ul>
#isset($data)
#foreach ($data as $item)
<li class="nav-item"> {{$item->polizza}}</li>
#endforeach
#endisset
</ul>
..other html code after..
Controller Method search
public function search(Request $request){
if ($request->ajax()) {
$data = Customers::select('id','contr_nom','email','targa','cliente')
->where('polizza',request('polizza'))
->get();
return view('pages.home',$data)->renderSections()['sidebar'];
}
//return json_encode($data);
}
I know that array $data is good because i try return just JSON and i know that just sidebar refresh because timestamp change.
But $data is not passed to sidebar section refreshed!!
Why?
Thks a lot
You have the right idea, you just need to send the variable in a form that will be recognized. I'll break it out to an extreme, to help understand the parts, but you can easily recombine for shorter code.
$view = view('pages.home', compact('data')); // Compact with the text name ('data') sends the variable through
$sections = $view->renderSections(); // returns an associative array of 'content', 'pageHeading' etc
return $sections['sidebar']; // this will only return whats in the sidebar section of the view
Using compact() should get your where you need, and is the key.
Related
Take a look at the following examples:
showPost.blade.php:
<div>
<livewire:content-box :content="$post"/>
<button wire:click="nextPost" >Next Post >></button>
</div>
and
content-box.blade.php :
<div>
<h1>{{ $content->title }}</h1>
<p>{{ $content->content }}</p>
</div>
So far, it is completely clear what is going to happen ...: First, the information of the content to be viewed is received through showPost and passed to the contentBox, and everything is OK ..
Well now I want to get the information of the next content via the account through the button I put and calling the nextPost method:
class ShowPost extends Component
{
public Post $post;
public function render()
{
return view('livewire.show-post');
}
public function nextPost()
{
$id = $this->post->id;
$nextPost = Post::where('id', '>', $id)->first();
$this->post = $nextPost;
}
...
But nothing happens and the contentBox component has no reaction .... Has anyone had this problem ???!
I'm not sure livewire works well with nested components. could use the pagination instead. The livewire docs suggest you should not use them for little snippets or use blade components for that kind of nesting.
You can achieve what you're doing at the moment with some simple pagination.
<?php
namespace App\Http\Livewire;
use App\Models\User;
use Livewire\Component;
use Livewire\WithPagination;
class SomeContent extends Component
{
use WithPagination;
public function render()
{
// Using simplePaginate(1) instead of paginate(1).
// simplePaginate only shows "<- Previous" and "Next ->" links
// paginate shows those 2 buttons but also page numbers which you don't seem to want.
return view('livewire.some-content', [
'users' => User::simplePaginate(1),
]);
}
}
<div>
{{-- This might look wrong, but essentially it's looping through an array of length 1 because we're paginating --}}
#foreach ($users as $user)
<h1>{{ $user->name }}</h1>
<h2>{{ $user->email }}</h2>
#endforeach
{!! $users->links() !!}
</div>
EDIT
I can confirm blade components work.
Here, nextUser is the same implementation you gave.
public function nextUser()
{
$id = $this->user->id;
$nextUser = User::where('id', '>', $id)->first();
$this->user = $nextUser;
}
<div class="container">
<div class="content">
{{-- These two have the exact same template --}}
<livewire:child :user="$user" />{{-- Doesn't update when clicking Next --}}
<x-blade-child :user="$user" />{{-- Updates when clicking Next --}}
</div>
<div>
<button wire:click="nextUser">Next</button>
</div>
</div>
When clicking nextUser, the blade component updates but the livewire one doesn't.
Livewire doesn't like nested components. In your case, we can use basic blade component:
<div>
<x-content-box :content="$post"/>
<button wire:click="nextPost" >Next Post >></button>
</div>
And then:
Move content-box.blade.php to resources/views/components/
Remove component_name.php file in app/Http/Livewire
Most of the time, we can change 2 nested livewire components to livewire(parent) + basic blade component(child),
I have stored logos in the database and want to display them in the footer which is included #include('layouts.footer'). I am fetching images from the database using this code
public function show(){
$logo = DB::table('logos')->get();
return view('layouts.footer',['logo'=>$logo]);
}
I want to display these images in the footer using
<div class="footer">
#foreach($logo as $l)
{{ $l->company_name}}
{{ $l->company_logo}}
#endforeach
<p class="footer-text">© Copyright <?php echo date("Y"); ?> Hotel Store Partners</p>
</div>
but returns an error "undefined variable logo".
What is the correct way to display the images in the footer?
Try Using View Composer
In you AppserviceProvider boot method you can gather these name and pass to all Views at once.
Reference
Two ways you can achieve this:
1. Put this code in footer.
#php
$logo = DB::table('logos')->get();
#endphp
<div class="footer">
#if(count($logo))
#foreach($logo as $l)
{{ $l->company_name}}
{{ $l->company_logo}}
#endforeach
#endif
<p class="footer-text">© Copyright <?php echo date("Y"); ?> Hotel Store Partners</p>
</div>
But this method is a bad practice
Create FooterComposer
public function compose(View $view)
{
$logo = DB::table('logos')->get();
$view->with(['logo'=>$logo]);
}
Now $logo will be accessible in footer.
I have used View Composer.
In AppServiceProvider boot method, I added this code:
public function boot()
{
View::composer('layouts.app', function($view){
$view->with('logo', Logo::all());
});
}
In the Controller, I used this code:
return view('layouts.footer');
In the footer, I used this code:
<footer>
#foreach($logo as $l)
<p>{{ $l->company_name }}</p>
<img src="https://partners.hotelstore.co.ke/public{{ $l->company_logo }}" style="width:100px; height:75px;">
#endforeach
<p class="footer-text">© Copyright <?php echo date("Y"); ?> Hotel Store Partners</p>
</footer>
This is what finally displayed the images on the footer.
What I actually want is, for a specific user, I'm trying to show every image under a single blog. What I'm getting is a single blog post images for every blog.
Controller
$user_id = Session::get('id');
$user = Users::find($user_id);
$blogs = Blog::where('user_id', $user_id)->paginate(10);
$blogImage = BlogImage::where('blog_id', $blogs->pluck('id'))->get();
return view('Users.userlayout', compact('user', 'blogCat', 'blogs', 'username', 'blogImage'));
View Page
#foreach($blogs as $blog)
<div class="post">
#foreach($blogImage as $img)
<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image"
class="img-responsive">
#endforeach
<p>
<?php $str = $blog->blog_desc; ?>
{{str_limit($str, 250, "...")}}
</p>
<a href="{{URL::to('/blog-details/'.$blog->id)}}" target="_blank" class="btn_1">
Read more
</a>
</div>
<hr>
#endforeach
This is because you're using where instead of whereIn.
If you try and pass an array or a collection to where it will only use the first value.
$blogImage = BlogImage::whereIn('blog_id', $blogs->pluck('id'))->get();
Since this will return all of the BlogImage's associated with the Blog's the in the paginated list I would imagine you'll need to do a check to make sure you're only displaying the images that are associated with the specific Blog. One way you can do this is by using `#continue():
#foreach($blogImage as $img)
#continue($blogImage->blog_id !== $blog->id)
<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image" class="img-responsive">
#endforeach
All of that being said I would recommend using a one-to-many relationship between Blog and BlogImage:
Blog
public function images()
{
return $this->hasMany(BlogImage::class);
}
BlogImage
public function blog()
{
return $this->belongTo(Blog::class);
}
Then in your controller you can Eager load the images and have something like:
$blogs = Blog::with('images')->where('user_id', $user_id)->paginate(10);
And your blade file would have:
#foreach($blog->images as $image)
<img src="{{asset('storage/blog_img/'.$image->blog_img)}}" alt="Image" class="img-responsive">
#endforeach
You could then apply the same one-to-many relationship logic between User and Blog as well.
I am trying to merge two arrays into a single one and return it (the merged array) into a view in order to format and display it.
Below is the method in my controller intended for that purpose;
public function showJobCategoryContent($id)
{
$jobsInfo = Job::where('category_id', '=', $id)->where('published', '=', 1)->paginate(3);
$userInfo = Employee::all();
$array = array_merge($jobsInfo->toArray(), $userInfo->toArray());
return view('front.category-content.job-category-content', [
'jobsInfosById'=> $array
]);
}
Here, the content of my view;
#forelse($jobsInfosById as $jobInfoById)
<li>
<div class="well">
<h4>{{ $jobInfoById['company_name'] }}</h4>
<h4>{{ $jobInfoById['full_name'] }}</h4>
</div>
</li>
#endforelse
I get the following error:
Undefined index: company_name
What am I doing wrong and how can I resolve it?
use
$jobInfoById->company_name
instead of
$jobInfoById['company_name']
chatcontroller.php function returns variable to view:
public function getChat()
{
$message = chat_messages::all();
return View::make('home',compact($message));
}
this is my route:
Route::get('/getChat', array('as' => 'getChat','uses' => 'ChatController#getChat'));
this is my home.blade.php:
#extends('layouts.main')
#section('content')
<div class="container">
<h2>Welcome to Home Page!</h2>
<p> <a href="{{ URL::to('/logout') }}" > Logout </a></p>
<h1>Hello <span id="username">{{ Auth::user()->username }} </span>!</h1>
<div id="chat_window">
</div>
<input type="text" name="chat" class="typer" id="text" autofocus="true" onblur="notTyping()">
</div>
<ul>
#foreach($message as $msg)
<li>
{{ $msg['sender_username']," says: ",$msg['message'],"<br/>" }}
</li>
#endforeach
</ul>
<script src="{{ asset('js/jquery-2.1.3.min.js') }}"></script>
<script src="{{ asset('js/chat.js') }}"></script>
#stop
I am trying to send result returned by select query to view from controller.
when I do this from homecontroller.php then it works fine.
if I try to pass from controller which I have defined it gives error message as:Undefined variable.
I have used the extends \BaseController do i need to do anything else to access my controller variable from view.
please suggest some tutorial if possible for same.
Verify the route to be sure it uses the new controller:
Route::get('user/profile', array('uses' => 'MyDefinedController#showProfile'));
First of all check your routes, as Matei Mihai says.
There are two different ways to pass data into your view;
$items = Item::all();
// Option 1
return View::make('item.index', compact('items'));
// Option 2
return View::make('item.index')->with('items', $items); // same code as below
// Option 3
View::share('item.index', compact('items'));
return View::make('item.index);
You can also do this:
$this->data['items'] = Item::all();
return View::make('item.index', $this->data);