hide records with controller - php

How can I hide records from my controller? i need to hide the records if the column 'retiro' is not null.
Controller:
public function retiro (Request $request){
$data = [
'category_name' => 'datatable',
'page_name' => 'miscellaneous',
'has_scrollspy' => 0,
'scrollspy_offset' => '',
];
$records = Registro::where('retiro', '<>', 'on')->get();
return view('retiro',compact('records'))->with($data);
}
if 'retiro' == 'on' should hide the record
the problem is that when printing the records it does not show anything.
help pls

You can pass all the variables as the second parameter on the view() using compact instead of doing it separately using the function with.
public function retiro (Request $request){
$data = [
'category_name' => 'datatable',
'page_name' => 'miscellaneous',
'has_scrollspy' => 0,
'scrollspy_offset' => '',
];
$records = Registro::whereNotNull('retiro')
->where('retiro','=','on')
->get();
return view('retiro',compact('records','data'));
}

Related

enter a view inserts an empty record

i have a problem, every time I enter or refresh a page it inserts a new record
Controller:
public function cobrar(Request $request,$id){
$data = [
'category_name' => 'datatable',
'page_name' => 'custom',
'has_scrollspy' => 0,
'scrollspy_offset' => '',
];
$cliente = \App\Models\Eventos::first();
$cobros = \App\Models\Cobros::where('turno_id', $request->id)->first();
$evento = \App\Models\Eventos::where('id' , $id)->with('servicio')->first();
$servicio = \App\Models\Servicios::where('id', $evento->servicio_id)->first();
$event = \App\Models\Eventos::find($id);
Cobros::insert([
'turno_id' => $request->input("turno_id"),
'importe' => $request->input("importe"),
'servicio_id' => $request->input("servicio_id"),
]);
return view('cobrar',compact('cobros', 'evento', 'servicio', 'event'))->with($data);
}
Image Database:
I suggest adding a check to see if method is get or post...
public function cobrar(Request $request,$id){
$data = [
'category_name' => 'datatable',
'page_name' => 'custom',
'has_scrollspy' => 0,
'scrollspy_offset' => '',
];
$cliente = \App\Models\Eventos::first();
$cobros = \App\Models\Cobros::where('turno_id', $request->id)->first();
$evento = \App\Models\Eventos::where('id' , $id)->with('servicio')->first();
$servicio = \App\Models\Servicios::where('id', $evento->servicio_id)->first();
$event = \App\Models\Eventos::find($id);
if ($request->isMethod('post')) {
Cobros::insert([
'turno_id' => $request->input("turno_id"),
'importe' => $request->input("importe"),
'servicio_id' => $request->input("servicio_id"),
]);
}
return view('cobrar',compact('cobros', 'evento', 'servicio', 'event'))->with($data);
}

Laravel API Resource not returning correct value with condition

