Laravel query return wrong data - laravel

QUERY:
$recent_posts = Blog::join("categories",'categories.id', '=', 'blogs.category_id')
->where('categories.status', 1)
->orderBy('blogs.id', 'desc')
->take(3)
->get();
Both tables had a created_at column.
At frontend im using this to retrieve the data:
<div class="row justify-content-center">
#foreach ($recent_posts as $recent_post)
<div class="col-md-6 col-lg-4 latest-blog-resp">
<div class="blog-item">
<div class="blog-img">
<a href="{{ url('blog/'.$recent_post->slug) }}">
#if ($recent_post->blog_image == '')
<img src="{{ asset('fibonacci/adminpanel/assets/img/dummy/no_image.jpg') }}" class="img-fluid round-item" alt="blog image">
#else
<img src="{{ asset('fibonacci/adminpanel/assets/img/blog/thumbnail1/'.$recent_post->blog_image) }}" class="img-fluid round-item" alt="blog image">
#endif
</a>
</div>
<div class="blog-inner">
<div class="blog-meta">
<span class="mr-2">
<i class="mdi mdi-calendar-account-outline"></i>{{ __('frontend.by_admin') }}
</span>
<span>
<i class="mdi mdi-calendar-range"></i>{{Carbon\Carbon::parse($recent_post->created_at)->isoFormat('MMMM')}} {{Carbon\Carbon::parse($recent_post->created_at)->isoFormat('DD')}}
</span>
</div>
<h5 class="blog-title">
{{ $recent_post->title }}
</h5>
<p class="blog-desc">{{ $recent_post->short_description }}</p>
<a href="{{ url('blog/'.$recent_post->slug) }}" class="blog-more-link">
{{ __('frontend.read_more') }} <i class="fa fa-angle-right ml-2"></i>
</a>
</div>
</div>
</div>
#endforeach
#if (count($recent_posts) === 3)
<div class="col-12 text-center margin-top-30">
<div class="btn-group">
<a href="{{ url('blog') }}" class="default-button">
{{ __('frontend.view_all') }}
</a>
</div>
</div>
#endif
</div>
THE PROBLEM:
$recent_post->created_at returns the category table creation (created_at) date but we expect to receive the blog table result as created_as (like a post creation data).
Thanks in advance!

Specify your fields in a select clause.
$recent_posts = Blog::select(
'blogs.slug',
'blogs.blog_image',
'blogs.title',
'blogs.short_description',
'blogs.created_at'
)
->join('categories', 'categories.id', 'blogs.category_id')
->where('categories.status', 1)
->orderByDesc('blogs.id')
->take(3)
->get();

Related

initialData is null in livewire component when there is a two events in the same component

