I want to build a formular in laravel.
My form blade php:
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Posting') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<form method="POST" action="/laravel/public/posts">
{{ csrf_field() }}
<h1 style="font-size: 30px; font-family: Tahoma; padding-left: 20px; padding-top: 20px;">Post publizieren</h1>
<div style="font-size: 15px; font-family: Tahoma; padding-left: 20px; padding-top: 20px;">
<label for="text">Titel:</label>
<input type="text" id="title" name="title" placeholder="Titel eingeben... " style="height:30px;">
</div>
<div style="font-size: 15px; font-family: Tahoma; padding-left: 20px; padding-top: 20px; " >
<label for="text" >Text:</label>
<input type="text" id="text" name="text" placeholder="Hier Text eingeben... " style="height:90px;">
</div>
<div style="font-size: 15px; font-family: Tahoma; padding-left: 20px; padding-top: 20px;">
<label for="hashtags">Hashtags:</label>
<input type="text" id="hashtags" name="hashtags" placeholder="#lulufm #radio #queer..." style="height:30px;">
</div>
<div style="font-size: 15px; font-family: Tahoma; padding-left: 20px; padding-top: 20px;">
<label for="bild">Bildlink:</label>
<input type="file" id="bild" name="bild" accept=”image/png, image/jpeg”>
</div>
<div style="font-size: 15px; font-family: Tahoma; padding-left: 20px; padding-top: 20px;">
<label for="mp3">MP3:</label>
<input type="file" id="mp3" name="mp3">
</div>
<div style="padding-left: 20px; padding-top: 20px; padding-bottom: 20px;">
<button type="submit" style="background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px;">Publizieren</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</x-app-layout>
My PostController.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsPost;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function store(Request $request)
{
Post::create([
'title'->request('title'),
'text'->request('text'),
'hashtags'->request('hashtags'),
'bild'->request('bild'),
'mp3'->request('mp3')
]);
return redirect('/dashboard');
}
/**
* Display the specified resource.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param IlluminateHttpRequest $request
* @param int $id
* @return IlluminateHttpResponse
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function destroy($id)
{
//
}
}
My Post Model:
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Post extends Model
{
protected $fillable = [
'userid',
'title',
'text',
'hashtags',
'bild',
'mp3',
];
}
My web.php routes:
<?php
use IlluminateSupportFacadesRoute;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return view('welcome');
});
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');
Route::get('/create', function () {
return view('create');
})->middleware(['auth', 'verified'])->name('create');
Route::get('/generator', function () {
return view('generator');
})->middleware(['auth', 'verified'])->name('generator');
Route::post('/posts', 'AppHttpControllersPostController@store')->middleware(['auth', 'verified']);
require __DIR__.'/auth.php';
On submitting form, the error "Call to a member function request() on string" occurs
Is it a wrong syntax in the controller or does it relate to something else?
How should i write the controller differently?
What am i doing wrong?
3
Answers
The problem is going to be
'title'->request('title')
(and all the similar lines below it).'title'->
means you’re trying to call a function on'title'
which is clearly a piece of text and therefore doesn’t have any functions.If you’re trying to create an associative array, where "title" is a key and the value comes from the request data, then
=>
is the syntax to assign a value to a keyYou need
$
in front of "request", so PHP knows you’re trying to access that variable, and$request
will be an object itself, so you need to use->
to denote access to its functions, not(
and)
.I think you’ve got confused between the syntax for calling a function on an object, and syntax for creating and accessing arrays.
Relevant documentation: https://www.php.net/manual/en/language.types.array.php
You are using a wrong syntax for array:
Do this instead
That should fix your issue. But Since your form field names match your table columns you can use a simpler method:
And, Do not forget to add validation.
First, In the form, action should be
Then in the controller.
For Validation and more, check this example