John Davidson

php - Grab manufacturer logo by 'id' or 'name' and display in table - Laravel 8 - aJax

0 comments
Message:


Morning all.


I have created a vehicle database with somewhat detailed information like engine type, fuel, transmission, manufacturer and so on...


I started out by selecting the logo for each entry and soon realized that I will end up with a folder full of the same logos just named differently by timestamp.


I am therefore trying to create a process of manually uploading all the manufacturer logos into an assets folder then when I input the 'Manufacturer Name' it will use the information to pull the relevant logo from public/storage/assets.


My Vehicle Model


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Vehicle extends Model
{
use HasFactory; /** Name of columns fillable */
protected $table = 'vehicles';
protected $fillable = [
'make',
'model_name',
'version',
'powertrain',
'trans',
'fuel',
'model_year',
'image',
'created_at'

];
};

My VehiclesController


namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Models\Vehicle;
use Illuminate\Http\Controllers;
use Illuminate\Database\Migrations\CreateVehiclesTable;

class VehiclesController extends Controller
{
public function index()
{
return view('index');
}

/** Handle insert */

public function store(Request $request)
{
// print_r($_POST);
// print_r($_FILES);
// // }


$file = $request->file('image');
$filename = time(). '.' .$file->getClientOriginalExtension();
$file->storeAs('public/images', $filename);



// handle insert vehicle ajax request
$vehicle = Vehicle::create(
[
'make' => $request->make,
'model_name' => $request->model_name,
'version' => $request->version,
'powertrain' => $request->powertrain,
'trans' => $request->trans,
'fuel' => $request->fuel,
'model_year' => $request->model_year,
'image' => $filename
]
);

return response()->json($vehicle);
}

// FETCH ALL AJAX REQUEST

public function fetchAll()
{
$vehicles = Vehicle::all(); //Could be model or controller...
$output = '';
if ($vehicles->count() > 0) {
$output .= '<table class="table table-striped table-sm text-center align-middle" >
<thead>
<tr>
<th class="tbl-head">ID</th>
<th class="tbl-head">Image</th>
<th class="tbl-head">Make</th>
<th class="tbl-head">Model</th>
<th class="tbl-head">Derivative</th>
<th class="tbl-head">Powertrain</th>
<th class="tbl-head">Transmission</th>
<th class="tbl-head">Fuel Type</th>
<th class="tbl-head">Model Year</th>
</tr>
</thead>
<tbody>';
foreach ($vehicles as $vehicle) {
$output .= '<tr class="tbl exp_tbl">
<td>'.$vehicle->id.'</td>
<td><img src="./storage/images/'.$vehicle->image.'" class="img-thumbnail justify-content-sm-center rounded-circle"></td>
<td>'.$vehicle->make.'</td>
<td>'.$vehicle->model_name.'</td>
<td>'.$vehicle->version.'</td>
<td>'.$vehicle->powertrain.'</td>
<td>'.$vehicle->trans.'</td>
<td>'.$vehicle->fuel.'</td>
<td>'.$vehicle->model_year.'</td>
<td>
<a href="#" id="'.$vehicle->id.'" class="text-success mx-2 editIcon" data-bs-toggle="modal" data-bs-target="editVehicleModal"><i class="bi-pencil-square h4"></i></a>

<a href="#" id="'.$vehicle->id.'" class="text-danger mx-1 delete-icon"><i class-"bi-trash h4"></i></a>
</td>
</tr>';
}
$output .= '</tbody></table>';
echo $output;
} else {
echo '<h1 class="text-center text-secondary my-5">No vehicles in the database!</h1>';
}
}

public function time($time)
{
$time->Carbon::now();
}
}

My Migration file


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateManufacturersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('manufacturers', function (Blueprint $table) {
$table->id('id');
$table->string('manu_logo');
$table->string('manu_name');
$table->timestamps('created_at');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('manufacturers');
}
}


I am under the impression that I will need to generate a new model and use the present VehiclesController to pull the logo from the manufacturers model.


I think I'm getting a little confused and would appreciate any help, if anymore information is needed please dont hesitate to ask


Thanks In Advance



My Answer:
using the manufacturer's name as a parameter in a PHP function. Here's an example code snippet:

php
function getManufacturerLogo($manufacturerName) {
// Assuming you have a database table or an array that stores the manufacturer logos
$manufacturerLogos = [
'Apple' => 'apple_logo.png',
'Samsung' => 'samsung_logo.png',
'Sony' => 'sony_logo.png',
// Add more manufacturer logos as needed
];

// Check if the manufacturer logo exists in the array
if (array_key_exists($manufacturerName, $manufacturerLogos)) {
return $manufacturerLogos[$manufacturerName];
} else {
return 'default_logo.png'; // Return a default logo if the manufacturer logo is not found
}
}

// Usage example
$manufacturerName = 'Apple';
$manufacturerLogo = getManufacturerLogo($manufacturerName);
echo "Manufacturer Logo: $manufacturerLogo";


In this code snippet, the `getManufacturerLogo` function takes the manufacturer's name as a parameter and returns the corresponding logo file name. You can customize the function to fetch the logo from a database or an external API if needed.

Rate this post

5 of 5 based on 7083 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us