Display CPT UI Posts by ID - html

(Sorry for my english, i'm trying to not use Google Translate ^^)
So.. I created a CPT UI "projets" which contain actually 2 posts (FMXD and Siemens) as you can see here :
I want that when i click on FMXD, i have a first page only displaying the post_thumbnail of FMXD. Like This :
As you can see, my page is displaying FMXD and Siemens and i don't understand why...
Actually, i have a single-projets.php :
<?php
/*
Template Name: Projets
Template Post Type: post, page, product, projets
*/
$context = Timber::get_context();
$posts = Timber::get_posts( array(
'post_type' => 'projets',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
) );
$context['posts'] = $posts;
Timber::render( [ 'single-projets.twig'], $context );
?>
and a single-projets.twig :
<div class="container-fluid">
{% for post in posts %}
<div class="row">
<div class="imgProjets">
<img src="{{ post.thumbnail.src }}" alt="">
</div>
<div class=" offset-9 col-3 title">
<a href="{{ post.link }}">
{{ post.title }}
</a>
</div>
</div>
<div class="row">
<div class="offset-9 col-3">
{{ post.meta('clients') }}
</div>
</div>
<div class="row">
<div class="offset-9 col-3 bg-light text-dark">
See the projects
</div>
</div>
{% endfor %}
</div>
And after that, i want to click on "See the projets" and see the content of FMXD OR Siemens, and not both.
Sorry it was quit long, but i really need your help, i'm really blocked....

This is the kind of code you might want in archive-projets.php, but not in a template for a single post.
This part:
$posts = Timber::get_posts( array(
'post_type' => 'projets',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
) );
$context['posts'] = $posts;
is getting all projets posts and passing them to the view to be rendered in a loop.
You can replace that entire part with:
$context['post'] = new Timber\Post();
You don't need a loop in your Twig view at all, since there's only one post to render. So just remove {% for post in posts %} and the corresponding {% endfor %}.

Related

How to just use json and ajx for Realtime notification in laravel 9

I have a function here which is needed to generate ticket. What is working rights is when Generating a ticket, it also creates a notification, but it was not real-time showing in the notification bell.
After a search, I found that using post-json and ajax is feasible. But I just started learning programming so I have zero knowledge about these two technologies. I hope someone can provide me with some help on how to use them.
PS. I also found that web socket and pusher are also viable solutions, but since my system only has few functions that need to send notifications. So I don't take websocket and pusher into consideration.
Translated with www.DeepL.com/Translator (free version)
I would like to attach my code below and hope somebody could help me. Any suggestion is highly appreciated. Thankyou in advance
In my controller
public function GenerateTicket($code)
{
# Retrieve the records using $code
$GiftGiving = GiftGiving::where('code', $code)->firstOrFail();
$tickets = GiftGivingBeneficiary::where('gift_giving_id', $GiftGiving->id)->get();
# Users can only access their own charity's records
if ($GiftGiving->charitable_organization_id == Auth::user()->charitable_organization_id) {
# Must have at least one beneficiary before generating tickets
if ($tickets->count() < 1) {
$notification = array(
'message' => 'Gift Giving must have at least one (1) beneficiary first before generating tickets',
'alert-type' => 'error'
);
return redirect()->back()->with($notification);
}
# Retrieve the last batch no. from the gift giving.
$batch_no = $GiftGiving->batch_no;
# Increment Batch no. by +1
$GiftGiving->update([
'last_downloaded_by' => Auth::id(),
'batch_no' => $batch_no + 1,
]);
$pdf = PDF::loadView('charity.gifts.generate_ticket', compact('tickets'));
# Audit Logs Creation
$log = new AuditLog;
$log->user_id = Auth::user()->id;
$log->action_type = 'GENERATE PDF';
$log->charitable_organization_id = Auth::user()->charitable_organization_id;
$log->table_name = 'Gift Giving';
$log->record_id = $GiftGiving->code;
$log->action = 'Charity Admin generated tickets for the Gift Giving [' . $GiftGiving->name . '] with batch no. ' . $GiftGiving->batch_no . '.';
$log->performed_at = Carbon::now();
$log->save();
# Send Notification to each user in their Charitable Organizations
$users = User::where('charitable_organization_id', Auth::user()->charitable_organization_id)->where('status', 'Active')->get();
foreach ($users as $user) {
Notification::insert([
'code' => Str::uuid()->toString(),
'user_id' => $user->id,
'category' => 'Gift Giving',
'Subject' => 'Generated Tickets',
'message' => Auth::user()->role . ' ' . Auth::user()->info->first_name . ' ' . Auth::user()->info->last_name . ' has generated tickets for ['.$GiftGiving->name.'] with batch no. ' . $GiftGiving->batch_no . '.',
'icon' => 'mdi mdi-ticket',
'color' => 'info',
'created_at' => Carbon::now(),
]);
}
return $pdf->download($GiftGiving->name . ' - No. ' . $batch_no . '.pdf');
} else {
$notification = array(
'message' => 'Users can only access their own charity records.',
'alert-type' => 'error'
);
return redirect()->back()->with($notification);
}
}
The view for the header
<div class="dropdown-menu dropdown-menu-lg dropdown-menu-end p-0"
aria-labelledby="page-header-notifications-dropdown">
<div class="p-3">
<div class="row align-items-center">
<div class="col">
<h6 class="m-0"> Notifications </h6>
</div>
<div class="col-auto">
View All
</div>
</div>
</div>
#include('charity.body.notifications')
<div class="p-2 border-top">
<div class="d-grid">
<a class="btn btn-sm btn-link font-size-14 text-center" href="{{ route('notifications.all') }}">
<i class="mdi mdi-arrow-right-circle me-1"></i> View all..
</a>
</div>
</div>
</div>
</div>
THe view for notification
#php
$notifications = App\Models\Notification::latest()->limit(3)->get();
#endphp
<div data-simplebar style="max-height: 230px;">
#if(Auth::user()->notifications->count() == 0)
<p class="text-muted font-size-12 text-center">You have no new notifications.</p>
#endif
#foreach (Auth::user()->notifications as $notif)
<a href="{{ route('notifications.view' , $notif->code) }}" class="text-reset notification-item">
<div class="d-flex">
<div class="avatar-xs me-3">
<span class="avatar-title bg-{{$notif->color}} rounded-circle font-size-16">
<i class="{{$notif->icon}}"></i>
</span>
</div>
<div class="flex-1">
<h6 class="mb-1">{{ $notif->subject}} #if($notif->read_status == 'unread')<span class="badge bg-danger">NEW</span>#endif</h6>
<div class="font-size-12 text-muted">
<p class="mb-1">{{Str::limit($notif->message, 75)}}</p>
<p class="mb-0"><i class="mdi mdi-clock-outline"></i> {{Carbon\Carbon::parse($notif->created_at)->diffForHumans()}}</p>
</div>
</div>
</div>
</a>
#endforeach
</div>

How to pass a value from controller in a button to the next blade?

I want to pass the value from this controller:
public function show($id)
{
$data_show = DB::select("SELECT * FROM staffs WHERE id = ?", [$id]);
$data_profil = DB::select("SELECT id FROM staffs WHERE id = ?", [$id]);
return view('keluarga.index', compact('data_show', 'data_profil'));
}
Passing the value to this blade:
<div class="row">
<div class="form-group">
<!--add new button -->
<div class="col-lg-2">
{!! Form::open(['route' => 'addkeluarga_form', $data_profil->id]) !!}
{{ Form::submit('Add New', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
</div>
<!-- show all button -->
<div class="col-lg-1">
{{ Form::submit('Delete', ['class' => 'btn btn-danger']) }}
</div>
<div class="col-lg-1">
{{ Form::submit('Cancel', ['class' => 'btn btn-warning']) }}
</div>
</div>
</div>
This is add new button which bring the staff id to the add new blade. But it fail to read the value of id in the blade.
How to fix this?
It would be easier to write
$data_profil = DB::table("staffs")->select("id")->whereId($id)->first();
BUT it doesn't make any sense, as you already have $id in your controller. There is no point on retrieving it again from the DB.
Pass $id to the template and use it instead
return view( 'keluarga.index', compact('data_show', 'id') );
check if the data_profil variable is containing your expectation using the print_r() method. i.e
$data_profil = DB::select("SELECT id FROM staffs WHERE id = ?", [$id]);
print_r($data_profil);//you will comment this out latter
check on the top of your page the content of your variable, I you see a nested array output like Array ( [0] => stdClass Object ( [id] => 1 , ...)) , then you may want to refer your variable as an array in your template. i.e.
$data_profil[0]->id
For the first query get all column,
and for secound query I don't think that you need to get id, as you already have id.
$data_show = DB::table("staffs")->where('id',$id)->first();
$data_profil = $data_show->id;
OR
$id = $id; // As the same id it will return
return view('keluarga.index', compact('data_show', 'data_profil','id'));

Blade, Use html inside variable being passed into partial view is not being rendered

I'm using a partial view to display page header, that view accepts two variables one is Icon the other is a title.
pageHeader.blade.php:
<div class="page-header">
<div class="row">
<!-- Page header, center on small screens -->
<h1 class="col-xs-12 col-sm-4 text-center text-left-sm"><i class="fa {{$icon}} page-header-icon"></i> {{$title}}</h1>
</div>
and I'm using it like so:
#include('zdashboard._partials.pageHeader',['icon'=>'fa-pencil','title'=>'<strong>Editing</strong>'.$center->translations()->whereLang('en')->first()->name])
Sometimes I like to make one word strong or italic like the example above but, blade engine won't render the HTML tags I'm typing as part of title variable (the output like the photo down).
So is there any idea how to solve this? Am I doing it!
wrong?
The output
By default in Laravel 5 {{ $title }} construction wil be escaped.
If you don't want the data to be escaped, you may use the following syntax:
{!! $title !!}
Read more about Blade control structures: http://laravel.com/docs/5.0/templates#other-blade-control-structures
view
#if(Session::has('success'))
<div class="alert alert-success">
{!! Session::get('success')!!}
</div>
#endif
Controller
public function store(Request $request)
{
$this->validate($request, [
'product_code' => 'required|unique:products',
'product_name' => 'required',
'description' => 'required',
'price' => 'required',
'brand_id' => 'required',
'category_id' => 'required',
]);
$product = Product::create([
'product_code' => $request->input('product_code'),
'product_name' => $request->input('product_name'),
'description' => $request->input('description'),
'price' => $request->input('price'),
'brand_id' => $request->input('brand_id'),
'category_id' => $request->input('category_id'),
]);
return redirect()->route('products.index')->with('success',
"The product <strong>".$product->product_name."</strong> has successfully been created.");
}

storing image in a existing table column and display it using laravel 4

I would like to store image in a existing table.
How can i do that in laravel 4 .I also want to display that image in my website.How i tried it always shows Call to undefined method Symfony\Component\HttpFoundation\File\UploadedFile::save() .
//my show blade
#extends ('layouts.default')
#section('content')
#foreach($posts as $post)
<div >
<div class="media col-md-8">
<a href="#" class="pull-left paddingTop ">
{{ HTML::image($post->image($destinationPath) }}
</a>
<div>
<h3><strong>{{$post->ad_title}}</strong><strong class="pull-right">{{ $post->price}} Tk</strong></h3>
</div>
<div>
<h5>{{ $post->description}}</h5>
</div>
</div>
</div>
#endforeach
#stop
this is my table storing method
public function store()
{
$post=new Post;
$post->user_id=Auth::id();
$post->category=Input::get('category');
$post->subcategory=Input::get('subcategory');
$post->add_type=Input::get('add_type');
$post->brand=Input::get('brand');
$post->screen_type=Input::get('screen_type');
$post->condition=Input::get('condition');
$post->ad_title=Input::get('ad_title');
$post->fuel=Input::get('fuel');
$post->transmission=Input::get('transmission');
$post->registration_year=Input::get('registration_year');
$post->engine_capacity=Input::get('engine_capacity');
$post->mileage=Input::get('mileage');
$post->description=Input::get('description');
$post->price=Input::get('price');
$post->mobile=Input::get('mobile');
$post->ad_title=Input::get('ad_title');
$post->image = Input::file('image');
//$post->image=Input::get('image');
$destinationPath = public_path() . '/images/';
$post->image->save($destinationPath);
$post->area=Input::get('area');
$post->save();
return 'your ad has been published :)';
}
my image input form
<div>{{ Form::file('image', ['class' => 'form-group']) }}</div>
Edit store function like below
public function store()
{
$post=new Post;
$post->user_id=Auth::id();
$post->category=Input::get('category');
$post->subcategory=Input::get('subcategory');
$post->add_type=Input::get('add_type');
$post->brand=Input::get('brand');
$post->screen_type=Input::get('screen_type');
$post->condition=Input::get('condition');
$post->ad_title=Input::get('ad_title');
$post->fuel=Input::get('fuel');
$post->transmission=Input::get('transmission');
$post->registration_year=Input::get('registration_year');
$post->engine_capacity=Input::get('engine_capacity');
$post->mileage=Input::get('mileage');
$post->description=Input::get('description');
$post->price=Input::get('price');
$post->mobile=Input::get('mobile');
$post->ad_title=Input::get('ad_title');
$name = Input::file('image')->getClientOriginalName();
Input::file('image')->move('public/images',$name);
$destinationPath = '/images/'.$name;
$post->image=$destinationPath;
$post->area=Input::get('area');
$post->save();
return 'your ad has been published :)';
}
and edit blade template like below
#extends ('layouts.default')
#section('content')
#foreach($posts as $post)
<div clss="row">
<div class="media col-lg-8">
<a href="#" class="pull-left paddingTop ">
{{ HTML::image($post->image) }}
</a>
<div>
<h3><strong>{{$post->ad_title}}</strong<strong
class="pull-right">{{ $post->price}} Tk</strong></h3>
</div>
<div>
<h5>{{ $post->description}}</h5>
</div>
</div>
</div>
#endforeach
<div class="">{{$posts->links()}}</div>
#stop

How to add an image beside to an input checkbox

I have a the following checkbox field (1)
and I would like to add an image to the checkbox.
I added it in twig file in the following way (2) but it is bad placed.
I would like to place it in the following way (3)
// (1)
->add('remove_avatar', 'checkbox', array(
'label' => 'user.label.current_avatar',
'required' => false,
'mapped' => false,
))
// (2)
{{ form_widget(form) }}
<span class="user">
<img src="{{ get_avatar(user) }}" class="user-avatar">
</span>
// (3)
<div class="controls">
<img src="some_like">
<input type="checkbox" id="profile_remove_avatar" name="profile[remove_avatar]" value="1">
<label class="control-label" for="profile_remove_avatar">Remove the current avatar</label>
</div>
What is the best way to do it?
Instead of rendering the total form with only form_widget(form) you can do more if you render each field for itself. Like this:
<div class="controls">
<img src="some_like">
{{ form_widget(form.remove_avatar) }}
{{ form_label(form.remove_avatar) }}
</div>
If you got more fields in your form render them the same way OR place them all at the end with form_rest(form).
This is how you customize a symfony2 form from inside a twig template. So i think this is the best and easiest way.