Stuck at the passing data from database to view - laravel

I am still beginner at laravel, and i have problem about undefined variable when i want to pass data from the controller
here is my route
Route::get('/jelajahi-search', 'TourismController#srch');
Route::get('/jelajahi','TourismController#index');
here is my search function in the controller
public function srch(Request $request)
{
$this->validate($request, ['cari'=>'required']);
$search = $request->get('cari');
$posts = Tourism::where('namatempat', 'like', "%$search%")
->orWhere('kabupaten', 'like', "%$search%")
->paginate(1)
dd($posts);
return view('vendor.jelajahi')->with('tourismSearch',$posts);
}
and here is my view
#foreach ($tourismSearch as $tour)
<tr>
<th>
<td>
<img src="{{ URL::asset('/jelajahi-image/'.$tour->path_gambar) }}" style="width: 25%; height: 25%;"><br>
</td>
<td>
Nama tempat : {{$tour->namatempat}}<br>
Kabupaten : {{$tour->kabupaten}}<br>
Kecamatan : {{$tour->kecamatan}}<br><br>
</td>
</th>
</tr>
#endforeach
thank you

It seems your search doesn't return any results.
Try to use this check:
#if ($tourismSearch)
#foreach ($tourismSearch as $tour)
<tr>
<th>
<td>
<img src="{{ URL::asset('/jelajahi-image/'.$tour->path_gambar) }}" style="width: 25%; height: 25%;"><br>
</td>
<td>
Nama tempat : {{$tour->namatempat}}<br>
Kabupaten : {{$tour->kabupaten}}<br>
Kecamatan : {{$tour->kecamatan}}<br><br>
</td>
</th>
</tr>
#endforeach
#endif
Also, you can check for $posts contents by using dd($posts); before return view clause.

Related

Highlight row if has duplicate value

Hello I have these columns 'name', 'email', 'username' and 'ip' how would I check or highlight table row in blade if records has same ip?
This is my controller
$promo = Promo::with(['participants' => function ($q) {
$q->orderBy('winner', 'desc')->orderBy('created_at', 'desc');
}])->find($id);
And this is my blade
#if($promos->participants->count() > 0)
#foreach($promos->participants as $participant)
<table>
<tr class="align-middle">
<td>Name</td>
<td>Email</td>
<td>Username</td>
<td>IP</td>
</tr>
<tr>
<td>{{$participant->name}}</td>
<td>{{$participant->email}}</td>
<td>{{$participant->username}}</td>
<td>{{$participant->ip}}</td>
</tr>
</table>
#endforeach
#endforeach
Another way is to get duplicates by taking advantage of Larvel's collection and have them passed in view.
$ips = $promos->participants->pluck('ip');
$duplicateIps = $ips->duplicates()->unique()->all();
So in your blade you would just need to check
#if($promos->participants->isNotEmpty())
#foreach($promos->participants as $participant)
<table>
<tr class="align-middle">
<td>Name</td>
<td>Email</td>
<td>Username</td>
<td>IP</td>
</tr>
<tr #class(['duplicate-row' => in_array($participant->ip, $duplicateIps)]>
<td>{{$participant->name}}</td>
<td>{{$participant->email}}</td>
<td>{{$participant->username}}</td>
<td>{{$participant->ip}}</td>
</tr>
</table>
#endforeach
Do like this
$ipList will store IPs when foreach runs and keeps checking whether they exist. If yes, add class duplicate-row to the <tr>
$ipList = [];
#foreach($promos->participants as $participant)
<tr class="align-middle #if(in_array($participant->ip, $ipList)) duplicate-row #endif">
<td>{{$participant->name}}</td>
<td>{{$participant->email}}</td>
<td>{{$participant->username}}</td>
<td>{{$participant->ip}}</td>
</tr>
#if(!in_array($participant->ip, $ipList))
<?php array_push($ipList, $participant->ip); ?>
#endif
#endforeach
In CSS you can add
.duplicate-row {
background-color: red;
}

Object of class stdClass could not be converted to string (View: C:\wamp64\www\Application 1\resources\views\admin\soutenance\themeValide.blade.php)

for the past few days I have been blocking part of my code. Indeed I want to retrieve information from my database in order to display them in a table.
web.php
Route::get('admin/soutenance/themeValide', 'SoutenanceController#index')->name('admin.soutenance.themeValide');
soutenanceController.php
public function index()
{
$themes = DB::table('themes')
->join('soutenances', 'themes.id', '<>', 'soutenances.theme_id')
->join('profs', 'profs.id', '=', 'themes.prof_id')
->join('users', 'users.id', '=', 'themes.user_id')
->select('themes.id', 'themes.title', 'profs.name AS prof_name', 'users.name AS user_name')
->where('themes.validated', '=', true)
->get();
// dd($themes);
return view('admin.soutenance.themeValide', compact('themes'));
}
themeValide.blade.php
<div class="box-body">
<div class="table-responsive">
<table class="table no-margin">
<thead>
<tr>
<th> ID </th>
<th> titre </th>
<th> Encadreur </th>
<th> Etudiant </th>
<th> Action </th>
</tr>
</thead>
<tbody>
#forelse($themes as $theme)
<tr>
<td>{{$theme->id}} </td>
<td>{{$theme->title}}</td>
<td>{{$theme->prof_name}}</td>
<td>{{$theme->user_name}}</td>
<td><span class="tools"><i class="fa fa-edit"></i>
{{' | '}}
<i class="fa fa-trash-o"></i></span></td>
</tr>
#empty
<tr>
<td></td>
<td> pas de Theme validé </td>
</tr>
#endforelse
</tbody>
</table>
</div>
</div>
when i do in soutenanceController
dd($themes);
I get the desired result but when I want it displayed in my page themeValide.blade.php,
it shows me this error
Object of class stdClass could not be converted to string (View: C:\wamp64\www\Application 1\resources\views\admin\soutenance\themeValide.blade.php)
I would like you to enlighten me, please.
You are passing $theme to the route helper which will try to use it as a string and it is an object. You would need to pass the property you want, most likely id:
route('admin.soutenance.create', $theme->id);