I am having two events in my component one is changing the layout and another one is sorting. Changing a layout basically it only replace a layout component inside a parent component. Now when i am calling a sorting then livewire giving me a error 'initialdata is null '.
I am looking for to create a sorting order with this same component. And my code is
<div class="dropdown px-2 d-none d-lg-block">
<div class=" text-dark fw-bold dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<span class="px-1">
<img class="" src="{{ Vite::image('icons/classic.png') }}" alt="" />
</span>
<span>
<img class="" src="{{ Vite::image('icons/filter-down.png') }}" alt="" />
</span>
</div>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item text-primary #if($show == 1)text-primary #else text-dark #endif fw-bolder" href="javascript:void(0)" wire:click="$set('show', '1')">
<img src="{{ Vite::image('icons/compact.png') }}" alt="" class="ps-2" /> Card </a>
<a class="dropdown-item uni-border #if($show == 2)text-primary #else text-dark #endif fw-bolder " href="javascript:void(0)" wire:click="$set('show', '2')">
<img src="{{ Vite::image('icons/classic.png') }}" alt`your text`="" class="ps-2" />Classic </a>
<a class="dropdown-item uni-border #if($show == 3) text-primary #else text-dark #endif fw-bolder" href="javascript:void(0)" wire:click="$set('show', '3')">
<img src="{{ Vite::image('icons/com.png') }}" alt="" class="ps-2" />Compact </a>
</div>
</div>
{{-- sorting option --}}
<div class="d-flex justify-content-between pt-2 align-items-center d-none d-lg-block ">
<div class="d-flex align-items-center justify-content-between">
<div>
<span class="m-0">Selected Filters:</span>
<span class=" mx-3 boat-span2 align-items-center rounded-1">
<span>$500 </span>
<span>
<img src="{{ Vite::image('icons/cross.png') }}" alt="" class="" />
</span>
</span>
<span class=" boat-span2 align-items-center rounded-1">
<span> Skipper </span>
<span>
<img src="{{ Vite::image('icons/cross.png') }}" alt="" class="" />
</span>
</span>
</div>
<div class="">
<h6 class="m-0 text-secondary fw-500 ">
<u> Clear Filters</u>
</h6>
</div>
</div>
</div>
#if($show == 2)
{{-- classic layout --}}
#livewire('frontend.boat.listing.layout.classic',['product' => $product])
#elseif($show == 1)
{{-- card layout --}}
#livewire('frontend.boat.listing.layout.card',['product' => $product])
#elseif($show == 3)
{{-- compact layout --}}
#livewire('frontend.boat.listing.layout.compact',['product' => $product])
#endif
</div>
<?php
namespace App\Http\Livewire\Frontend\Boat\Listing;
use App\Models\Product;
use Livewire\Component;
class Listing extends Component
{
// Mount , boot , hydrate component
public $filter_option_recommend = "recommended";
public $show = "2";
// public $filter_option = "name";
// public $product;
public function detail($id, $title)
{
return redirect()->route('website.boat.detail', [$id, $title]);
}
public function render()
{
$product = Product::join('product_time_assign', 'product_time_assign.product_id', '=', 'products.id')
->where([
'products.type_id' => 1, // boat
'products.status' => 'Active',
])
->select(
'products.id as productid',
'product_time_assign.id as timeassignid',
'products.name',
'products.capacity',
'products.price_starts',
'product_time_assign.skipper',
'product_time_assign.product_time_assign_id',
'products.review_rating',
'products.image',
'products.description',
'products.discount_price'
)
->orderBy($this->filter_option_recommend, 'asc')->get();
return view('livewire.frontend.boat.listing.listing', ['product' => $product]);
}
}

Laravel issue in room booking

Hi all I have some issue with my code ..
The idea is that I have multi room's which I retrieve from database every room have multi booking date my idea is to show when open room page all page if the room have current reservation mark it red or future reservation marked blue I can't know how to make this happen my room.blade is:
#section('content')
<div class="row text-right ">
#foreach ($room_typ as $a )
<div class="col-12 col-lg-2">
<div class="card radius-15">
<div class="card-body text-center radius-15">
<h4 title="{{ $a->r_det }}" class="mb-0 font-weight-bold mt-0 text-white">{{ $a->r_name }}
</h4>-<h6>{{ $a->f_name }}</h6>
<span class="mt-1 mb-1 acupp"></span>
<i class="bx bx-plus"></i>
<i class="fadeIn animated bx bx-trash-alt mr-md-3"></i>
<i class="bx bx-info-square mr-md-3"></i>
{{ $rr }} // should show the count for reservation with room number
</div>
</div>
</div>
#endforeach
</div>
#endsection
the controller is :
public function allrom()
{
$room_typ = addroom::all();
foreach ($room_typ as $dd) {
$rom = $dd->r_name;
$rr = res_c::where('rno', '=', $rom)->get();
}
echo view('room', compact('room_typ', 'rr'));
}
Try this please change the checkin and checkout fields with the one in your database let me know if it work
#section('content')
<div class="row text-right ">
#foreach ($room_typ as $a )
<div class="col-12 col-lg-2">
<div class="card radius-15" style="{{ date("Y-m-d") >= date("Y-m-d",strtotime($a->r_date_checkin)) && date("Y-m-d") < date("Y-m-d",strtotime($a->r_date_checkout)) ? "background-color:red" : ( date("Y-m-d") > date("Y-m-d",strtotime($a->r_date_checkin) ) ) ? "background-color:blue" : "" }}">
<div class="card-body text-center radius-15">
<h4 title="{{ $a->r_det }}" class="mb-0 font-weight-bold mt-0 text-white">{{ $a->r_name }}
</h4>-<h6>{{ $a->f_name }}</h6>
<span class="mt-1 mb-1 acupp"></span>
<i class="bx bx-plus"></i>
<i class="fadeIn animated bx bx-trash-alt mr-md-3"></i>
<i class="bx bx-info-square mr-md-3"></i>
{{ $rr }} // should show the count for reservation with room number
</div>
</div>
</div>
#endforeach
</div>

Only one image is displayed in the image carousel in bootstrap and laravel

