V
V
Vadim Cherkashin2018-08-04 03:26:00
Laravel
Vadim Cherkashin, 2018-08-04 03:26:00

How in my case to receive and deduce a variable?

There is a table of posts, I need to get using the model only those that are the user who went to the page for viewing their posts (in the table there is a field in which there is an author). Then pull out separate columns from everything received, for example title, id. And what I got to display in the Blade template.
I myself didn’t do anything and dance as soon as I didn’t, but my experience is still not enough. I catch a lot of bugs. Correcting which I get new ones.

use App\all; // my model posts
...
class DashPosts extends Controller
{
    public function index()
    {
        $data = all::where('postAuthor', '=', Auth::user()->name)->value('id', 'title');
        $data = array_only($data, ['id', 'title']);
        return view('posts', ['data' => $data]);
    }
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Victor, 2018-08-04
@WebProgrammer

The value method takes 1 parameter and returns the value of the column of the first record in the selection. Why he is needed here is not at all clear.
You probably need something like all::where(...)->select("id", "title")->get().
Well, yes, next time post the error code and dump variables, there are no psychics here.
The name all for the post model is confusing.
And in general, read about relationships, all this can be made even easier.

S
Spetros, 2015-01-11
@Spetros

In the browser everything is OK, in the terminal there are crocozyabras. What can be wrong?

Obviously in the terminal. Does it support utf-8 exactly?

I
IceJOKER, 2015-01-11
@IceJOKER

Finally learn how to use the search engine for such simple questions!
anvarichn.livejournal.com/43752.html

S
Sergey Petrikov, 2015-01-11
@RicoX

Switch the encoding of the terminal itself to the desired one, I don’t know what OS you have, for a poppy it’s done like this:
0852d2321f0548a99fd54f512eec75e3.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question