Display date to view with a M-D-Year - laravel

I am new to laravel, with regards to displaying the date to the view with the MM-DD-YYYY format
#extends('layouts.common')
#section('title' ,'Administrator | Category')
#section('contents')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">All Categories</div>
<div class="panel-body">
<table class="table">
<thead class="thead-inverse">
<tr>
<th>Category Name</th>
<th>Published At</th>
</tr>
</thead>
<tbody>
#foreach($categories as $category)
<tr>
<td><a href="{{ url('/category', $category->id) }}"> {{
$category->name }}</a></td>
<td>{{$category->published_at }}</td>
<td><a href="{{ route('category.edit',$category->id) }}"><i
class="icon fa fa-edit"></i>&nbsp&nbsp&nbsp</a><i
class="icon fa fa-times"></i></td>
</tr>
</tbody>
#endforeach
</table>
</div>
</div>
</div>
</div>
#endsection #section('js') {!!Html::script('assets/js/jquery.min.js')!!}
{!!Html::script('assets/js/bootstrap.min.js') !!} #endsection
With regards to that i would like to display to the view the {{$category->published_at }} data into that format. Please help thanks

Please follow way below
{!! date('d-m-Y', strtotime($model->start_date)) !!}
or
{{ date('d-m-Y', strtotime($model->start_date)) }}
I Hope Help You!

In your Category model
public $dates = ['published_at'];
Then in view you can use
$category->published_at->format('m-d-Y')
Further reading: Eloquent: Mutators

Another approach. Extends Skysplit answer
In Category Model
public function getPublishedAtAttribute(){
return $this->attributes['published_at']->format('m-d-Y');
}
And never worry about published_at again. Just
$category->published_at

Related

i want to get Instagram profile total followers and following and post get this error 429 Too Many Requests on laravel

I want to get Instagram profile total followers and following and post get this error" 429 Too Many Requests on Laravel.
I need for it to show my users profile information on their account page on my website. I tried it, but I didn't get the profile json data.
Here I send my route view and controller.
1. routes/web.php
Route::get('instagram', 'InstagramController#index');
2. app/Http/Controllers/InstagramController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class InstagramController extends Controller {
/**
* Get the index name for the model.
*
* #return string
*/
public function index(Request $request) {
$items = [];
if($request->has('username')){
$client = new \GuzzleHttp\Client;
$url = sprintf('https://www.instagram.com/%s/media', $request->input('username'));
$response = $client->get($url);
$items = json_decode((string) $response->getBody(), true)['items'];
}
return view('instagram',compact('items'));
}
}
3. resources/views/instagram.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 5 Instagram API tutorial with example</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2>Laravel 5 Instagram API tutorial with example</h2><br/>
<form method="GET" role="form">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" id="username" name="username" class="form-control" placeholder="Enter Instagram Username" value="{{ old('username') }}">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<button class="btn btn-success">Search</button>
</div>
</div>
</div>
</form>
<div class="panel panel-primary">
<div class="panel-heading">Instagram Feed</div>
<div class="panel-body">
<table class="table table-bordered">
<thead>
<th>No</th>
<th width="200px;">Id</th>
<th>Code</th>
<th>Image</th>
<th>Location</th>
<th>Total Likes</th>
<th>Total Comments</th>
</thead>
<tbody>
#if(!empty($items))
#foreach($items as $key => $item)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $item['id'] }}</td>
<td>{{ $item['code'] }}</td>
<td><img src="{{ $item['images']['standard_resolution']['url'] }}" style="width:100px;"></td>
<td>{{ isset($item['location']['name']) ? $item['location']['name'] : '' }}</td>
<td>{{ $item['likes']['count'] }}</td>
<td>{{ $item['comments']['count'] }}</td>
</tr>
#endforeach
#else
<tr>
<td colspan="4">There are no data.</td>
</tr>
#endif
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>

Laravel Problem Call to undefined method App\Models\User::getRoleNames()