I am trying to display images in bootstrap carousel but am not able to loop. Only one image is shown but not sliding.Am using laravel in the back end. Please assist if you can this is the code.
This is the code:
<div class="container">
<div class="row">
<div id="imageCarousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
#foreach($images as $value)
<li data-target="#imageCarouselIndicator" data-slide-to="{{ $loop->index }}" class="{{ $loop->first ? 'active' : '' }}"></li>
#endforeach
</ol>
<div class="carousel-inner">
<h3>{{$product->title}}</h3>
#foreach($images as $image )
<div class="item image {{ $loop->first ? 'active' : '' }}">
<div class="carousel-item active">
<img src="/images2/{{$image->filename}}" alt="{{$image->title}}">
</div>
</div>
#endforeach
</div>
<a class="carousel-control-prev" href="#imageCarousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#imageCarousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</div>
Try this:
<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
#foreach($images as $image )
<div class="carousel-item {{ $loop->first ? 'active' : '' }}">
<img src="/images2/{{$image->filename}}"/>
</div>
#endforeach
</div>
</div>

Laravel Error: Too few arguments to function, 0 passed and exactly 1 expected

Using Laravel-5.8, I have this code:
public function manager_employee_list(Request $request)
{
$employees = HrEmployee::paginate(6);
return view('appraisal.appraisal_goals.manager_employee_list')->with('employees', $employees);
}
And it render this view: manager_employee_list
<div class="row d-flex align-items-stretch">
#if (count($employees))
#foreach($employees as $key => $employee)
<div class="col-12 col-sm-6 col-md-4 d-flex align-items-stretch">
<div class="card bg-light">
<div class="card-header text-muted border-bottom-0">
{{isset($employee->designation) ? $employee->designation->designation_name : ''}}
</div>
<div class="card-body pt-0">
<div class="row">
<div class="col-7">
<h2 class="lead"><b>Staff ID: {{$employee->employee_code}}</b></h2>
<h2 class="lead"><b>{{$employee->first_name}} {{$employee->last_name}}</b></h2>
<h6 class="lead"><b>Employee Department: </b>{{isset($employee->department) ? $employee->department->dept_name : ''}}</h6>
</div>
<div class="col-5 text-center">
#if($employee->emp_image != '')
<img src="{{ URL::to('/') }}/public/storage/employees/image/{{ $employee->emp_image }}" class="img-circle img-fluid" />
#else
<img class="profile-user-img img-fluid img-circle" src="{{asset('theme/adminlte3/dist/img/default.png')}}" alt="" class="img-circle img-fluid">
#endif
</div>
</div>
</div>
<div class="card-footer">
<div class="text-right">
<a href="{{ route('appraisal.appraisal_goals.manager_employee_goal', ['id'=>$employee->id]) }}" class="btn btn-sm btn-primary">
<i class="fas fa-user"></i> View Goal
</a>
</div>
</div>
</div>
</div>
#endforeach
#else
<h4 style="text-align:center;">No matching records found</h4>
#endif
</div>
I want to pass the parameter
['id'=>$employee->id]
from above to another controller action:
public function manager_employee_goal($id)
{
$goals = AppraisalGoal::where('employee_id', $id)->get();
return view('appraisal.appraisal_goals.manager_employee_goal')->with('goals', $goals);
}
It utilizes it here:
$goals = AppraisalGoal::where('employee_id', $id)->get();
When I clicked on
<a href="{{ route('appraisal.appraisal_goals.manager_employee_goal', ['id'=>$employee->id]) }}" class="btn btn-sm btn-primary">
<i class="fas fa-user"></i> View Goal
</a>
I got this error:
Too few arguments to function App\Http\Controllers\Appraisal\AppraisalGoalsController::manager_employee_goal(), 0 passed and exactly 1 expected
and this is underlined:
public function manager_employee_goal($id)
route/web.php
Route::get('appraisal_goals/manager_employee_list', 'Appraisal\AppraisalGoalsController#manager_employee_list')->name('appraisal.appraisal_goals.manager_employee_list');
Route::get('appraisal_goals/manager_employee_goal', 'Appraisal\AppraisalGoalsController#manager_employee_goal')->name('appraisal.appraisal_goals.manager_employee_goal');
How do I resolve it?
Thank you.
You need to change in your web.php file. add {id} in URL.
otherwise, all are fine as now your get method will be like 'appraisal_goals/manager_employee_goal/{id}'
Route::get('appraisal_goals/manager_employee_goal/{id}','Appraisal\AppraisalGoalsController#manager_employee_goal')->name('appraisal.appraisal_goals.manager_employee_goal');
I believe your route need to be updated to
Route::get('appraisal_goals/manager_employee_goal/{id}','Appraisal\AppraisalGoalsController#manager_employee_goal')->name('appraisal.appraisal_goals.manager_employee_goal');

