0

I have had problems with my BusinessController for a long time now. It is impossible to understand what is not written so. Please help me fix mistakes; I feel I made a lot of them)

My Problem:

my database business_table.php

class BusinessTable extends Migration
{
    public function up()
    {
        Schema::create('businesses', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('mail');
            $table->string('web-site');
            $table->timestamps();
        });
    }
    
    public function down()
    {
        Schema::drop('business');
    }
}

My Controller: BusinessController.php

<?php

    namespace App\Http\Controllers;
    use \App\Models\Business;
    use Illuminate\Http\Request;

    class BusinessController extends Controller
    {
    public function index()
    {
    $business = \App\Models\Business::all();
    return view('business', compact('business'));
    }
    public function createbusiness()
    {
    return view('/createbusiness');
    }
    public function store()
    {
    return view('/business');
    }
    public function create()
    {
    return view('business.create');
    }
    public  function store()
    {
    $business = new Business();
    $business->title = request()->input('title');
    $business->description = request()->input('description');
    $business->save();
    return redirect('/business');

    }
    }

My Model: Business.php

<?php

namespace App\Models;

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

class Business extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'business';
}

My view file: business.blade.php

@extends('layouts.layout')
@section('title')Бізнес@endsection
@section ('main_content')
    <h1>Бизнес</h1>
    <p>
    @foreach ($business as $singleBusiness)
        <li>{{ $singleBusiness->title}}</li>>
        @endforeach
        </p>
@endsection

my createbusiness.blade.php:

<p>It works</p>
<form method="post" action="/business">
    {{ csrf_field() }}
    <div>
        <input type="text" name="title" value="" placeholder="title">
    </div>
    <div>
        <textarea type="text" name="description" value="" placeholder="description"></textarea>
    </div>
    <div>
        <button type="sumbit">Sumbit</button>
    </div>
</form>

My web.php

<?php

    use App\Http\Controllers\UserController;
    use Illuminate\Support\Facades\Route;

    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group, which
    | contains the "web" middleware group. Now create something great!
    |
    */

    Route::get('/welcome', 'App\Http\Controllers\MainController@welcome');
    Route::get('/users', 'App\Http\Controllers\MainController@users');
    Route::get('/business', 'App\Http\Controllers\BusinessController@index');
    Route::post('/business', 'App\Http\Controllers\BusinessController@store');
    Route::get('/projects', 'App\Http\Controllers\ProjectsController@index');
    Route::post('/projects', 'App\Http\Controllers\ProjectsController@store');
    Route::get('/projects/create', 'App\Http\Controllers\ProjectsController@create');
    Route::get('/business/create', 'App\Http\Controllers\BusinessController@createbusiness');
    Auth::routes();

    Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

my MainController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MainController extends Controller
{
    public function welcome()
    {
    return view('/welcome');
    }
    public function users()
    {
    return view('users');
    }
}

my problem image

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Mineral
  • 39
  • 1
  • 1
  • 7

2 Answers2

0

you have to add route

Route::get('/business', 'App\Http\Controllers\BusinessController@index');

you are not invoking business controller with index method, which passes:

return view('business', compact('business'));

business to business view.

UPDATE

remove this route which results in conflict:

Route::get('/business', 'App\Http\Controllers\MainController@business');

for every path (like '/business') and verb (like get/post) only one route should exists.

UPDATE 2

oh my gosh, you have 2 store function in your BusinessController, Remove one.

Abilogos
  • 4,777
  • 2
  • 19
  • 39
0

as your using

Route::get('/business','App\Http\Controllers\MainController@business');

in your MainController.php

public function business()
{
    $business = \App\Models\Business::all();
    return view('business', compact('business'));
}

here you miss to give $business data and loading business.blade.php

Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33