dane5890's avatar

Passing Variables Inside A Laravel 8 Livewire Blade File

Hi all, I'm using Laravel 8 to make an ecommerce site. So far, when I use the traditional laravel controllers, I can easily pass all the variables in a blade. When I try to use the same data for a livewire blade file, it keeps giving me a variable is undefined error.

I'm new to livewire, so you have to forgive me for this mistake. Basically, I'm trying to mimic the same regular controller behavior inside a livewire controller. For example here is BrandsController:

namespace App\Http\Controllers;

use Illuminate\Http\Request; use App\Models\Brand; use App\Models\MultiImage; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\DB; use Image;

class BrandController extends Controller {

public function __construct() {

$this->middleware('auth');

}

public function AllBrands() {

  $brands = Brand::latest()->paginate(5);

  return view('admin.brands.index', compact('brands'));

}

public function AddBrand(Request $request) {

  $validateData = $request->validate([

    'brand_name' => 'required|unique:brands|min:4',
    'brand_image' => 'required|mimes:jpg.jpeg,png',
    'meta_title' => 'required|unique:brands|max:70',
    'meta_keywords' => 'required',
    'meta_description' => 'required|unique:brands|max:160',
    'slug' => 'required',
  ],


  [
    'brand_name.required' => 'Please Type The Brand Name',
    'brand_name.min' => 'The Brand Name Must Be At Least Four Characters',
    'meta_title' => 'Please Enter Meta Title',
    'meta_title.max' => 'The Meta Title Can Not Be More Than 70 Characters',
    'meta_description' => 'The Meta Description Can Not Be More Than 160 Characters'

  ]);

  $brand_image = $request->file('brand_image');

//  $name_gen = hexdec(uniqid());
//  $img_ext = strtolower($brand_image->getClientOriginalExtension());
//  $img_name = $name_gen.'.'.$img_ext;
//  $up_location = 'images/brands/';
//  $last_img = $up_location.$img_name;
//  $brand_image->move($up_location,$img_name);

$name_gen = hexdec(uniqid()).'.'.$brand_image->getClientOriginalExtension();
Image::make($brand_image)->resize(300,200)->save('images/brands/'.$name_gen);

$last_img = 'images/brands/'.$name_gen;

  Brand::insert([

    'brand_name' => $request->brand_name,
    'brand_image' => $last_img,
    'meta_title' => $request->meta_title,
    'meta_keywords' => $request->meta_keywords,
    'meta_description' => $request->meta_description,
    'slug' => $request->slug,
    'created_at' => Carbon::now()

  ]);

  return Redirect()->back()->with('success','Brand Inserted Successfully');

}

public function Edit($id) {

  $brands = Brand::find($id);
  return view('admin.brands.edit', compact('brands'));

}

public function Update(Request $request, $id) {

  $validateData = $request->validate([

    'brand_name' => 'required|min:4',
    'meta_title' => 'required|max:70',
    'meta_keywords' => 'required',
    'meta_description' => 'required|max:160',
    'slug' => 'required',
  ],


  [
    'brand_name.required' => 'Please Type The Brand Name',
    'brand_name.min' => 'The Brand Name Must Be At Least Four Characters',
    'meta_title' => 'Please Enter Meta Title',
    'meta_title.max' => 'The Meta Title Can Not Be More Than 70 Characters',
    'meta_description' => 'The Meta Description Can Not Be More Than 160 Characters'

  ]);

  $old_image = $request->old_image;
  $brand_image = $request->file('brand_image');

  if($brand_image){

    $name_gen = hexdec(uniqid());
    $img_ext = strtolower($brand_image->getClientOriginalExtension());
    $img_name = $name_gen.'.'.$img_ext;
    $up_location = 'images/brands/';
    $last_img = $up_location.$img_name;
    $brand_image->move($up_location,$img_name);

    unlink($old_image);

    Brand::find($id)->update([

      'brand_name' => $request->brand_name,
      'brand_image' => $last_img,
      'meta_title' => $request->meta_title,
      'meta_keywords' => $request->meta_keywords,
      'meta_description' => $request->meta_description,
      'slug' => $request->slug,
      'created_at' => Carbon::now()

    ]);

    return Redirect()->back()->with('success','Brand Updated Successfully');

  }

  else{

    Brand::find($id)->update([

      'brand_name' => $request->brand_name,
      'meta_title' => $request->meta_title,
      'meta_keywords' => $request->meta_keywords,
      'meta_description' => $request->meta_description,
      'slug' => $request->slug,
      'created_at' => Carbon::now()

    ]);

    return Redirect()->back()->with('success','Brand Updated Successfully');

  }


}

public function Delete($id) {

  $image = Brand::find($id);
  $old_image = $image->brand_image;
  unlink($old_image);

  Brand::find($id)->delete();
  return Redirect()->back()->with('success','Brand Deleted Successfully');

}

}