Using 2 Controller Functions in a View in Laravel

I want to have two loops in my view, so I wrote these two functions
public function index()
{
$books = Book::orderBy('created_at', 'desc')->take(10)->get();
return view('bookpage')->with('books', $books);
}
public function loggedin()
{
$books = Book::orderBy('RAND()')->take(1)->get();
return view('bookpage')->with('books', $books);
}
In the view I have
<!--First Loop -->
#foreach($books as $book)
<div class="col-md-6">
<div class="out-box">
<h2>{{ $book->name }}</h2>
<h3>{{ $book->author->name }}</h3>
<br>
Start Reading<br><br>
<img src="assets/img/cart-buy.png" width="13px"/> Buy
</div>
</div>
</div>
</div>
<div class="col-md-6">
<input id="aboutbook" type="radio" name="tabs" checked>
<label for="aboutbook" class="aboutbook">About This Book</label>
<input id="bookreview" type="radio" name="tabs">
<label for="bookreview" class="bookreview">Reviews</label>
<hr style="background-color:black;">
<section style="padding-top:5px;" id="bookabout" >
<div class="row">
<div class="col-md-12">
<p>{{ $book -> about }}</p>
<h1>About the Author</h1>
</div>
</div>
<div class="row">
<div class="col-sm-4 col-md-3 col-6">
<img src="assets/img/Ellipse.png" class="rounded-circle" width="120px">
</div>
<div class="col-sm-4 col-md-4 col-6">
<h1>{{ $book->author->name }}</h1>
<h4>{{ $book->author->about }}</h4>
</div>
<div class="col-sm-4 col-md-5">
<div id="learnbtn">
Learn More
</div>
</div>
</div>
</section>
<section style="padding-top:5px;" id="bookabout1" >
jjjjjjj
</section>
</div>
</div>
</div>
#endforeach
</section>
<!--Second Loop -->
#foreach($books as $book)
#if($book->recommended === 1)
<div class="col-1-5">
<div class="home-catalog-image">
<a href="{{ $book->image_url }}" target="_blank">
<!-- <img src="{{ $book->image }}" alt="trending image" /> -->
<img src="{{ $book->image_url }}" class="img-responsive" alt="{{ $book->image_url }}">
</a>
<!-- <img src="{{ asset('/books/'.$book->image) }}" alt="trending image" /> -->
</div>
<p class="author">{{ $book->author->name }}</p>
<h1 class="book-title">{{str_limit($book -> name, 20) }}</h1>
</div>
#endif
#endforeach
In my web.php
Route::get('/', 'WelcomeController#index')->name('welcome');
I want to call another function in the view, although I know the method is wrong, I don't know how to go about it.
You don't have to create two different method for logged in user just use
public function index()
{
if(auth()->user()) {
$books = Book::orderBy('RAND()')->take(1)->get();
} else $books = Book::orderBy('created_at', 'desc')->take(10)->get();
return view('bookpage')->with('books', $books);
}
in view file use
#auth
//code for logged in user
#else
//code for guest user
#endauth
I was able to solve my problem like this
public function loggedin()
{
$data = array();
$data['recommends'] = Book::where('recommended', 1)->take(10)->get();
$data['latests'] = Book::orderBy('created_at', 'desc',)->where('recommended', 0)->take(10)->get();
$data['logged'] = Book::all()->random(1);
return view('index-logged', compact("data"));
}
In my view, I did
#foreach($data['logged'] as $log)
<h1>{{ $log->author->name }}</h1>
<h4>{{ $log->author->about }}</h4>
#endforeach
#foreach($data['recommends'] as $recommend)
<p class="author">{{ $recommend->author->name }}</p>
<h1 class="book-title">{{str_limit($recommend -> name, 20) }}</h1>
#endforeach
#foreach($data['latests'] as $latest)
<p class="author">{{ $latest->author->name }}</p>
<h1 class="book-title">{{str_limit($latest -> name, 20) }}</h1>
#endforeach

Resources