Home > database >  laravel - formular - Call to a member function request() on string
laravel - formular - Call to a member function request() on string

Time:09-30

I want to build a formular in laravel.

My form blade php:

<x-app-layout>
    <x-slot name="header">
        <h2 >
            {{ __('Posting') }}
        </h2>
    </x-slot>

    <div >
        <div >
            <div >
                <div >
                    <div >
                        <div >



                                    <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 App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    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 \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

My Post Model:

<?php

namespace App\Models;

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

class Post extends Model
{
    protected $fillable = [
            'userid',
            'title', 
            'text', 
            'hashtags', 
            'bild',
            'mp3',
]; 
}

My web.php routes:

<?php

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('/', 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', 'App\Http\Controllers\PostController@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?

CodePudding user response:

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

  1. => is the syntax to assign a value to a key

  2. You need $ in front of "request", so PHP knows you're trying to access that variable, and

  3. $request will be an object itself, so you need to use -> to denote access to its functions, not ( and ).

Post::create([
  'title' => $request->input("title"),
  'text' => $request->input("text"),
  'hashtags' => $request->input("hashtags"),
  'bild' => $request->input("bild"),
  'mp3' => $request->input("mp3")
]);

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

CodePudding user response:

You are using a wrong syntax for array:

Post::create([

            'title'->request('title'),
            'text'->request('text'),
            'hashtags'->request('hashtags'),
            'bild'->request('bild'),
            'mp3'->request('mp3')
    ]);

Do this instead

Post::create([

            'title' => $request->input('title'),
            'text' => $request->input('text'),
            'hashtags' => $request->input('hashtags'),
            'bild' => $request->input('bild'),
            'mp3' => $request->input('mp3')
    ]);

That should fix your issue. But Since your form field names match your table columns you can use a simpler method:

Post::create($request->input());

And, Do not forget to add validation.

  • Related