How can I get the same effect inside a livewire controller, because copying the information verbatim doesn't work. Thanks.

0 likes
2 replies
dane5890's avatar

Thanks, but still nothing. even when I pass the variable in the @livewire feature, the dynamic images don't render. Here is the code from the Brand livewire controller. It is verbatim to the BrandController.

namespace App\Http\Livewire\Shop;

use Livewire\Component; use App\Models\Brand; use App\Http\Controllers\BrandController;

class Brands extends Component {

public $brands; public $brand_image; public $last_img;

public function AllBrands() {

  $brands = Brand::latest()->paginate(5);

  return view('admin.brands.index', compact('brands'));

}

public function AddBrand(Request $request) {

  $validateData = $request->validate([

    'brand_name' => 'required|unique:brands|min:4',
    'brand_image' => 'required|mimes:jpg.jpeg,png',
    'meta_title' => 'required|unique:brands|max:70',
    'meta_keywords' => 'required',
    'meta_description' => 'required|unique:brands|max:160',
    'slug' => 'required',
  ],


  [
    'brand_name.required' => 'Please Type The Brand Name',
    'brand_name.min' => 'The Brand Name Must Be At Least Four Characters',
    'meta_title' => 'Please Enter Meta Title',
    'meta_title.max' => 'The Meta Title Can Not Be More Than 70 Characters',
    'meta_description' => 'The Meta Description Can Not Be More Than 160 Characters'

  ]);

  $brand_image = $request->file('brand_image');

//  $name_gen = hexdec(uniqid());
//  $img_ext = strtolower($brand_image->getClientOriginalExtension());
//  $img_name = $name_gen.'.'.$img_ext;
//  $up_location = 'images/brands/';
//  $last_img = $up_location.$img_name;
//  $brand_image->move($up_location,$img_name);

$name_gen = hexdec(uniqid()).'.'.$brand_image->getClientOriginalExtension();
Image::make($brand_image)->resize(300,200)->save('images/brands/'.$name_gen);

$last_img = 'images/brands/'.$name_gen;

  Brand::insert([

    'brand_name' => $request->brand_name,
    'brand_image' => $last_img,
    'meta_title' => $request->meta_title,
    'meta_keywords' => $request->meta_keywords,
    'meta_description' => $request->meta_description,
    'slug' => $request->slug,
    'created_at' => Carbon::now()

  ]);

  return Redirect()->back()->with('success','Brand Inserted Successfully');

}

public function Edit($id) {

  $brands = Brand::find($id);
  return view('admin.brands.edit', compact('brands'));

}

public function Update(Request $request, $id) {

  $validateData = $request->validate([

    'brand_name' => 'required|min:4',
    'meta_title' => 'required|max:70',
    'meta_keywords' => 'required',
    'meta_description' => 'required|max:160',
    'slug' => 'required',
  ],


  [
    'brand_name.required' => 'Please Type The Brand Name',
    'brand_name.min' => 'The Brand Name Must Be At Least Four Characters',
    'meta_title' => 'Please Enter Meta Title',
    'meta_title.max' => 'The Meta Title Can Not Be More Than 70 Characters',
    'meta_description' => 'The Meta Description Can Not Be More Than 160 Characters'

  ]);

  $old_image = $request->old_image;
  $brand_image = $request->file('brand_image');

  if($brand_image){

    $name_gen = hexdec(uniqid());
    $img_ext = strtolower($brand_image->getClientOriginalExtension());
    $img_name = $name_gen.'.'.$img_ext;
    $up_location = 'images/brands/';
    $last_img = $up_location.$img_name;
    $brand_image->move($up_location,$img_name);

    unlink($old_image);

    Brand::find($id)->update([

      'brand_name' => $request->brand_name,
      'brand_image' => $last_img,
      'meta_title' => $request->meta_title,
      'meta_keywords' => $request->meta_keywords,
      'meta_description' => $request->meta_description,
      'slug' => $request->slug,
      'created_at' => Carbon::now()

    ]);

    return Redirect()->back()->with('success','Brand Updated Successfully');

  }

  else{

    Brand::find($id)->update([

      'brand_name' => $request->brand_name,
      'meta_title' => $request->meta_title,
      'meta_keywords' => $request->meta_keywords,
      'meta_description' => $request->meta_description,
      'slug' => $request->slug,
      'created_at' => Carbon::now()

    ]);

    return Redirect()->back()->with('success','Brand Updated Successfully');

  }


}

public function Delete($id) {

  $image = Brand::find($id);
  $old_image = $image->brand_image;
  unlink($old_image);

  Brand::find($id)->delete();
  return Redirect()->back()->with('success','Brand Deleted Successfully');

}

public function render(Brand $brands)
{
   $this->brands = Brand::all();

    return view('livewire.shop.brands');
}

}

Please or to participate in this conversation.