Attempt to read property "color_name" on bool

Advertisements I want to display the first color of the product. Product.php public function colors() { return $this->belongsToMany(Color::class); } blade @if($product->colors->count() > 0) @foreach($product->colors->first() as $color) <div data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="{{ $color->color_name }}"> <input type="radio" id="color-{{ $color->id }}" name="color" value="color-{{ $color->id }}"> <label for="color-{{ $color->id }}"> <span> <img src="{{ asset(‘themes/images/check.svg’) }}" alt="{{ $color->color_name }}"> </span> </label>… Read More Attempt to read property "color_name" on bool

sync table based on pivot column value

Advertisements I have this schema for the M-M table in laravel 9 Schema::create(‘role_user’, function(Blueprint $table)) { $table->bigInteger(‘role_id’)->unsigned(); $table->foreign(‘role_id’)->references(‘id’)->on(‘roles’); $table->bigInteger(‘user_id’)->unsigned(); $table->foreign(‘user_id’)->references(‘id’)->on(‘users’); $table->bigInteger(‘business_unit_id’)->unsigned(); $table->primary([‘role_id’, ‘user_id’, ‘business_unit_id’]); } The idea is that for each business unit the user has a different role. I tried to save the data using: $user->roles()->syncWithoutDetaching([$role->id => [‘business_unit_id’ => $businessUnitId]]); is not working properly…… Read More sync table based on pivot column value

how to display data from database in laravel , i stuck in ( $reviews is undefined )

Advertisements Hi Sorry iam new in laravel , iam tring to display data from DB but i get error ($reviews is undefined) in home.blade . Plz help " web.php : use App\Http\Controllers\ReviewController; use Illuminate\Support\Facades\Route; Route::get(‘/’, function () {return view(‘home’);});Route::get(‘/car’, function () {return view(‘car’);});Route::post(‘home’,[ReviewController::class, ‘reviewstore’])->name(‘review.store’); home.blade : <br/> @foreach ($reviews as $review) <li> {{ $review[‘name’]}} </li>… Read More how to display data from database in laravel , i stuck in ( $reviews is undefined )

How to keep old value in select option when updated the form in laravel?

Advertisements How to keep old value in select option when I updated the form? Form: <select class="form-select" name="slug" id="slug" value="{{$project->slug ?? ” }}"> <option value="">Select Category </option> <option value="commercial">commercial</option> </select> >Solution : You can use old() like this <select class="form-select" name="slug" id="slug"> <option value="commercial" {{ old(‘slug’, $project->slug) == ‘commercial’ ? ‘selected’ : ” }}>Commercial</option> <option… Read More How to keep old value in select option when updated the form in laravel?

Using multiple database connections inside controllers

Advertisements I have setup multiple database connections "wc" => [ "driver" => "mysql", "url" => env("DATABASE_URL"), "host" => env("DB_HOST", "127.0.0.1"), "port" => env("DB_PORT", "3306"), "database" => env("DB_DATABASE", "forge"), "username" => env("DB_USERNAME", "forge"), "password" => env("DB_PASSWORD", ""), "unix_socket" => env("DB_SOCKET", ""), "charset" => "utf8mb4", "collation" => "utf8mb4_unicode_ci", "prefix" => "", "prefix_indexes" => true, "strict" => true,… Read More Using multiple database connections inside controllers

Laravel table join and pass the value to view

Advertisements this is my code public function view( $sid ) { $subpage = DB::table(‘subpages’) ->join(‘categories’, ‘subpages.category_id’, ‘=’, ‘categories.id’) ->where(‘random_id’, ‘=’, $sid) ->get(‘subpages.*’, ‘categories.category as category_name’); return view(‘subpage_view’, compact(‘subpage’,’category_name’)); } and I am getting error compact(): Undefined variable $category_name highlighting this line return view(‘subpage_view’, compact(‘subpage’,’category_name’)); Not sure what I am missing here. Note:This is in subpage… Read More Laravel table join and pass the value to view

When using Laravel I can't get an ajax call to pass data to a class method

Advertisements I’m very new to Laravel. I would like to pass two variables from an ajax call to a method within a class. My ajax call (derived from various Google searches) is: var token = $(‘meta[name="csrf-token"]’).attr(‘content’); var inputValue = "Testing"; var anotherInput = "Testing2"; var data = {}; data.anotherInput = anotherInput; data.inputValue = inputValue; data._method… Read More When using Laravel I can't get an ajax call to pass data to a class method

Should pivot tables in Laravel use UUIDs or auto-incrementing IDs?

Advertisements In Laravel, pivot tables are used to define many-to-many relationships between two models. When creating a pivot table, one has the option of defining an auto-incrementing id field as the primary key or using a UUID instead. For example, consider the following pivot table that links a benefits table with a units table: Schema::create(‘benefit_unit’,… Read More Should pivot tables in Laravel use UUIDs or auto-incrementing IDs?