How to fetch images from the database

I am working in Laravel Framework, i have tried to fetch the image from the database. But it's not getting pulled properly from the DB, can anyone help me on this?
Here is my code-
public function getAllPost()
{
$posts = DB::table('posts')
->join('categories','posts.category_id','categories.id')
->select('posts.*','categories.name')
->get();
return view('posts.all_posts', compact('posts'));
}
public function getIndividualPosts($id)
{
$posts = DB::table('posts')->
join('categories','posts.category_id','categories.id')
->select('posts.*','categories.name')
->where('posts.id', $id)->first();
return view('posts.view_post', compact('posts'));
}
view.blade.php
#foreach($posts as $post)
<tr>
<td>{{ $post-> id}}</td>
<!--The name came from the categories table-->
<td>{{ $post-> name}}</td>
<td>{{ $post-> title}}</td>
<td>{{ $post-> details}}</td>
<td><img src="{{ URL::to($post->image)}}" style="height: 70px; width: 100px"></td>
<td>
Edit
Delete
View
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>

How to fix " (1/1) ErrorException compact(): Undefined variable: categories in AdminPostsController.php line 23 '"

I want to create a post but I cannot because it shows me the Undefined variable error for the compact function,
can someone help me?
Below are the samples from files:
AdminPostsController.php
public function index(){
$posts = Post::paginate(5);
return view('admin.posts.index', compact('posts','categories'));
}
admin/posts/index.blade.php
#extends('layouts.admin')
#section('content')
<h1>Posts page</h1>
<table class="table">
<thead>
<th>ID</th>
<th>Owner</th>
<th>Category</th>
<th>Photo ID</th>
<th>Title</th>
<th>Body</th>
<th>Created</th>
<th>Updated</th>
</thead>
<tbody>
#if($posts)
#foreach($posts as $post)
<tr>
<td>{{$post->id}}</td>
<td>{{$post->user->name}}</td>
<td>{{$post->category_id ? $post->category->name : 'No category'}}</td>
<td><img height="50" src="{{$post->photo ? $post->photo->file : 'http://placehold.it/400x400'}}"></td>
<td>{{$post->title}}</td>
<td>{{str_limit($post->body,10)}}</td>
<td>{{$post->created_at->diffForHumans()}}</td>
<td>{{$post->updated_at->diffForHumans()}}</td>
<td>View post </td>
<td>View comments</td>
</tr>
#endforeach
#endif
</tbody>
</table>
<div class="row">
<div class="col-sm-6 col-sm-offset-5">
{{$posts->render()}}
{{--pagination--}}
</div>
</div>
#stop
Make sure your $categories variable is defined in the index function inside your controller.
// AdminPostsController.php
public function index(){
$posts = Post::paginate(5);
$categories = Category::all();
return view('admin.posts.index', compact('posts','categories'));
}

Call to a member function links() on array in Laravel

I am receiving this error while trying to create pagination in laravel.
ErrorException in 1e886a45102a3e4898f23b52cd7ca771 line 396: Call to a
member function links() on array (View:
C:\xampp\htdocs\soulfy_repo\framework\resources\views\soulfy\setting.blade.php)
line 396: <?php echo e($themes->links()); ?>
HomeController.php
public function getBackgroundTheme()
{
$query = DB::table('theme_background');
if (request()->has('menu')) {
$theme = DB::table('kategori_name')->where('kategori_theme', request('menu'))->first();
$query = $query->where('kategori_id', $theme->kategori_id);
}
//$model = $query->get();
$theme = $query->paginate(4);
return view('soulfy.setting', [
'themes'=>$theme,
'user' => auth()->user(),
]);
// return redirect('/home/setting' .
}
setting.blade.php
<table>
#if(isset($themes))
#foreach($themes as $m)
<tr>
<td><img width="100px" height="100px" src="{{url('/')}}/uploads/theme/{{$m->pic_name}}.jpg"/></td>
<td><img width="100px" height="100px" src="{{url('/')}}/uploads/theme/{{$m->pic_name}}.jpg"/></td>
<td><img width="100px" height="100px" src="{{url('/')}}/uploads/theme/{{$m->pic_name}}.jpg"/></td>
</tr>
<tr>
<td><div class="box"><input type="checkbox" name="pic" value=""></div></td>
<td><input type="checkbox" name="pic" value=""></td>
<td><input type="checkbox" name="pic" value=""></td>
</tr>
#endforeach
{{ $themes->links() }}
#endif
</table>
$theme = DB::table('kategori_name')->where('kategori_theme', request('menu'))->paginate(PAGELIMIT);
Use This In Your Home Controller. Instead Of first();
And Define This In Your Routes : define('PAGELIMIT','4');

Resources