My page could not be found, can someone help me? - laravel

Here is my function in the controller
public function list()
{
$customers = Customer::all();
foreach ($customers as $customer) {
$list = [
'Customer ID' => $customer->id,
'Customer Name' => $customer->full_name,
];
$lists[] = $list;
}
$collection = $this->paginate($lists, $perPage = 5, $page = null, $options = []);
return new mainCollection($collection);
}
and here is my route
Route::get('customers/list', 'customerController#list')
When I access http://127.0.0.1:8000/api/customers/list in PostMan I have error 404 Not Found
Another Route works well.
can someone help me?

Check php artisan route:list / double check your route info.
Also, check your controller class name and namespace.

Related

get page number in object of laravel

i have object given below and i wanted to pagination in this how can i get
$productListArray = array();
$productListObject = ((object)[
"id"=>$productList->id,
"title"=>$productList->title,
"slug"=>$productList->slug,
'categoryName'=>$categoryName[0]->cat_title,
'brand'=>$brandName[0]->brandname,
'minMrp'=>$minMrp,
'maxMrp' =>$maxMrp,
'minSellingPrice' => $minSellingPrice,
'maxSellingPrice' => $maxSellingPrice,
'rating'=>$productList->rating,
'rating_count' => $productList->rating_count,
'image' => $img[0]
])->paginate();
array_push($productListArray, $productListObject);
}
return response()->json($productListArray, 200);
Hi you can get the Current page using laravel paginator Paginator::currentPageResolver
public function index()
{
$currentPage = 3; // You can set this to any page you want to paginate to
// Make sure that you call the static method currentPageResolver()
// before querying users
Paginator::currentPageResolver(function () use ($currentPage) {
return $currentPage;
});
$users = \App\User::paginate(5);
return view('user.index', compact('users'));
}
thanks for your support i found solution,
first add on your header
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
then make an other function
public function paginate($items, $perPage = 5, $page = null, $options = [])
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
after than call this function where you want in class
$data = $this->paginate($productListArray);
return response()->json($data, 200);
last 2 lines already mentioned in my questions
thanks again

How to select specfic id and update in Laravel?

I'm studing Laravel CRUD.
Laravel Framework is 6.18.15
I would like to select of a record and update.
This is photo gallery.
Now if I click one of photo I can get below URL
https://mywebsite.net/public/edit?id=59
but in edit.blade.php I got this error
Undefined variable: id
Could someone teach me correct code please?
These are current code
Controller UPDATED
public function edit(Request $request)
{
$images = ImageGallery::find($id);
return view('edit',compact('images'));
}
public function editpcs(Request $request)
{
$this->validate($request, [
'title' => 'required',
'image' => 'required|mimes:jpeg,jpg'
]);
$input['image'] = time().'.'.$request->image->getClientOriginalExtension();
if($request->hasFile('image')) {
$image = $request->file('image');
$filename = time().'.'.$request->image->getClientOriginalExtension();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(1280, null, function ($image) {$image->aspectRatio();});
$image_resize->save(public_path('images/ServiceImages/' .$filename));
}
$request->image->move(public_path('images'), $input['image']);
$input['title'] = $request->title;
// ImageGallery::update($input);
$update = DB::table('image_gallery')->where('id', $id)->update( [ 'title' => $request->title, 'image' => $request->image]);
return view('edit',compact('images'))->with('sucess','sucessfully updated');
}
web.php
//edit view
Route::get('edit', 'ImageGalleryController#edit');
Route::post('edit', 'ImageGalleryController#edit');
//edit procces
Route::get('editpcs', 'ImageGalleryController#editpcs');
Route::post('editpcs', 'ImageGalleryController#editpcs');
UPDATE
#if($images->count())
#foreach($images as $image)
<div class='text-center'>
<small class='text-muted'>{{$image['id']}}/ {{$image['title']}} </small>
</div>
#endforeach
#endif
MODEL
namespace App;
use Illuminate\Database\Eloquent\Model;
class ImageGallery extends Model
{
protected $table = 'image_gallery';
protected $fillable = ['title', 'image'];
}
Actually $id is really undefined here, it would be $request->route('id') or request('id') or $_GET['id'] or $request->input('id') :
public function edit(Request $request)
{
$id = request('id');
$images = ImageGallery::findOrFail($id); // use findOrFail() id not exist in table, it throw a 404 error
return view('edit',compact('images'));
}
Take a look at the $_GET and $_REQUEST superglobals. Something like the following would work for your example:
$id = $_GET['id'];
$country = $_GET['country'];
In laravel you can to use Input::get(), But Input::get is deprecated in newer version of laravel, prefer the $request->input instead of Input::get :
$id= $request->input('id');
$country= $request->input('country');
It looks to me like this function:
public function edit(Request $request)
{
$images = ImageGallery::find($id);
return view('edit',compact('images'));
}
Should be something like this perhaps?
public function edit(Request $request)
{
$id = $request->input('id', null);
$images = ImageGallery::find($id);
return view('edit',compact('images'));
}
As it is, $id appears to be undefined before you attempt to pass it into the find() method. But according to your URL it is in the $request object. So you need to get it from there and into the function. You can read about this method in the docs.
public function edit(Request $request)
{
$id = request('id');
$images = ImageGallery::where('id',$id)->first();
return view('edit',compact('images'));
}