#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Users Management</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('users.create') }}"> Create New User</a>
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Name</th>
<th>Email</th>
<th>Roles</th>
<th width="280px">Action</th>
</tr>
#foreach ($data as $key => $user)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>
#if(!empty($user->getRoleNames()))
#foreach($user->getRoleNames() as $v)
<label class="badge badge-success">{{ $v }}</label>
#endforeach
#endif
</td>
<td>
<a class="btn btn-info" href="{{ route('users.show',$user->id) }}">Show</a>
<a class="btn btn-primary" href="{{ route('users.edit',$user->id) }}">Edit</a>
{!! Form::open(['method' => 'DELETE','route' => ['users.destroy', $user->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</table>
{!! $data->render() !!}
<p class="text-center text-primary"><small>Tutorial by ItSolutionStuff.com</small></p>
#endsection
Im agree with Gary, if you are using Spatie add the next code to User.php
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, HasRoles;
Please add this method into User.php model file
public function getRoleNames()
{
return $this->belongsToMany(Role::class);
}
If this method not work then share you table structure and user model file code.
In the controller that call this view, you should to do:
$data = Model::all(); // ---> this solved that for me
return view('model.view',compact('data'));

Error "Trying to get property 'pertanyaan' of non-object" on Dompdf Laravel 5.8

I have a view and I want to have a link that downloads a PDF.
View
<div class="col-lg-12 col-xs-12">
<div class="box">
Download PDF
<div class="box-header">
<div style="text-align:center"><h3 class="box-title">PEMELIHARAAN DAN PERAWATAN ALAT UJI </h3></div>
<div style="text-align:center">Jln. Kabupaten Sragen</div>
</div>
<p> Laporan : {{ $pemeliharaan->status }} </p>
<p> Tanggal : {{ $pemeliharaan->created_at }} </p>
<p> Jenis Alat : {{ $pemeliharaan->alat->nama_alat }} </p>
<p> User : {{ $pemeliharaan->user->name }} </p>
<div class="box">
<div class="box-header">
<h3 class="box-title"></h3>
</div>
<div class="box-body no-padding">
<table class="table table-condensed">
<tbody>
<tr>
<th style="width: 10px">#</th>
<th>Pertanyaan</th>
<th>Hasil</th>
</tr>
<tr>
<td>1.</td>
<td>{{ $pemeliharaan->pertanyaan['question1'] }}</td>
<td>{{ $pemeliharaan->pertanyaan['answer1'] }}</td>
</tr>
<tr>
<td>2.</td>
<td>{{ $pemeliharaan->pertanyaan['question2'] }}</td>
<td>{{ $pemeliharaan->pertanyaan['answer2'] }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
The link 'download' has an error.
Just Showing 404
Controller
public function showQuestion(Request $request, $id)
{
$pemeliharaan = Pemeliharaan::findOrFail($id);
$pemeliharaan->pertanyaan = json_decode($pemeliharaan->pertanyaan, true);
if ($request->has('download')) {
$pdf = PDF::loadView('users.view_question', $pemeliharaan);
return $pdf->download('view_question.pdf');
}
return view('users.view_question', compact('pemeliharaan'));
}
Routes
Route::get('/user/show/question/pdf/{id}','userController#showQuestion')->name('pdf');
Route::get('user/show/question/{id}', 'userController#showQuestion')->name('usershowQuestion');
Can someone help me with the code for the download?
You are not supplying the id route parameter to the named route 'pdf'. That's causing the line $pemeliharaan = Pemeliharaan::find($id) to yield null and later an error.
Try this:
{{ route('pdf', [ $id ] }}
In order for it to work you must supply the variable $id to the view. You can do that something like
return view('your-view')->with([
"id" => $id
]);

Laravel eloquent delete

I am looking for some assistance on how to understand Laravel Eloquent model.
I need to delete User by Id, and I have tables Main_info, Personal_info, Holidays, other_info, fm_day, oth_daysoff.
On the tables, I have inserted UserId field, so I need to delete selected UserId from all of the tables? May someone help me on how I can do this with Laravel Eloquent model?
Here is my view with delete submit button and post method:
<div class="container">
<div class="row">
<div class="col">
<div class="card">
<div class="card-head-man">
<span>Filter type: </span>
Position
Active/No
Employment type
Location
</div>
</div>
</div>
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">List of employees</h3>
</div>
<div class="table-responsive">
<table class="table card-table table-vcenter text-nowrap">
<thead>
<tr>
<th class="w-1">ID<span class="caret"></span></th>
<th>First Name<span class="caret"></span></th>
<th>Last Name</th>
<th>Position</th>
<th>Active/no</th>
<th>Employment type</th>
<th>Location</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
#foreach($allData as $data)
<tr>
<td><span class="text-muted">{{ $data->id }}</span></td>
<td>{{ $data->first_name }}</td>
<td>
{{ $data->last_name }}
</td>
<td>
{{ $data->position }}
</td>
<td>
{{ $data->employment_type }}
</td>
<td>
<span class="status-icon bg-success"></span> Active
<span class="status-icon bg-danger"></span> No
</td>
<td>{{ $data->location }}</td>
<td class="text-right">
Manage
<div class="dropdown">
<form action="/deleteFromHome" method="post">
{{csrf_field()}}
<input style="display: none;" type="text" name="deleteUserId" class="form-control" id="inputName3" value="{{ $data->id }}">
<button type="submit" class="btn btn-secondary btn-sm">Delete</button>
</form>
</div>
</td>
<td>
<a class="icon" href="javascript:void(0)">
<i class="fe fe-edit"></i>
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
Here is how my controller doing it now:
public function deleteUserFromHome(Request $req)
{
$userId = $req->input('deleteUserId');
OtherInfo::where('id', $userId)->delete();
OthDaysInfo::where('id', $userId)->delete();
HolidaysInfo::where('id', $userId)->delete();
FMdaysInfo::where('id', $userId)->delete();
PersonalInfo::where('id', $userId)->delete();
PersonalInfo::where('id', $userId)->delete();
MainInfo::where('id', $userId)->delete();
return redirect('home');
}
Maybe there is some better way to delete all data from all tables about selected userId?
I need some example how I can make Laravel Eloquent model to delete with function HasMany or some else?
You can use ->onDelete('cascade')when you create your table migrations.
For instance,
// consider a sample migration below for a table with a foreign key on your users table
Schema::create('main_info', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->timestamps();
});
And then you can easily just delete the specified user, and Laravel will take care of deleting it's child records.
$user = User::find($id);
$user->delete(); // this will also delete any record for this user on main_info table
Read more here

Nested html formBuilder with Laravel

cart.blade.php
#extends('master')
#section('content')
<div class="container">
#if(Cart::isEmpty())
<b>The card is empty</b> Shopping now
#else
<table class="table table-condensed table-bordered">
<thead>
<tr>
<th>item id</th>
....
</tr>
</thead>
<tbody>
{!! Form::open(["url"=>"/pay"]) !!}
<?php $i = 0; $totalCart_price = 0; ?>
#foreach($cart_total_items as $item)
<tr>
<td>{{ $item['id'] }}</td>
....
<td>
{!! Form::open(["url"=>"/my-cart/".$item['id'], "method"=>"DELETE", "style"=>"display: inline"]) !!}
<button type="submit" class="btn btn-danger" aria-label="Left Align">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
{!! Form::close() !!}
</td>
</tr>
<?php $i++; $totalCart_price += Cart::get($item['id'])->getPriceSum(); ?>
#endforeach
</tbody>
</table>
<b>Total price: ${{ $totalCart_price }} {{ Form::hidden("total_price", $totalCart_price) }}</b>
<br><br>
{!! Form::submit('check out (paypal)', ["class"=>"btn btn-primary"]) !!}
{!! Form::close() !!}
Clear cart
#endif
</div>
#stop
Issue: I can delete any item, but during click check out nothing happens, but when I remove clear item form, check out run successfully.
I want to run two operations, How Can I solve it?
Thanks
You can have several forms in a page but they should not be nested.
As given in html5 working draft:
4.10.3 The form element
Content model:
Flow content, but with no form element descendants.
As far as I know form is just the html tags which can be used to send a group(array) of data at once to the server.
In your case your best bet would be to use the ajax query.(though it would make our page javascript dependent)
Or, as I observed you can just seperate the two form like below:
#extends('master')
#section('content')
<div class="container">
#if(Cart::isEmpty())
<b>The card is empty</b> Shopping now
#else
<table class="table table-condensed table-bordered">
<thead>
<tr>
<th>item id</th>
....
</tr>
</thead>
<tbody>
<<!-- Remove the Form tag from here and insert it below -->>
<?php $i = 0; $totalCart_price = 0; ?>
#foreach($cart_total_items as $item)
<tr>
<td>{{ $item['id'] }}</td>
....
<td>
{!! Form::open(["url"=>"/my-cart/".$item['id'], "method"=>"DELETE", "style"=>"display: inline"]) !!}
<button type="submit" class="btn btn-danger" aria-label="Left Align">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
{!! Form::close() !!}
</td>
</tr>
<?php $i++; $totalCart_price += Cart::get($item['id'])->getPriceSum(); ?>
#endforeach
</tbody>
</table>
<<!-- Remove the Form tag from above and insert it below -->>
{!! Form::open(["url"=>"/pay"]) !!}
<b>Total price: ${{ $totalCart_price }} {{ Form::hidden("total_price", $totalCart_price) }}</b>
<br><br>
{!! Form::submit('check out (paypal)', ["class"=>"btn btn-primary"]) !!}
{!! Form::close() !!}
Clear cart
#endif
</div>
#stop

Resources