How to retrieve specific column data from model bind show method? - laravel

I'm setting up a new project and want to retrieve data from passed model binding controller show method but output in show view is null
model :
class Content extends Model
{
protected $fillable = ['title','body'];
}
this is my controller :
public function show(Content $content)
{
return view('content.show',compact('content'));
}
this is my index :
#foreach ($content as $item)
<td>
<a class="btn btn-primary" href="{{ route('Content.show',$item) }}">View</a>
</td>
#endforeach
and this is show method that result is null:
<div class="card-header">
{{ $content->title }}
</div>
<div class="card-body">
{{ $content->body }}
</div>
<div class="card-footer">
{{ $content->created_at }}
/div>
i expect have result in show method because everything is okay but there isn't where is my problem ?
Updated :
dd($content)

because you try to get $content while it is $item now :)
<div class="card-header">
{{ $item->title }}
</div>
<div class="card-body">
{{ $item->body }}
</div>
<div class="card-footer">
{{ $item->created_at }}
</div>

I found the answer
I changed the route resource from resource('Content','ContentController') to
resource('content','ContentController') and the problem has been fixed !
i dont know why !

//used to show a list, like what you are trying to do
public function index()
{
$contents = Content::get();
return view('content.index',compact('contents'));
}
//used to show one item
public function show(Content $content)
{
return view('content.show',compact('content'));
}
What you have for you view works if you are looping over multiple content, otherwise you want:
{{ $content->title }}
</div>
<div class="card-body">
{{ $content->body }}
</div>
<div class="card-footer">
{{ $content->created_at }}
</div>```

Related

How do I use 2 variable in foreach in laravel blade

One variable is used for searching and
Another variable is used to display name based on id from DB
public function recorddisplay(Request $request)
{
$search =$request['search'] ??"";
if($search != "")
{
$covidrecord=Covidrecord::where('fullname','LIKE',"%$search%")
->orWhere('province','LIKE',"%$search%")
->orWhere('district','LIKE',"%$search%")
->orWhere('localgovernment','LIKE',"%$search%")->paginate(15);
}
else{
$covidrecord= Covidrecord::paginate(15);
}
$data = compact('covidrecord','search');
$dataCovid=DB::table('coviddeathrecord')
->join('district','coviddeathrecord.district','=','district.id')
->join('province','coviddeathrecord.province','=','province.id')
->join('localgovernment','coviddeathrecord.localgovernment','=','localgovernment.id')
->get(['coviddeathrecord.*','district.district','province.province','localgovernment.localgovernment']);
return view('admin.dashboard.display', compact('dataCovid'))->with($data);
}
This is the blade
#foreach(array_merge($covidrecord,$dataCovid) as $data)
<tr>
<td>
<div class="d-flex px-2 py-1">
<div class="d-flex flex-column justify-content-center">
{{-- <label>{{ $data->fullname }}</label> --}}
<h6 class="mb-0 text-sm">{{$data->fullname}}</h6>
<p class="text-xs text-secondary mb-0">{{$data->gender }} ,{{$data->age }} वर्ष</p>
</div>
</div>
</td>
<td>
<p class="text-xs font-weight-bold mb-0">{{ $address->province }}</p>
<p class="text-xs text-secondary mb-0">{{ $address->district }}, {{$address->localgovernment }}<br/> {{ $data->tole }}</p>
</td>
#endforeach
As I am using array_merge, it is throwing the error
//array_merge(): Expected parameter 1 to be an array, object given
You can pass variables in a compact function:
return view('admin.dashboard.display', compact('data', 'dataCovid'));
Also please see compact function
return like this
return view('website.condo_living.index')->with([
'var1'=>$var1,
'var2'=>$var2,
]);
to use 2 variables in foreach in your blade. It´s better that you pass you data from controller
return view('yourSubFolderView.View')->with('yourVariable')->with('yourVariable')
Also you can use
return view('yourSubFolderView.View')->compact('yourVariable', 'yourVariable')
UPDATE
in this line:
return view('admin.dashboard.display', compact('dataCovid'))->with($data);
change to:
return view('admin.dashboard.display', compact('dataCovid'))->with('data',$data);
and in your blade, you can access to $data or $dataCovid with for-each
in your code was missing your variable name 'data', $data
FYI, the a covidrecord variable returns collections of data and a dataCovid variable that returns an array of data that you have to standardize the types of variables to deal with in the view array only or collection only
I convert the data type to collection of array
and passing collection correctly from your method:
public function recorddisplay(Request $request)
{
$search = $request->get('search') ?? "";
if($search != "") {
$covidrecord = DB::table('covidrecord')->where('fullname','LIKE',"%$search%")
->orWhere('province','LIKE',"%$search%")
->orWhere('district','LIKE',"%$search%")
->orWhere('localgovernment','LIKE',"%$search%")
->paginate(15)
->items();
} else {
$covidrecord= DB::table('covidrecord')->paginate(15)->items();
}
$dataCovid = DB::table('coviddeathrecord')
->join('district','coviddeathrecord.district','=','district.id')
->join('province','coviddeathrecord.province','=','province.id')
->join('localgovernment','coviddeathrecord.localgovernment','=','localgovernment.id')
->get(['coviddeathrecord.*','district.district','province.province','localgovernment.localgovernment']);
$items = collect($users)->concat($data);
return view('admin.dashboard.display', compact('items'));
}
and within the blade you can looping normal with collection of array like:
#foreach($items as $data)
<tr>
<td>
<div class="d-flex px-2 py-1">
<div class="d-flex flex-column justify-content-center">
{{-- <label>{{ $data['fullname'] }}</label> --}}
<h6 class="mb-0 text-sm">{{$data['fullname']}}</h6>
<p class="text-xs text-secondary mb-0">{{$data['gender'] }} ,{{$data['age'] }} वर्ष</p>
</div>
</div>
</td>
<td>
<p class="text-xs font-weight-bold mb-0">{{ $address->province }}</p>
<p class="text-xs text-secondary mb-0">{{ $address->district }}, {{$address->localgovernment }}<br/> {{ $data['tole'] }}</p>
</td>
#endforeach

LaravelShoppingcart package, rowId property not working

Hi I'm using the LaravelShoppingcart package(https://github.com/bumbummen99/LaravelShoppingcart) and When I output {{ $item->rowId }} on the blade template I get the rowId on the browser when the blade is rendered but when I pass it in the qtyIncreased({{ $item->rowId}}) method and try to dump and die it in the livewire component it doesn't work. Mark you when I pass the $item->id and dump and die it, it works. The only error I'm getting is on the console and is:
Error handling response: TypeError: Cannot destructure property 'yt' of 'undefined' as it is undefined.
at chrome-extension://cmedhionkhpnakcndndgjdbohmhepckk/scripts/contentscript.js:535:6
Any assistance is highly appreciated.
Live wire blade component
<div class="cart-overlay {{ $active ? 'transparent' : '' }}">
<div class="cart {{ $active ? 'showCart' : '' }}">
<span class="close-cart" wire:click="$emit('closeCart')">
<i class="fas fa-window-close"></i>
</span>
<h2>Cart</h2>
<div class="cart-content">
#foreach ($cartContent as $item)
<div class="cart-item">
<img src="{{ $item->options->image }}" alt="product" />
<div>
<h4>{{ $item->name }}</h4>
<h5>${{ $item->price }}.99</h5>
<span class="remove-item">remove</span>
</div>
<div>
<i class="fas fa-chevron-up" wire:click="qtyIncreased({{ $item-
>rowId }})"></i>
<p class="item-amount">{{ $item->qty }}</p>
<i class="fas fa-chevron-down"></i>
</div>
</div>
#endforeach
</div>
<div class="cart-footer">
<h3>your total : $ <span class="cart-total">{{ $cartTotal }}</span></h3>
<button class="clear-cart banner-btn">clear cart</button>
</div>
</div>
</div>
Livewire Class component
<?php
namespace App\Http\Livewire;
use Gloudemans\Shoppingcart\Facades\Cart;
use Livewire\Component;
class CartOverlay extends Component
{
public $active = false;
public function render()
{
$cartContent = Cart::content();
$cartTotal = Cart::total();
return view('livewire.cart-overlay', compact(['cartContent', 'cartTotal']));
}
public $listeners = [
'showCart',
'closeCart'
];
public function qtyIncreased($rowId)
{
dd($rowId);
}
public function showCart()
{
$this->active = true;
}
public function closeCart()
{
$this->active = false;
}
}
I managed to fixed it, the parameter in the function should be passed in as a string. So, I added quotes around the parameter. Changed it from <i class="fas fa-chevron-up" wire:click="qtyIncreased({{ $item->rowId }})"></i>
TO
<i class="fas fa-chevron-up" wire:click="qtyIncreased('{{ $item->rowId }}')"></i>

Laravel Notifications Mark One Notification as Read

I'm new to laravel notification
I want when to click on the notification the link takes me to the invoice and the notification should be marked as read
I don't know how to mark one notification as read.
I know that I should take the notification id to mark the specific notification as read but I don't know how to is it in a function.
blade :
<div id="unreadNotifications">
#foreach (auth()->user()->unreadNotifications as $notification)
<div class="main-notification-list Notification-scroll mark-as-read" >
<a class="d-flex p-3 border-bottom"
href="{{ url('InvoicesDetails') }}/{{ $notification->data['id'] }}" data-id="{{$notification->id}}" >
<div class="notifyimg ">
<i class="la la-file-alt text-pink text-center"></i>
</div>
<div class="ml-3">
<h5 class="notification-label mb-1">
{{ $notification->data['title'] }}
{{ $notification->data['user'] }}
</h5>
<div class="notification-subtext">{{ $notification->created_at }}
</div>
</div>
</a>
</div>
#endforeach
</div>
Controller:
public function MarkAsRead_all (Request $request)
{
$userUnreadNotification= auth()->user()->unreadNotifications;
if($userUnreadNotification) {
$userUnreadNotification->markAsRead();
return back();
}
}
public function unreadNotifications_count()
{
return auth()->user()->unreadNotifications->count();
}
public function unreadNotifications()
{
foreach (auth()->user()->unreadNotifications as $notification){
return $notification->data['title'];
}
Create a link in the blade
<a class="d-flex p-3 border-bottom" href="{{ url('ReadNotification') }}/{{ $notification->data['id'] }}" data-id="{{$notification->id}}" >
Define a route for it
Route::get('ReadNotification/{id}','BlahBlahController#ReadNotification')->name('ReadNotification');
In Controller
public function ReadNotification($id)
{
$userUnreadNotification = auth()->user()
->unreadNotifications
->where('id', $id)
->first();
if($userUnreadNotification) {
$userUnreadNotification->markAsRead();
}
return back();
}
You just need to send the id of the notification to your controller and make it as read it is the same as you did for all read

Laravel + Vue, How to do ->where() in vue-for?

I'm listing projects and those projects have statuses. First I'm listing the statuses and inside them projects, but this returns all the projects no matter what the status.
With Laravel I could just:
foreach ($projects->where($project->status_id == $status->id) as $project)
but how to do that with Vue?
<div v-for="status in statuses">
{{ status.title }}
<div v-for="project in projects">
{{ project.title }}
</div>
</div>
I also tried
<div v-for="status in statuses">
{{ status.title }}
<div v-for="project in filteredprojects">
{{ project.title }}
</div>
</div>
computed:{
filteredProjects() {
return this.projects.filter(function(project) {
return project.status_id == status.id;
})
}
},
But no luck
Try like this:
<div v-for="status in statuses">
{{ status.title }}
<div v-for="project in projects">
<div v-if="status.id === project.status_id">
{{ project.title }}
</div>
</div>
</div>
If you have set up a relationship between status and projects (as you should since you're using Laravel), you can load the relationship before it ever gets to the Vue component.
Controller
$statuses = Status::where(...your conditions...)->with('projects')->get();
// EDIT: or get all statuses without conditions
$statuses = Status::with('projects')->get();
// EDIT 2: get all statuses with conditional projects
$statuses = Status::with(['projects' => function($query) {
// add your conditions for projects
$query->where('completed', 0);
}])->get();
Vue
<div v-for="status in statuses">
{{ status.title }}
<div v-for="project in status.projects">
{{ project.title }}
</div>
</div>

Accessing collection from a relationship

So I have 2 tables.
DirtyEvent model and Event model.
I am retrieving DirtyEvent with Event which works fine
In blade I have:
#if (\Request::is('profile/event'))
#foreach ($events as $event)
#if (empty( $event->image ))
<div class="card-header">
<span class="card-title"> {{($event->title) }}</span>
</div>
#else
<div class="card-image">
<img class="img-responsive" src="{{ $event->image }}"></img>
<span class="card-title"> {{($event->title) }}</span>
</div>
#endif
<div class="card-content">
<p><strong>Starts: </strong>#php echo ($startdate) #endphp - {{$event->startime }}</p>
<br>
#if ($startdate != $enddate)
<p><strong>Ends: </strong>#php echo ($enddate) #endphp - {{$event->endtime }}</p>
<br>
#endif
<p><strong>Description:</strong></p>
<br>
<p>{{$event->description }}</p>
</div>
<div class="card-action">
<i class="fa fa-pencil-square-o fa-2x" aria-hidden="true"></i>
<form method="POST" action={{ url('events/delete/' . $event->id) }}>
{{ method_field('PATCH') }}
{{ csrf_field() }}
<button type="submit" class="delete" style="border:none;"><i class="fa fa-trash-o fa-2x" aria-hidden="true"></i></button>
</form>
</div>
#foreach ($events->publicevents as $eventss)
{{dd($events)}}
#endforeach
#endforeach
However dd($events) gives me:
DirtyEvent Collection -> relations -> publicevents -> Event collection
But, It is saying that publicevents do not exist in current collection.
Controller
public function index()
{
$id = Auth::id();
$events = DirtyEvent::where('user_id', $id)
->with('publicevents')
->get();
return view('events.viewEvent', compact('events'));
}
Model
public function publicevents()
{
return $this->hasMany('App\Event', 'event_id');
}
I guess not all dirty event objects have events. So check this first with empty() or isEmpty() or count():
#if (!empty($event->publicevents))
#foreach ($event->publicevents as $eventss)
{{ $eventss->id }}
#endforeach
#endif
Update
It should be $event->publicevents, not $events->publicevetns.

Resources