Attach user id to a post (Laravel 5.3)

in my app I want to attach a logged in user id to a post, below is my controller :
public function storejournal(JournalRequest $request) {
$input = $request->all();
//Input PDF
if ($request->hasFile('file')) {
$input['file'] = $this->uploadPDF($request);
}
//Insert data jurnal
$id = $request->id;
$journal = Edition::findOrFail($id)->journal()->create($input);
$journal->user_id = Auth::id();
$journal->user()->attach($request->input('penulis'));
return redirect()->route('edition', ['id' => $id]);
}
I tried the above controller it gave error : SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert intojournal(title,abstract,file,id_edition,journalslug,updated_at,created_at) values (Rancang bangun website Jurnal Online jurusan Teknik Informatika Universitas Palangkaraya, ddd, test3.pdf, 1, rancang-bangun-website-jurnal-online-jurusan-teknik-informatika-universitas-palangkaraya, 2016-11-15 03:43:34, 2016-11-15 03:43:34))
I don't understand what I did wrong, if someone can help that would be great. Thanks.
You're trying to create a Journal without specifying the user_id when it's created.
I'd suggest the following:
public function storejournal(JournalRequest $request) {
$input = $request->all();
//Input PDF
if ($request->hasFile('file')) {
$input['file'] = $this->uploadPDF($request);
}
//Insert data jurnal
$id = $request->id;
$journal = Edition::findOrFail($id)->journal()->create($input + ['user_id' => Auth::id()]);
$journal->user()->attach($request->input('penulis'));
return redirect()->route('edition', ['id' => $id]);
}
Also, don't forget to have user_id set as mass assignable in the Journal class.
The error says you should pass user_id too. You can do this with adding user ID to an $input:
$input = $request->all();
$input['user_id'] = auth()->user()->id;

how to delete empty rows in laravel excel?

So i work with Laravel, and i use Laravel excel to load excel/csv files, but my files contains empty rows and i want to delete every empty row.
this is my code :
Excel::selectSheetsByIndex(0)->load($path, function($reader){
$results = $reader->noHeading()->toArray();
foreach ($results as $row) {
//my code
}
}, 'UTF-8');
So please if someone has any idea how i can do that i will be very appreciative
I think you can do it in this way
/** #var LaravelExcelReader $data */
$data = \Excel::load('file.xls', function ($reader) {
$reader->limitRows(20);
$reader->ignoreEmpty();
})->get()->toArray();
# remove empty rows
$data = array_filter($data);
use ToCollection method the wrap everything within if($row->filter()->isNotEmpty())
public function collection(Collection $rows)
{
foreach($rows as $row) {
if($row->filter()->isNotEmpty()){
// you logic can go here
$user = User::create([
'name' => ucwords($row['name']),
'class' => $row['class'],
...
]);
}
}
}

Debug Codeigniter Shoppincart Functions:

Can I have some help with this, please. This is a Codeigniter script MVC. There is a Controller "Function add to cart" with a Model: "Function get_products" in Models. I cannot see what is wrong here and why the function get_products doesn't execute. Could someone help me please.
This is the Model: get_product which connects to database:
function get_product()
{
$product_id = $this->input->post(‘product_id’);
$query = $this->db->select(‘product_id, product_name, description, price, photopath’);
$query = $this->db->from(‘product’);
$query = $this->db->where(‘product_id’, $product_id);
$query = $this->db->get(’‘);
return $query->result_array();
}
This is the Controller called Function add_cart, which add products to the "shopping cart view":
public function add_cart()
{
$thisProduct = $this->Cart_model->add_product();
if($thisProduct->num_rows() > 0)
{
$data = array(‘id’ => $thisProduct[‘product_id’],
‘qty’ => 1,
‘price’ => $thisProduct[‘price’],
‘name’ => $thisProduct[‘product_name’],
‘description’ => $thisProduct[‘description’]
);
$this->cart->insert($data);
}
$this->load->view(“site_header”);
$this->load->view(“site_nav”);
$this->load->view(“shoppingcart”, $data);
$this->load->view(“site_footer”);
}
you make many mistakes.
read here: http://codeigniter.com/forums/viewthread/128969/

Resources