Storage disk delete image from db - laravel

I have this error when i trying to delete an image from db:
Undefined variable: removeslider
public function delete($id){
$getslider = DB::table('slider')->where('id',$id)->get();
foreach($getslider as $getslider) {
$removeslider = $getslider->bgimage;
}
Storage::disk('uploadssliders')->delete($removeslider);
return redirect('admin/inicio');
}

Try this:
public function delete($id)
{
$getslider = DB::table('slider')->where('id',$id)->first();
if($getslider){
Storage::disk('uploadssliders')->delete($getslider->bgimage);
return redirect('admin/inicio');
} else {
//id has no match in the database
echo "the id ". $id . " does not exist";
}
}
I changed your get() to first() to only get one result as this seems what you are trying to do then the foreach is not needed.

Related

Why squizlabs/php_codesniffer marked as error code with(new Vote)?

I laravel 9 project with squizlabs/php_codesniffer my phpstorm 2021 shows error :
Expected parameter of type '\TValue', 'Vote' provided
in model when I use table name in scope condition :
class QuizQualityResult extends Model
{
protected $table = 'quiz_quality_results';
public function scopeGetByVoteCategories($query, $voteCategoryId= null)
{
// “new Vote” is marked as error
$voteTable = with(new Vote)->getTable();
if (!empty($voteCategoryId)) {
if ( is_array($voteCategoryId) ) {
$query->whereIn( $voteTable . '.vote_category_id', $voteCategoryId);
} else {
$query->where( $voteTable . ' . vote_category_id', $voteCategoryId);
}
}
return $query;
}
If there is a way to fix this error ? Or maybe to use better syntax here ?
Thanks!
There is no need for helper with() here
$voteTable = (new Vote())->getTable()
Ps: there is a feeling that your method does not work the way you intended. Perhaps you meant to do the following (I could be wrong):
public function scopeGetByVoteCategories($query, $voteCategoryId = null)
{
if (empty($voteCategoryId)) {
return $query;
}
return $query->whereHas('vote', static function ($query) use ($voteCategoryId) {
if (is_array($voteCategoryId)) {
return $query->whereIn('vote_category_id', $voteCategoryId);
}
return $query->where('vote_category_id', $voteCategoryId);
});
}
public function vote()
{
// your relationship
}

how i avoid this error Trying to get property 'id' of non-object?

i have this index function to show just my hotels :
public function index()
{
$myhotels = hotel::where('created_by',Auth::user()->id)->first();
$reservations = Reservation::where('hotel_id',$myhotels->id)->get();
return view('moderateur/reservation',compact('reservations'));
}
but when there is no hotel i got this error Trying to get property 'id' of non-object
itry this but still the same
public function index()
{
$myhotels = hotel::where('created_by',Auth::user()->id)->first();
if (!hotel::where('created_by',Auth::user()->id)->exists()){
$reservations = Reservation::where('hotel_id',$myhotels->id)->get();
return view('moderateur/reservation',compact('reservations'));
}
return view('moderateur/reservation');
}
how can i fix this :( ? and when there is no hotels i want to be shown 'no hotels'
Check if the object is empty before executing the rest of your code or you firstOrFail().
eg.
public function index()
{
...
if (!empty($myhotels) {
...
return view('moderateur/reservation');
}
}
You need to check if the hotel exists:
public function index()
{
$myhotels = hotel::where('created_by', Auth::user()->id)->first();
if ($myhotels) {
$reservations = Reservation::where('hotel_id', $myhotels->id)->get();
return view('moderateur/reservation', compact('reservations'));
}
return view('moderateur/reservation');
}
While you first tried to fetch the hotel, then you were checking if the hotel doesn't exist. Since it doesn't exist, you were entering the if statement.
And then in the view:
#if ($reservations)
show reservation details
#else
show that the reservaiton is not found
#endif
https://laravel.com/docs/7.x/blade#if-statements

Multiple database laravel

How to show the value from other database in a view? i am using multi database.
My Controller :
public function index()
{
$oil = DB::connection('mysql2')->table('oils')->select('oil_price')->get();
$this->oil = [
'oil_price' => $oil
];
return view('home')->with('oil', $this->oil);
}
This is my view :
{{$oil['oil_price']}}
Output is :
enter image description here
I want show only 10000.
Don't forget to change the $mySqlConnection, $tableName, $filedName:
public function index()
{
//to get all the value of oil_price
$mySqlConnection = 'CONNECTION_NAME';
$tableName = 'TABLE_NAME';
$filedName = 'FILED_NAME';
//to get all the oil_price(only) from the databse
$controlVariableOne = DB::connection($mySqlConnection)->table($tableName)
->select($filedName)
->get();
foreach ($controlVariableOne as $controlVariableOneKey => $controlVariableOneValue)
{
$allcontrolVariableOneValues [] = $controlVariableOneValue->$filedName;
}
$controlVariableTwo = DB::connection($mySqlConnection)->table($tableName)
->select($filedName)
->first()->$filedName;
dd('All the Values From Databse ', $allcontrolVariableOneValues, 'First From Databse ',$controlVariableTwo);
$viewShare = ['controlVariableOne','controlVariableTwo'];
return view('home', compact($viewShare));
}
You should try this:
Your Controller
public function index()
{
$oil = DB::connection('mysql2')->table('oils')->select('oil_price')->get();
return view('home',compact('oil'));
}
Your view like :
#foreach($oil as $oildetails)
{{$oildetails->oil_price}}
#endforeach

Deleting a zip file using codeigniter

I want delete a zip file using codeigniter
This is my controller file:
public function delete($id)
{
$this->db->select('*');
$this->db->from('ag_angel1');
$this->db->where('angel1_id',$id);
$this->db->join('ag_file','ag_file.file_id = ag_angel1.angel1_id','left');
$ag=$this->db->get()->result();
foreach($ag as $g){
$path=realpath('uploaded/angel1/'.$g->file_name);
if (file_exists($path)) {
$this->load->helper('file');
delete_files($path) or die('failed deleting: ' . $path);
}else{
$this->load->helper('file');
unlike($path);
}
}
Code in model:
function delete($id)
{
if(!$id==null){
$this->db->where('angel1_id',$id);
if($this->db->delete('ag_angel1'))
{
return true;
}
}
}
But it doesn't work.
What is wrong?
$this->db->delete is used to delete data from a databse to delete a file use unlink
<?php unlink(server_path_to_your_file);?>
use this code in your model instead
function delete($id)
{
if(!$id==null){
$this->db->where('angel1_id',$id);
if(unlink('path_to_your_file'))
{
return true;
}
}
}

CodeIgniter just show one result from my table

SELECT * FROM image;
I am using that query to get data from image table, when i checked it using this query directly into mysql commandline it showed 3 result like this
3 rows in set (0.00 sec);
but, when i try to fetch it from codeigniter using query or active_record, it just show me 1 result. This is my code
class Produk_model extends CI_Model{
function __construct(){
parent::__construct();
}
function get_phome(){
$this->db->select('nama_produk, nama_seo, nama_kategori, seo_kategori, nominal_harga');
$this->db->join('kategori','kategori.id_kategori=produk.id_kategori');
$this->db->join('harga','harga.id_produk=produk.id_produk');
$q=$this->db->get('produk');
if($q->num_rows()){
return $q;
}
}
function get_all_pimage(){
$q=$this->db->get('image');
if($q->num_rows()>0){
return $q;
}
}
function get_by_kategori($kategori_name=''){
$this->db->select('nama_kategori, seo_kategori, nama_produk, nama_seo, nominal_harga, url_img');
$this->db->join('kategori', 'kategori.id_kategori=produk.id_kategori');
$this->db->join('harga','harga.id_produk=produk.id_produk');
$this->db->join('image','image.id_produk=produk.id_produk');
$this->db->where('seo_kategori',$kategori_name);
$q=$this->db->get('produk');
if($q->num_rows()){
return $q;
}
}
function get_by_produk($produk_name=''){
$this->db->select('produk.id_produk, nama_produk, nama_seo, short_desc, min_beli, nominal_harga');
$this->db->join('produk_detil','produk_detil.id_produk=produk.id_produk');
$this->db->join('harga','harga.id_produk=produk.id_produk');
$this->db->where('nama_seo', $produk_name);
$q=$this->db->get('produk');
if($q->num_rows()){
return $q;
}
}
function get_img_by_produk($produk_id=''){
$this->db->select('url_img');
$this->db->where('id_produk', $produk_id);
$q=$this->db->get('image');
if($q->num_rows()){
return $q;
}
}
}
i am trying to use get_all_pimage() with the function below from my controller
function home(){
$this->load->model('produk_model');
$data['konten']=$this->produk_model->get_phome()->result();
$data['img']=$this->produk_model->get_all_pimage()->row_array();
$this->load->view('frontend/home', $data);
}
but it just showed me 1 result, like this :
Array
(
[id_image] => 2
[id_produk] => PRD002
[seo_image] => pastel-1
[url_img] => pastel-1.jpg
)
i am confused, why it can be happened. because i don't know where is the mistake . Thank for your helping
Change function to..
function get_all_pimage(){
$q=$this->db->get('image');
if($q->num_rows()>0){
return $q->result_array();
}
}
Take row->Array() off your call...like so..
$data['img']=$this->produk_model->get_all_pimage();
Then foreach...
foreach($data['img'] as $val){
//display you data
}

Resources