I am new to Laravel API, I want to return a book where recommended === 1
In my resources, I have this
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'about' => $this->about,
'content' => $this->content,
// 'image' => asset('/storage/'.$this->image),
'image' => $this->image_url,
// 'recommended' => $this->recommended,
'recommended' => $this->when($this->recommended === 1, $this->recommended),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'author' => $this->author,
];
I want to return books when recommended === 1
My table is Like this
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->text('about');
$table->string('image');
$table->string('image_url');
$table->string('epub_url');
$table->integer('author_id');
$table->string('publisher');
$table->year('year');
$table->boolean('recommended')->default(0);
$table->timestamps();
});
I was able to achieve the same thing on web using this
public function index()
{
$data = array();
$data['recommends'] = Book::where('recommended', 1)->take(10)->get();
$data['latests'] = Book::orderBy('created_at', 'desc')->take(10)->get();
return view('welcome', compact("data"));
}
But I don't know how to replicate the same using Laravel API.
UPDATE
I was able to achieve the same thing on web using this
public function index()
{
$data = array();
$data['recommends'] = Book::where('recommended', 1)->take(10)->get();
$data['latests'] = Book::orderBy('created_at', 'desc')->take(10)->get();
return view('welcome', compact("data"));
}
But I don't know how to replicate the same using Laravel API.
Normally I will get all Books or Post like this using API Resource
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'about' => $this->about,
'content' => $this->content,
'image' => $this->image_url,
'recommended' => $this->recommended,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'author' => $this->author,
];
and call it like this in my controller
public function indexapi()
{
return BookResource::collection(Book::with('author')->Paginate(16));
}
But there some cases recommended is == 1 and some recommended == 0, in this case, I want to return data only when recommended == 1
I know my question is quite confusing
Thanks.
Thanks.
If I get it right, you want to filter & get only the books with ( recommended attribute == 1 ). If thats the case you shouldn't do it in your Collection file. You should do this filtering process in your Controller before passing any data to Collection.
Here is some code example from one of my project.
In ProductController.php FILE
public function index()
{
return new ProductCollection( Product::where('recommended','1')->get() );
}
As you can see , I'm filtering the products to get only the recommended ones. Then I'm sending this filtered data to the ProductCollection. This way The collection will only return the data I want.
In ProductCollection.php FILE
public function toArray($request)
{
return [
'data' => $this->collection->map( function($data) {
return [
'id' => (integer) $data->id,
'name' => $data->name,
'category_id' => $data->category_id,
'brand_id' => $data->brand_id,
'photos' => json_decode($data->photos),
'gtin' => $data->gtin
];
})
];
}
I don't have to make any changes in Collection. Because in this way , Collection should do the job for every data it gets.

can't access variable in view from controller laravel

I can't access $prospectus in the function show() but works well in the function store() in laravel version 5.6.27
public function store(Request $request) {
$course = Course::create([
'name' => $request['name'],
'title' => $request['title'],
'division_id' => $request['division_id'],
]);
$prospectus = Prospectus::create([
'years' => $request['years'],
'name' => $course->name,
'user_id' => null,
'course_id' => $course->id,
]);
return view('courses.show', compact('course', 'prospectus'));
}
public function show(Course $course) {
$prospectus = Prospectus::where('course_id', $course->id)->get();
//return $prospectus;
return view('courses.show', compact('course', 'prospectus'));
}
the data is passed when i use return $prospectus; but not in return view('courses.show', compact('course', 'prospectus'));
here are my routes
Route::resource('courses', 'CourseController');
Route::post('courses', 'CourseController#store')->name('courses.store');
Route::get('courses/{course}', 'CourseController#show')->name('courses.show');
I supose you want a single Prospectus object, get() will give you a collection of objects.
Use the first() function to get only the first match from the database as a single object.
$prospectus = Prospectus::where('course_id', $course->id)->first();
Confirm that $prospectus query don't return NULL
Try this:
$prospectus = Prospectus::where('course_id', $course->id)->first();

How to display activedataprovider data in gridview yii2

My array
$data = [
'subcategoryName' => 'asd',
'fromdate' => $fromdate,
'todate' => $todate,
'amount' => $finalamount,
];
Array data provider
$provider = new ArrayDataProvider([
'allModels' => $data,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'attributes' => ['subcategoryName'],
],
]);
// get the rows in the currently requested page
$rows = $provider->getModels();
return $rows;
Now I want to display this data on my grid view but I am getting this error Call to a member function getCount() on array
Please tell me that how can I display my array on yii2 grid view
You should'nt return here result of function getModels(). I assume u pass this to GridView, but u have to pass ArrayDataProvider.
Change this:
$rows = $provider->getModels();
return $rows;
To this:
return $provider;

Cakephp hasOne relationship with finder

In the initialize function of OrdersTable.php, I have
$this->hasOne('CurrentReview', [
'className' => 'Reviews',
'finder' => 'latest',
'foreignKey' => 'order_id
]);
and in ReviewsTable.php, I have
public function findLatest(query $q)
{
return $q->orderDesc('id')->limit(1);
}
I am trying to get only the latest review associated with the order, but I am only ever able to get the first one.
$order = $this->Orders->get($id, ['contain' => [
'CurrentReview'
]]);
What am I missing?
I am almost getting what I need with
$order = $this->Orders->get($id, ['contain' => [
'Reviews' => function ($q) {
return $q->find('latest');
}
]]);
but it's inside an array that I don't want

Categories